Questions tagged [bash-scripting]

Bash scripting is making scripts in the Bash shell language.

Bash has several features which allow making small uncompiled programs (scripts) to automate functionality. It features basic programming constructs like variables, IF-statements and loops.

Bash scripts are usually created in files with the .sh extension. To make the file executable as it were a program, put the following line at the top of the file:

#!/bin/bash

This will instruct the kernel to start a Bash shell and use the rest of the file as input. After adding the 'shebang' line, change the permissions of the file to be executable with chmod +x filename.sh. You can then run the script with ./filename.sh.

Example IF-statement

VARIABLE1='hello world'
if [[ $VARIABLE1 == 'hello world' ]]
then
  echo 'Hello World!' 
fi

In this example we see:

  • Variable $VARIABLE1 is set to the value hello world
  • The content of $VARIABLE1 is matched against the string hello world
  • If this is true, then print Hello World! to the screen.

Example Loop

Several kinds of loops can created in bash:

For loop

VARIABLE1="anna bob charlie dave eve"
for NAME in $VARIABLE1
do
  echo $NAME
done

The for statement will iterate through the names in $VARIABLE1 and run the code block between do and done for each name. This will output all 5 names separated by newlines.

You can also create a C-style for-loop:

for ((i = 0 ; i < 10 ; i++)); do
  echo $i
done

This will print numbers 1 through 9 separated by newlines.

While loop

VARIABLE1=1

while [ $VARIABLE1 -lt 10 ]
do
  echo "$VARIABLE1"
  VARIABLE1=$[$VARIABLE1+1]
done
953 questions
9
votes
2 answers

Use "read" in Bash script with a standard answer

How can I use read with something as standard response, which the user can change though? (a default answer)
Flo
  • 101
3
votes
2 answers

How to work around Bash for loop not exiting when a subcommand errors?

I have a for-loop in a shell script a la: #!/bin/bash set -u set -e for l in sh rb py php java cs; do (cd $l; ./run-tests.sh) done The intent is to have the for-loop die in a fire when any one of the sub-commands similarly errors out. Now, I have…
2
votes
1 answer

Bash script to execute jar with parameters and using file path variables

I am trying to run the following bash script and it gives me the following error #!/bin/bash -x jars= $HOME/SelGridProto/selserversidedjars java -jar $jars/selenium-server-standalone-2.44.0.jar -role hub -hubConfig $jar/ServerHub.json Upon bash…
2
votes
4 answers

BASH - grep - works on command line, but not script

I am trying to get the number of occurrences in a script and when running the command on the command line, it works fine, but not in a script. Both variables are initialized. FILE_PATH is the absolute path of the file and VARIABLE_NAME is…
Walter
  • 1,009
2
votes
2 answers

Why can't I delete a file with a Bash script?

I can't seem to run rm from a Bash script and remove a file. #!/bin/bash rm -rf myjunk.out exit 0; doesn't remove myjunk.out.
2
votes
1 answer

How can i get the case and user input into a tee

I work in support and am creating a script that allows a user to quickly gather information for diagnostic reasons. I have multiple searches and choose to use the case method inside a .sh main() { #What we looking for? echo "Whats broken? ( Key ,…
Alex R
  • 51
2
votes
2 answers

Bash scripting: 365-024=345?

I am trying to build some beginner scripts with bash. I want to find how many days left from today until the end of the year, using date program. So I am using a variable, current_date=$(date +%j) to get the number of the day we have now. If I …
Mr T
  • 125
1
vote
1 answer

[: -gt: unary operator expected

I have a code, which gives [: -gt: unary operator expected, when the value is empty. Can anyone please suggest or correct me where I'm wrong ? if [ -e $POSFile ]; then # Read last Position lastPosition=`cat $POSFile` fi fileLength=`stat -c %s…
1
vote
2 answers

Bash Script Error

I'm trying to write a bash script to manage setting up a user profile. I can't seem to figure out why this if statement won't work. I have the code: #!/bin/bash #check if programs are installed ( TMUX=$(tmux -V) && echo "tmux at version $TMUX" ) ||…
Jacobm001
  • 151
1
vote
3 answers

How could I make a bash script to execute apt-get?

I'm trying to automatize some configurations I have with bash script, I've never done this before so I tried with something easy like a Hello World! and everything works just fine, but then I tried something like this: #!/bin/bash sudo su apt-get…
poz2k4444
  • 904
1
vote
2 answers

What's the easiest way to 'cat' groups of files together?

I have files with naming convention of this pattern: bond_7.LEU.CA.1.dat bond_7.LEU.CA.2.dat bond_7.LEU.CA.3.dat bond_12.ALA.CB.1.dat bond_12.ALA.CB.2.dat bond_12.ALA.CB.3.dat ... I want to concatenate all files of the same group into a single one.…
rajitha
  • 11
1
vote
1 answer

If or statement with $1 parameters keeps failing

I have written the following bash script to run bleachbit. #!/bin/bash # Use p for preview and c for activating cleaners after script name followed by a gap. Example: bb p or bb c. if [[ $1 != "p" ]] || [[ $1 != "c" ]] || [[ $# -eq 0 ]] then …
1
vote
1 answer

Extract a value from a text file in a specific position

I have a file containing tons of information. It looks like this: =============================================================================== NSTEP ENERGY RMS GMAX NAME NUMBER 52 -4.8969E+05 …
1
vote
1 answer

Variable into another variable with simple quotation

I have this command (user is the name of my user account: echo $USER): sudo -H -u user bash -c 'DISPLAY=:0 /usr/bin/dbus-launch /home/user/myappimage start -i &' Work fine. Now i want to create some variables to replace my user and path…
acgbox
  • 785
1
vote
1 answer

how to set variables using IFS

I have a file, input.txt, with the following values: field1|value1 field2|value2 field3|d:\foldername field4|null How do I set variables to column1 and column2 so that I can then evaluate column2 for null and display an error message? I can do this…
1
2 3