Logo

"Graphical user interfaces make easy tasks easier, while command line interfaces make difficult tasks possible."


* Most of the material is taken from this book.

Shell Scripting

A Simple Shell Script

#!/bin/bash
clear
echo "Hello world!"

The first line tells the operating system what shell to use to interpret the script and the location of the shell.
We need the following command to make the file executable: $ chmod 755 script_name (or $ chmod +x script_name).
We can then run the script by $ ./script_name.
Alternatively, we could use $ bash script_name. In This method we don't need to make the file executable.

All shell commands and scripts generate a return value upon finishing execution; the value can be set with the exit statement (default 0). The return value is always stored in the $? variable.

Variables

$RANDOM Generates a random integer number (0 - 32767)

Reading Keyboard Input

read -p "Please enter your name: " USER_NAME

With -p option we can provide a message before the user input.
read -t 5 FILENAME # wait up to 5 seconds to read a filename
-n 10 FILENAME # read no more than 10 characters
The -r (raw input) option disables the backslash escaping of special characters.

printf

printf is very similar to the C standard I/O printf() function, but they are not identical. In particular, single- and double-quoted strings are treated differently in shell scripts than in C programs.
The first parameter is a format string describing how the items being printed will be represented. For example, the special formatting code “%d” represents an integer number, and the code “%f” represents a floating-point number:
$ printf "%d and %f\n" 5 5
		5 and 5.000000

if command

if test Condition; then
  Expression1
else
  Expression2
fi

We can use square brackets instead of test:
if [ Condition ]; then
  ...
fi

We can use carriage return instead of semi-colon (;) :
if [ Condition ]
then
  Expression1
else
  Expression2
fi

Make richer Conditions by using -a (and), -o (or), ! (not).
Unlike most programming languages, in BASH the "not" operator doesn't take precedence over "and" and "or". So use parenthesis if needed.
if [ \( ! -f "$TMP1" \) -a -f "$TMP2" ]

Alternatively we can use &&, || for 'and', 'or'.
if [ \( ! -f "$TMP1" \) ] && [ -f "$TMP2" ]

* help test gives useful options.
Writing if commands in terminal:
if [ condition ]; then <rest of code> ; fi
Alternatively,
$ if [ condition ]; then
> <rest of code>
> fi

Arrays

Command History

The easiest way to browse the command history is with the Up and Down arrow keys. The history can also be searched with an exclamation mark (!). This denotes the start of a command name to be completed by Bash. Bash executes the most recent command that matches. For example,
$ !d
date
Thu Apr 13 08:24:51 UTC 2017

!!repeats the last command. A negative number indicates the relative line number. That is, it indicates the number of commands to move back in the history to find the one to execute. !! is the same as !-1.

The !# repeats the content of the current command line. (Don’t confuse this with #!) Use this to run a set of commands twice.

history n lists the last n commands.

Looping Constructs

for i in list
do
	something
done
sum=0
for i in 1 2 3 4
do
	sum=$(($sum+$i))
done
echo "The sum of $i numbers is: $sum"

while condition is true
do
  something
done

until condition is false
do
  something
done

Security

Create random and unpredictable filenames for temporary storage with the mktemp utility:
TEMP=$(mktemp /tmp/tempfile.XXXXXXXX) # To create a temporary file
TEMPDIR=$(mktemp -d /tmp/tempdir.XXXXXXXX) # To create a temporary directory

The XXXXXXXX is replaced with random characters by the mktemp utility.

Misc.

Debugging

We can run a script in debug mode by bash –x ./script_file.
Inside a sccript we can do it by
set -x # turns on debugging
...
set +x # turns off debugging

Quick Refference Table

${#str} Length of the string str
${str:i:j} String Slicing. i: start, j: length
${str#*i} String Slicing. The part of str after character i.
$n Referring to the nth argument that is given alongside the command