Bash Scripting Tutorial

Please read and write these tutorial scripts yourself to familiarize yourself with the syntax and common commands in bash scripting. The line numbers are included only for reference in the tutorial, they are not used in the scripts themselves.

The first script we will write is the iconic Hello World script, this will show how to create a variable and print text to the console. However before beginning your script first type the following command into your terminal: which bash

This will print a line something like "/usr/local/bin/bash" to your console which we may need in your script. The full script we will be writing looks like this:

  1. #!/bin/bash
  2. #declare STRING variable
  3. STRING="Hello World"
  4. #print variable on a screen
  5. echo $STRING

We will first run this script and then look at what each line is doing. Create a file called "hello_world.sh" using your favorite text editor and put the above script into the document without the line numbers. Then save the file and use the command "chmod +x hello_world.sh" in the directory you saved the file in, and then type "./hello_world.sh" to execute our script. If the script does not run then replace the first line with the file path that was returned by your "which bash" command. If you still encounter problems, please speak to a TA during the next available lab hours, do not wait until the project is due to try to fix any problems you encounter. It is most beneficial to rewrite this code yourself, but a copy is available for download here.


if_else.sh

  1. #!/bin/bash
  2. directory="./Scripting"
  3. #if statement to check if directory exists
  4. if [ -d $directory ]; then
  5.     echo "Directory exists"
  6. else
  7.     echo "Directory does not exist"
  8. fi

Again it is best to rewrite this code yourself but a copy is available for download here. See the previous example for how to run this script.


pipes.sh

  1. #!/bin/bash
  2. echo "Retrieving data about" $USER
  3. who | grep $USER

This short and simple bash script demonstrates the use of pipes and the environment variable $USER in order to retrieve information about the current user using the who and grep commands. For more information about these commands type "man who" or "man grep" into your terminal. To get a better idea of what they return, also try typing the command who itself into your terminal. This illustrates how you can use commands such as cd or who in both the terminal and bash script for the same effect. To get a list of all environment variables type printenv into your terminal. A copy of this script is available here.


passing_arguments.sh

  1. #!/bin/bash
  2. echo $1 $2 $3

A copy of this script is available here. Run this script by typing "./passing_arguments.sh Why Hello There!" into your terminal.


file_io.sh

  1. #!/bin/bash
  2. echo Hello there people! > outfile.txt
  3. cat < outfile.txt

A copy of this script is available here.


mail_hello.sh

  1. #!/bin/bash
  2. echo "Hello $USER!!" > tmp-message
  3. echo >> tmp-message
  4. echo "Today is" `date` >> tmp-message
  5. echo >> tmp-message
  6. echo "Sincerely," >> tmp-message
  7. echo " Myself" >> tmp-message
  8. /usr/bin/mailx -s "mail-hello" $USER < tmp-message
  9. echo "Message sent."

This script will also be discussed in lecture and is included in the class slides.


loops.sh

  1. #!/bin/bash
  2. # for loop
  3. for f in $( ls /var/ ); do
  4.     echo $f
  5. done
  6. COUNT=6
  7. # while loop
  8. while [ $COUNT -gt 0 ]; do
  9.     echo Count: $COUNT
  10.     let COUNT=COUNT-1
  11. done

This script can be found here.


string_manipulation.sh

  1. #!/bin/bash
  2. foo="Hello"
  3. foo="$foo World"
  4. echo $foo
  5. a="hello"
  6. b="world"
  7. c=$a$b
  8. echo $c
  9. echo ${#foo}

This script is fairly self-explanatory. It shows two fairly similar ways to concatenate strings, one is no better than the other and it is up to personal preference which you use. The last line shows how to get the number of characters in a string.

environment_variables.sh

  1. #!/bin/bash
  2. echo $HOME
  3. echo $PATH
  4. echo $USER

This script is a simple one, it illustates the use of "environment variables". You can see all the environment variables currently available by using the command printenv. These are variables that are persistent throughout the terminal shell you are using and are available to programs and scripts run in the shell.

example.sh

  1. #!/bin/bash
  2. COUNT=10
  3. if /usr/xpg4/bin/egrep -q [0-9] testdoc.txt ; then
  4.     let COUNT=COUNT+5
  5. fi
  6. echo $COUNT

This script is a simple example of what part of your script for the project might look like. This will check if the document contains a line with numbers and then will increment the count if it does

References:
http://bash.deta.in/abs-guide.pdf
http://www.faqs.org/docs/bashman/bashref_68.html
http://linuxconfig.org/bash-scripting-tutorial#h12-loops