- Introduction
- Commenting
- Conditional Statements
case
Statement- Looping
- Array
- Command Line Arguments
- Exit Staus
All shell script files start with #!/bin/bash
line.
This is called a shebang. This tells which interpreter
should call to parse the script. Example of a shell
script file:
#!/bin/bash
echo "hello, world"
Any line begin with #
is a comment.
#!/bin/bash
# This is a comment
echo "hello, world" # This is a comment too
Feed text as it is into the standard input including whitespace and quotation marks. Here documents have the following form:
command << token
text
token
For example following script will feed the whole text to cat
. As a result whole text will
be printed as it is into the shell.
#!/bin/bash
cat << __EOF__
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
__EOF__
- if
<<-
is used insted of<<
, leading tabs will be ignored. - Can be used for multiline comment:
#!/bin/bash
<< __TOKEN__
This will be ignored
This line too
And this
__TOKEN__
echo "hello, world!"
Output:
$ ./script.sh
hello, world!
$
When variable isn't set or have empty string default value can be set.
#!/bin/bash
arg1=${1:-"dfile.txt"}
Following example will show conditional statements in shell scripting
#!/bin/bash
# Structure
#
# if [ Expression ]; then
# do something
# elif [ Expression ]; then
# do something else
# else
# do something else
# fi
number=2
if [ $number -lt "2" ]; then
echo "Less than 2"
elif [ $number -gt "2" ]; then
echo "Greater than 2"
else
echo "Equal to 2"
fi
File expressions can be used to do different tests on file/directory
#!/bin/bash
FILE_NAME=$HOME/hello.o
# Remove file if exists
if [ -e "$FILE_NAME" ]; then # quotation used so that empty variable don't cause error
rm $FILE_NAME
fi
TODO: String expressions and regular expression matching. [[ ]]
and (())
.
To see how to write the conditional expressions see man test
.
case statements has the following form:
case $variable in
pattern1)
do something
;;
pattern2)
do something else
;;
pattern3 | pattern4)
do third important work
;;
*)
do another thing
;;
esac
|
matches multiple patterns.*
default action if no pattern matches.
Example:
#!/bin/bash
# A program to do some usefull things with unix tools
TOOL=shell
case $TOOL in
shell)
echo "Throw into the sea."
;;
strip)
echo "Jump into the water."
;;
time)
echo "Jump in a black hole"
;;
sleep)
echo "Now do a interstellar travel"
;;
*)
echo "You are in a limbo"
;;
esac
#!/bin/bash
# Structure of the for loop
#
# for $variable in sequence; do
# something with the variables
# done
for f in $(ls); do
echo $f
done
Executes while a condition is true.
#!/bin/bash
# Structure of while loop
#
# while [ Expression ]; do
# do something
# done
number=0
while [ $number -lt 10 ]; do
echo "Number: $number"
number=$((number + 1))
done
Executes until a condition is true.
number=0
until [ $number -gt 10 ]; do
echo "Number $number"
number=$((number + 1))
done
Like any other programming languages break
and continue
statement can be applied in a loop to break the loop or continue
the loop on some condition.
Arrays are defined as array_name=(0 "name" 4)
where array_name
is the name of the array
an 0
, name
are the elements of the array.
Array can be subscripted and iterated in following
way:
array_name=(0 "name" 4)
# Print first element of the array
echo ${array_name[1]}
# Iterating through the array
# Print all the elements of the array
for elem in ${array_name[@]}; do
echo $elem
done
Structure of a function in shell script is following:
func_name () {
commands
return
}
# Function call
func_name
Example:
#!/bin/bash
get_hostname () {
echo $HOSTNAME
return
}
get_hostname
- Functions must be defined before they are called.
- In shell all variables are global. A local variable can be created using
local
command.
#!/bin/bash
ID=0
get_hostname () {
local ID
ID=1
echo $HOSTNAME
echo "local ID before calling function: ${ID}"
return
}
echo "global ID before calling function: ${ID}"
get_hostname
echo "global ID after calling function: ${ID}"
sample code:
#!/bin/bash
echo "Number of arguments: '$#'"
echo "All arguments(space seperated): '$@'"
echo "Program name: '$0'"
echo "Argument 1: '$1'"
echo "Argument 2: '$2'"
sample output:
Number of arguments: '2'
All arguments(space seperated): 'hello world'
Program name: './cmd.sh'
Argument 1: 'hello'
Argument 2: 'world'
sample code:
echo "Number of arguments given: $#"
number=1
while [ "$1" != "" ]; do
echo "Argument $number: $1"
number=$((number + 1)) # number+=1
shift
done
sample output:
Number of arguments given: '2'
Argument 1: hello
Argument 2: world
touch
echo $?