Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

3
votes
3answers
36 views

How to pass array to bash shell script?

How do I pass an array as a variable from a first bash shell script to a second script. first.sh #!/bin/bash AR=('foo' 'bar' 'baz' 'bat') sh second.sh "$AR" # foo sh second.sh "${AR[@]}" # foo ...
2
votes
1answer
27 views

Bash shell script program that prompts for and reads two integers from the user.

Okay, so I been trying to figure this problem for a while and I'm coming up with errors left and right. This is my first time trying to learn the basics. So any advice would be helpful! Problem: I'm ...
2
votes
2answers
66 views

Accessing array index variable from bash shell script loop?

I want to access the array index variable while looping thru an array in my bash shell script. myscript.sh #!/bin/bash AR=('foo' 'bar' 'baz' 'bat') for i in ${AR[*]}; do echo $i done The result ...
0
votes
1answer
28 views

Automating Backup and Shutdown

I want to backup my entire internal hard drive files into an external hardrive and execute system shutdown once the backup is finished. The reason why I want to do that is to execute the backup during ...
1
vote
1answer
33 views

Join two files in a batch mode

I have many files with the following names: 0001mm.xyz , 0002mm.xyz , 0003mm.xyz ... 0001qm.xyz , 0002qm.xyz , 0003qm.xyz ... I want to join the contents of 0001mm.xyz and 0001qm.xyz ...
3
votes
2answers
73 views

Changing a file's modified date based on date in file name

I have a directory full of photos with file names in this format: IMG-20160305-WA0001.jpg. The date taken is obviously in the file title. Unfortunately the date modified on all files is today. I ...
3
votes
3answers
218 views

Why and when should eval use be avoided in shell scripts? [duplicate]

Many answers and comments on this site mention that one should generally avoid using eval in shell scripts. Often these comments are addressed to beginners. I've seen mention of security issues and ...
1
vote
1answer
42 views

Aligning Hexdump Data in a Specific format?

I am reading serial data from my Raspberry Pi and all I am doing is a Hexdump I have to find the header and then go to the 2nd byte of data from header for ex: ed 13 74, I have to take the value 4 , ...
0
votes
4answers
46 views

Setting variables from shell: how to use them in a script?

How can I set variables to be used in scripts as well? They don't have to be global / system-wide, just the current session is sufficient. But for some reason they seem gone even if I run a script ...
1
vote
2answers
41 views

Detect and remove empty directories from list

A common problem game developers have with the Unity3D engine and Git is that Unity and Git fight for whether an empty directory should exist or not - Git doesn't care, Unity3D will keep making git ...
0
votes
1answer
43 views

Need help parsing a filename and adding it to a text file

I have a directory full of files with the date they were created in the filename (ie "mvr-20160420.csv"). The files contain comma delimited data that I'm combining into a single text file. I'm ...
0
votes
2answers
35 views

Shell script for executing programs within a loop and checking their exit values

