Gs Shellscripting 2019 Nov11
Gs Shellscripting 2019 Nov11
computing
• Finding things
http://swcarpentry.github.io/shell-novice/07-find/index.html
#!/bin/bash
# My first script
first.sh
Executing a script
bash-4.1$
bash-4.1$
bash-4.1$ bash first.sh
This is my first script!
bash-4.1$
bash-4.1$
bash-4.1$ chmod a+x first.sh
bash-4.1$
bash-4.1$ ./first.sh
This is my first script!
bash-4.1$
Scripting in Bash shell
#!/bin/bash
MESSAGE=”Hello world!”
echo $MESSAGE
varfirst.sh
Variables - user-defined
#!/bin/bash
GREETING=Hello
NAME=Bob
MESSAGE=”$GREETING $NAME”
echo $MESSAGE
varfirst.sh
Variables – assign an output of a command
#!/bin/bash
# date is a command
VAR=$(date)
echo "today is $VAR"
varfirst.sh
Variable declaration
• Environment variables
• e.g. $PATH, $SHELL, $USER, $HOME
• Exit value
• $?
Variable type
• $ denotes a variable
• ` evokes a command execution
• " closes initial double quote
• \ means ignore special meaning of the next character
Single and double quotes
VAR=foo
echo $VAR
foo
bash-4.1$ a=1
bash-4.1$ b=2
bash-4.1$ ((c=a+b))
bash-4.1$ echo $c
3
bash-4.1$ echo $((b-a))
1
bash-4.1$ echo $((b*a))
2
bash-4.1$ echo $((a/b))
0
bash-4.1$ echo $((b**3))
8
bash-4.1$
Control flow statements
• if
• test
• loops
• case
If statement
if [ expression ]; then
echo "it is true"
else
echo "it is false"
fi
ifelse.sh
If + elif statements
if [ expression1 ]; then
# if code
elif [ expression2 ]; then
# elif code
else
# else code
fi
Logical expression
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to
-gt is grater than
-ge is greater than or equal to
Logical expressions – logical comparison
for i in {1..4}
do
echo "Iteration $i."
done
bash-4.1$ ./forloop.sh
Iteration 1.
Iteration 2.
Iteration 3.
Iteration 4.
forloop.sh
For loop with a list
bash-4.1$ ./forloop.sh
Mercury
Venus
Earth
Mars
Jupiter
forloop.sh
For loop with a string – quotes matter
bash-4.1$ ./forloop.sh
Mercury Venus Earth Mars Jupiter
forloop.sh
For loop – iterations controlled by a variable
number=4
for num in $(seq 1 $number); do
echo $num;
done
bash-4.1$ ./forseq.sh
1
2
3
4
forseq.sh
While loop – iterates until a condition is false
while [ condition ]
do
echo "condition still true"
done
echo "condition is false"
While loop - read a file
while.sh
Case
$var="Monday"
case $var in
"Monday") echo "New week, new possibilities!";;
"Friday") echo "TGIF!";;
*) echo "Hang in there!";;
esac
caseweek.sh
Functions
myfunction () {
echo "This is a function!"
}
func.sh
Functions – variable scope
ftest () {
x=3
echo "x in function: $x"
}
x=2
echo "x in main: $x"
ftest
echo "x in main: $x"
bash fnctn.sh
x in main: 2
x in function: 3
x in main: 3 fnct.sh
Functions – local variables
# declare function vars as local to limit their scope
ftest () {
local x=3
echo ”x in function: $x”
}
x=2
echo ”main x: $x"
ftest
echo ”main x: $x”
bash-4.1$ ./funcvar.sh
main x: 2
x in function: 3
main x: 2 funcvar.sh
Functions – passing parameters
hello ()
{
echo "Function params: $@"
}
#function call
hello 1 2 3
bash-4.1$ ./funcposind.sh
Function params: 1 2 3
funcpos.sh
Functions – return values
A bash function can return an integer between 0 and 255 using the
“return” command that is essentially designed to use the Unix exit
status codes.
fhello (){
return 0;
}
fhello
echo "return is $?"
funcsimple.sh
Follow your script step-by-step