Scripts are text files with list of commands that need to be executed.
Usually such files are named '.sh' (instead of '.txt').
Each script must begin with:
#!/bin/bash
This tells linux which shell must be used when executing commands (usually bash).
Example 1:
#!/bin/bash
echo Hello from Bash World
Example 2:
#!/bin/bash
echo I will create a backup of my files
tar -cZf /home/backupFiles/my-backup.tgz /home/me/
In scripts you can also use variables. Create a variable with custom name and
assign a value to it.
Call that variable value with '$' and its name.
If you are reffering to a value which is a result of some other command use $(command).
First the command in brackets is executed and its return value is used. Eg. $(ls).
Example 3:
#!/bin/bash
STR1="Hello again!"
echo $STR1
STR2="The time is"
echo $STR2 $(date)
STR3="The contents of your diractory:"
echo $STR3
echo $(ls)
Creating conditionals.
Example 4:
#!/bin/bash
if [ 2 = 2 ]; then notice white spaces between ALL characters!!
echo two equals two
fi
Example 5:
#!/bin/bash
if [ 2 = 1 ]; then
echo two equals two
else
echo two is not equal to one
fi
Loops.
The use of loops is little different than in other programming languages.
for is usually used to iterate over string of words
while executes until the control expression no more equals true
until executes until the control expression is false (similar to while)
Example 6:
#!/bin/bash
for i in $(ls); do
echo item: $i
done
Example 7:
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do lt=less then
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
Example 8:
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER-=1
done
Functions.
Declare a function as: func_name { the code } and then just call its name.
Example 9:
#!/bin/bash
function sayHello {
echo Hello!
}
function displayDate {
echo $(date)
}
sayHello
displayDate
Create a function with parameters
Example 10:
#!/bin/bash
function askQuestion {
echo What is this:
}
function e {
echo $1
}
askQuestion
e olleH
e dlroW
User interfaces.
Using 'select' to make simple menus.
Example 11:
#!/bin/bash
OPTIONS="Hello Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo done
exit
elif [ "$opt" = "Hello" ]; then
echo Hello world
else
clear
echo Bad option
fi
done
Using command line.
This example expects a parameter (directory name) from command line and creates an
archive of that directory. Execute this script as:
./testScript myDirectory
Example 12:
#!/bin/bash
if [ -z "$1" ]; then
echo usage: $0 directory
exit
fi
SRCDIR=$1
TGTDIR="/Users/matjaz/"
OF=home-$(date +%Y%m%d).tgz
tar -cZf $TGTDIR$OF $SRCDIR
Reading user input with read.
Read method can accept more than one parameter.
Example 13:
#!/bin/bash
echo Enter your name:
read NAME
echo "Hi $NAME!"
echo Enter your firstname and lastname
read FIRST LAST
echo "My name is $LAST. $FIRST $LAST."
Arithmetic evaluation.
Bash shell is calculating fractions with integers. Thats why 3/4 produces an output 0.
You can pipe 3/4 to the bc calculator which evaluates the expression before displaying it.
Example 14:
#!/bin/bash
echo 1+1
echo $((1+1))
echo $[1+2]
echo $[3/4]
echo 3/4|bc -l
Getting the return value from the script.
In bash the return value is stored in a special variable called $?.
Example 15:
#!/bin/bash
cd /dadada &> ./testFile.txt
echo rv: $?
cd $(pwd) &> ./testFile.txt
echo rv: $?
Following example stores path to current directory, goes to another directory,
executes some java code and then returns back to current directory.
Example 16:
#!/bin/bash
currDir=$(pwd)
cd /Users/matjaz/knopflerfish_osgi_1.3.6/knopflerfish.org/osgi
java -jar framework.jar
#echo $currDir
cd $currDir
Check if command line arguments exist. -z operator checks if argument's size is zero.
Example 17:
#!/bin/bash
if [ -z $1 ]; then
echo no arg
else
echo $1
fi