How do I do this in a shell script: for (int i=0;i<1000;i++) { run applicationA run applicationB if (retvalFromApplicationB!=0) {continue;} else {pipe ouptut (stdout) from ...
-1
votes
2answers
45 views

shell script replace and add next line with special characters [closed]

I am trying replace a line with another line and add new line below that using a shell script. I want to replace: export PATH with: ...
0
votes
1answer
56 views

associative array with two strings

is it possible to loop a simple array with key and value with a string in the [key] part ? #!/bin/bash -x wget url://stringlist1 wget url://stringlist2 #cat/sed some stuff .... declare -A ...
1
vote
1answer
38 views

Rewrite a find command that uses sed -i for AIX

I have a script that searches for a string and replaces it via the sed command. If that string contains special characters, the script will escape them (except for the slash because it's my current ...
2
votes
2answers
45 views

pssh (Parallel-ssh) passing different parameter for every host

I have a file called hosts which looks as follow: host1 host2 host3 host4 ..... On these machines there is also a local user named as the machine name. host1 user is created on host1 host and so ...
1
vote
1answer
37 views

How to add an word for a line where transpose has done

I have a file fruits: apple grapes banana orange By using cat < fruits | tr "\n" "," It will give output as apple,grapes,banana,orange, Here I want to print the file name also along with ...
-5
votes
1answer
52 views

help with a bash shell script that creates directories

I have a problem I been trying to tackle. If anyone can help, it would save me a headache! For reference,I started self learning so I'll be looking for problems around but can't find the answer for ...
2
votes
2answers
67 views

bash read of a newline, printf reports character 0

I use bash printf function to print ASCII codes of characters in an input file, but for some reason printf outputs ascii code 0 for LF characters, instead of 10. Any ideas why? while IFS= read -r -n1 ...
0
votes
1answer
53 views

Reading user input with read back

when I use simple bash command #!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!" and run it using source filename.sh it gives me an error :Undefined variable what could be the ...
0
votes
2answers
34 views

Universal abstraction of ls

Specs OS: OS X; Client: Terminal; Shells: sh, bash Problem I'm writing a shell script that adds a layer of abstraction to the ls command. I want to make it "unbreakable", so I started by figuring ...
2
votes
2answers
41 views

Using awk to combine two csv files on three fields too create a new csv file

I would like to compare fields in 2 csv file and create new csv file for output. I have 2 csv files; this first file is PCs in my network: ...
2
votes
1answer
83 views

Multi processing / Multi threading in BASH

I have a test file that looks like this 5002 2014-11-24 12:59:37.112 2014-11-24 12:59:37.112 0.000 UDP ...... 23.234.22.106 48104 101 0 0 8.8.8.8 53 68.0 1.0 1 0.0 0 68 0 48 Each line contains a ...
1
vote
2answers
36 views

Bash find and replace in a C++ file

I have a C++ script for which I would like to change variable values in some places and then compile, doing this many times for a set of numbers. The following shows my shell script: #!/bin/bash ...
6
votes
1answer
144 views

Map ordinal to alphabet in shell script

I'm quite new to shell scripting. Is there any simple solution to map ordinal number like 1, 2, 3, ... to alphabet a, b, c in shell script? Thanks!
1
vote
1answer
25 views

Save number of lines output to a variable in a shell script without consuming the output

I have a shell script where I have a if statement based off of the number output lines output from another call. (In my case do something there is more that 1 line of output). LINES=$(function_call | ...
3
votes
3answers
271 views

Using grep with a pattern that contains a variable to be expanded

I have a file on the form 2e95d7582c53583fa8afb54e0fe7a2597c92cbba 1461065389 52880 temp/hello/file.txt 46c897a7aa8a641f46080b3431860bd0cd4a8f05 1461066221 207 temp/Another file.txt ...
2
votes
3answers
60 views

How do I find my distro name via the command line?

Is there any command that outputs your distro name like this: CentOS Ubuntu,etc.? I am looking to make a script that will execute a different batch of commands for each distro and I need to know how ...
0
votes
0answers
20 views

Printing specific column in bash script and avoiding duplicates [duplicate]

I am writing a Bash script to print a specific column from a file. But that column contains many same strings and I want to just print every content just once. I tried this - awk '!seen{print $9}' ...
0
votes
0answers
25 views

How do i run multiple scripts within another script in the same directory?

Last part of a project and I've created 3 scripts already that do the following, but I now need to run them within a 4th script as if it was one program. Below are the instructions as well as the code ...
2
votes
2answers
120 views

bash source script.sh with exit 0

I use a lot of external files in my scripts. But I have a problem, that if I source a file with exit in the contents of the file, the script exits from loading and the strings that I want to use are ...
1
vote
4answers
232 views

print specific line from multiple files

I have a collection of 5000+ files and I just want to create a output.txt file which contain 27th line of all file along with filename. well I am noob at linux level so what i got from internet is to ...
3
votes
2answers
61 views

use printf to format output of an array

I have an array that contains details about each NIC. Each array index consists of three space-separated values. I would like to have a nice table as output. Is there a way to assign formatting ...
3
votes
1answer
148 views

curl outfile variable not working in bash script

Attempting to shorten a bash script that uses curl to fetch multiple API calls takes something like this: curl --user $USER:$PASS https://api.example.com/foo -o 'foo.json' curl --user $USER:$PASS ...
0
votes
2answers
108 views

How to copy files from directory meeting certain criteria

I'm trying to copy files from directory A to directory B. But I want to copy only files that have lower size then X and don't have file extensions specified in file extension.txt. My main problem ...
0
votes
1answer
34 views

How do I count the number of files in all subdirectories and add the counts together

I am trying to count the number of file in each directory/subdirectory, add them together and get a total which I can then compare to another directory: #!/bin/bash echo "Checking directories for ...
0
votes
1answer
25 views

What are the unreadable characters in Linux Anaconda install script?

The Anaconda for Linux installation scripts contain about 750 lines of a pretty straightforward set of bash commands, but they are then followed with 1.4 million lines of characters unreadable (at ...
2
votes
2answers
40 views

What's the difference between 'test' and evaluating with /dev/null?

I noticed different .sh scripts for homebrew which check whether it's installed. One uses this syntax: if test ! $(which brew); then The other uses this syntax: if ! which brew > /dev/null; ...
1
vote
1answer
91 views

Prevent shutdown using shell script run at shutdown

Background: I have a Python script that runs (infinitely) from startup in the background of a Ubuntu server. The process is ended by sending a SIGINT which is handled by the script which finishes all ...
3
votes
4answers
146 views

Check if a shell variable has more than one line using built-ins?

I can do this by calling the external utility sed (for a known non-empty $myvar) like so: if [ "$(printf %s "$myvar" | sed -n '$=')" -eq 1 ]; then echo "Your variable has only one line, proceeding" ...
0
votes
0answers
31 views

netcat output does not write to array if launched from cron

I am able to run this script as a user or root with bash or sh. Doing so gives me the output that I expect (arrayFWS is written to file, then emailed). However, when I run it with crontab with user or ...
0
votes
0answers
13 views

Declaring multiple arrays from contents in multiple directories, then iterating over those arrays and their contents [duplicate]

In a bash script, I'm trying to declare 15 separate arrays with the contents of a folder for 15 separate folders (IE; array1 = contents of folder1, array2 = contents of folder 2, ..., array15 = ...
1
vote
0answers
45 views

Bash scripts not found. Maybe EOL issue? [closed]

I am facing a problem that is driving me crazy. In the past months I have been developing a project based on a Linux distribution (Openwrt) that has been recently updated. My project has a lot of bash ...
0
votes
1answer
54 views

How do I calculate the column percentage of a file?

I have a tsv file that contains some values. I want the sum of each column and total number of values and percentage values. Eg: file.tsv contains x 1 1 0 1 x x 1 x ...
6
votes
2answers
289 views

Sed with inplace editing changes group ownership of file

I have shell (php) script that gets in touch with target file this way: inspects whether file and directory are writable with php 's is_writable() (I don't think that this is problem) does in-place ...
1
vote
1answer
61 views

Check if the files in a specific directory have the proper extension?

I have created a script that will create a few directories and then organize other files from one directory by moving them into specific sub-directories based on their extension (i.e. .gif in media, ...
0
votes
1answer
24 views

assign string components into variables [duplicate]

Looking for a smarter way to do something I'm doing already. I have a string: "firstbit.secondbit.thirdbit.fourthbit.fifthbit.sixthbit" The content between the periods changes, but the delimited ...
1
vote
2answers
69 views

bash - All possible combinations of words from different files

I have n files with one word per line file 1 file 2 file 3 ... 1_a 2_a 3_a 1_b 2_b 3_b 1_c 3_c I want to write a bash script that takes all this files ...
1
vote
1answer
34 views

bash set -o errexit problem or the way of incrementing variable?

I have a shell script incrementing a variable like in the example below with set -e: $ var=0; echo $? 0 $ ((var++)); echo $? 1 $ ((var++)); echo $? 0 $ ((var++)); echo $? 0 $ echo $var; echo $? 3 0 $ ...
0
votes
1answer
128 views

How to execute shell script on Kali Linux startup?

I need to execute a shell script on a Kali Linux startup. I am using 2016.1 version of Kali Linux. i tried many things like rc.local, init.d, X11 and GDM3 config etc. but I couldn't get it work. ...