Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views26 pages

2023 UNIX Lecture 4 - Bash

This document is a lecture on Bash scripting by Nachum Danzig, covering essential concepts such as creating scripts, using loops, conditional statements, and handling variables. It explains the shebang line, the use of comments, and various testing tools for file and string conditions. Additionally, it discusses input/output operations, environment variables, and variable indirection in Bash.

Uploaded by

Meir Shaanani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views26 pages

2023 UNIX Lecture 4 - Bash

This document is a lecture on Bash scripting by Nachum Danzig, covering essential concepts such as creating scripts, using loops, conditional statements, and handling variables. It explains the shebang line, the use of comments, and various testing tools for file and string conditions. Additionally, it discusses input/output operations, environment variables, and variable indirection in Bash.

Uploaded by

Meir Shaanani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unix Systems Lecture 4

Bash scripting
Nachum Danzig

ⒸNachum Danzig 2021


Shell Programming in Bash
Any command that works in the shell can be put in a file and when you run that file
the command will run

ex. ls

You can put many commands in a file. Running that file will run all the commands
in it.

Each line in the file will be interpreted by the shell you are running (Bash) as a
command. The commands will be run one by one.

ⒸNachum Danzig 2021


Shebang
At the start of a file you can choose to specify which interpreter or shell you want
to be reading the lines of your file. Therefore, you can specify any scripting
language. But you need to specify where the interpreter that will be running your
script is located. Any flags you want to pass to the interpreter should also be
specified. This line is called the shebang line. If you leave out the shebang line,
your shell will interpret the script.

If you want to specify the Bash interpreter to interpret your script and you know
that it is located in /bin, then your shebang could look like this:

#!/bin/bash

ⒸNachum Danzig 2021


Example of “for” loop
Shell understands many programming concepts. For example, in this example,
variable i will be assigned three values in succession. When the loop runs out of
data it will end.
#!/bin/bash
for i in a b c; do
echo $i
done
will print:
a
b
c
ⒸNachum Danzig 2021
Comments
To add comments use #
example: #refer to variable i as $i

When you declare variables you need to define them.


Do this using = with no spaces.
example: myvariable=hello
No need to quote strings. For space, you can enclose in “ or escape the space
with a \ example: myvariable=hello\ world
Further use of variables requires prepending $
example: i=$myvariable

ⒸNachum Danzig 2021


Example 2 of “for”
Here I will run a command and use the output as my list of items to iterate upon.
(Given that in directory mydirectory there are 3 files: klum1, klum2 and klum3)
#!/bin/bash
a=nothing
for i in `ls mydirectory`; do
echo $a$i
done

nothingklum1
nothingklum2
nothingklum3
ⒸNachum Danzig 2021
Exit
Use exit to end the script before the end
Example
#!/bin/bash
a=nothing
for i in $(ls mydirectory); do
echo $a$i
exit
done
Output:
nothingklum1

ⒸNachum Danzig 2021


bash -e
A bash shell script will attempt to run every command you write.

If one command fails, it will print the appropriate error message and continue
running the other commands. This is called running in “batch mode”.

If you want the script to stop running as soon as one of the commands fails, use
the -e flag in the shebang line.

#!/bin/bash -e

This is useful when future commands depend on the success of previous


commands.
ⒸNachum Danzig 2021
Example defining a range of variables in a for loop
#!/bin/bash
for i in {0..10}; do
echo "Welcome $i times"
done
Default incrementation is 1. But you can specify a different number.
#!/bin/bash
for i in {0..10..2}; do #count by twos
echo "Welcome $i times"
done

ⒸNachum Danzig 2021


Using tests with if
For strings is, “Not equal” is != “Equal” is single =.
Spaces before and after [ are required. Space before ] and after ; is also required.
#!/bin/bash
for i in $(ls mydirectory); do
if [ $i != klum1 ]; then
echo $i
fi
done
Output:
klum2
klum3

ⒸNachum Danzig 2021


When to use [ ] in an if
if [ "$name" = 'Bob' ]; then … vs. if grep -q "$text" $file ; then …

The square brackets are a synonym for the test command. An if statement checks
the exit status of a command in order to decide which branch to take. grep -q
"$text" is a command, but "$name" = 'Bob' is not--it's just an expression. test is a
command, which takes an expression and evaluates it:

if test "$name" = 'Bob'; then ...

Since square brackets are a synonym for the test command, you can then rewrite
it as your original statement:

if [ "$name" = 'Bob' ]; then ...


ⒸNachum Danzig 2021
More testing tools, or
You can test if one of two conditions is met with -o “or”
Example:
for i in `ls klum`; do
if [ $i = klum1 -o $i = klum2 ]; then
echo $i
else
echo abc$i
fi
done

ⒸNachum Danzig 2021


Manual for test
( EXPRESSION ) EXPRESSION is true
! EXPRESSION EXPRESSION is false
EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true
-n STRING the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING the length of STRING is zero
STRING1 = STRING2 the strings are equal
STRING1 != STRING2 the strings are not equal
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to
INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2 FILE1 and FILE2 have the same
device and inode numbers ⒸNachum Danzig 2021
More tests
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is grantedⒸNachum Danzig 2021
Two file testing examples
Example 1: #!/bin/bash
for i in `ls`; do
if [ -d $i ]; then #if file is a directory
echo $i
fi
done
Example 2: #!/bin/bash
for i in `ls mydirectory`; do
if [ ! -r mydirectory/$i ]; then #if not readable
echo $i
fi
done ⒸNachum Danzig 2021
Getting command line parameters
Let’s say you run a script with 4 parameters: myscript 1 2 3 4

echo $0 #program name. myscript


echo $1 #first parameter, 1
echo $2 #second parameter, 2
echo $# #the number of parameters ,4
echo $* #the entire parameter list (not including program name), 1 2 3 4
echo $@ #same as above
echo $$ #process number, PID

ⒸNachum Danzig 2021


Sample use
#!/bin/bash
while [ $# -ne 0 ]; do #make sure not zero arguments
/usr/bin/echo -n "$# " # echo the current number of arguments
# -n do not output the trailing newline
echo $1 # echo the current 1st argument
shift # shift off one argument, like left shift
done
#at this point print $1 would result in a blank line

Shift 4 would have removed 4 parameters at once


Also possible to write for i in $* ;do echo $i done
ⒸNachum Danzig 2021
math operations
z=`expr $z + 3` # The 'expr' command performs the expansion.

z=$(($z+3))

z=$((z+3))

let z=z+3

let "z += 3" # Quotes permit the use of spaces in variable assignment

(( n += 1 )) # Increment.

(( n=$n+1 )) # (( $n += 1 )) is incorrect! lvalue must not have $


ⒸNachum Danzig 2021
Input output in Bash (Built-in Commands)
We saw you can create output with the echo command. Bash has another built-in
command for doing output, printf. (More on built-in commands in Bash can be
found at https://www.computerhope.com/unix/bash/ )

printf is more versatile than echo. It allows you to format the printing.

To read a single line from the standard input use the built-in shell command read.

example 1: read a
printf $a
example 2: read a
printf “you typed %s \n” $a
ⒸNachum Danzig 2021
Read Examples
while read; do echo "$REPLY"; done

Read and print in a loop. Because no variable was specified, the entire line of text
is stored in the special variable REPLY. This loop continues until you press Ctrl+D
(EOF) on a new line.

Equivalent to the above: while read text; do echo "$text"; done

You can also read directly into an array*, and then print each element in a loop:

read -a my_array
for i in "${my_array[@]}"; do echo "$i"; done
*We will learn about arrays in Bash later in this presentation
ⒸNachum Danzig 2021
Delineating the variable
I can use a variable in a string and bash will know it is a variable because it starts
with a $.
example: var=Mr
echo hello $var Danzig
But what if I want the variable to be adjacent to the string. How will bash know
where the variable name ends and the string begins?
Bad example: var=Mr.
echo hello $varDanzig
Solution: Use {} to delineate your variable.
Good example: var=Mr.
echo hello ${var}Danzig
ⒸNachum Danzig 2021
Specifying elements in a Range
We saw previously that we can specify a range using braces. You can also use
braces to specify specific elements in a range .

echo {1..10} #range , will print 1 2 3 4 5 6 7 8 9 10

echo {w,e,r} #elements, will print w e r

Practical examples:

echo hello{a,b} #will print helloa hellob

echo $1{,1,2,3} #assuming $1 is “go” will print go go1 go2 go3

ⒸNachum Danzig 2021


Environment Variable
Special variables defined by each user’s login scripts. There is no limit as to how
many are created at login. By convention they are all caps.
Example: echo $HOME
echo $PATH
echo $USER
output:
/home/ACAD/danzig/UNIX/jctstaff/danzig
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
danzig

ⒸNachum Danzig 2021


Defining undefined variables
#!/bin/bash
echo $x #will print a blank line because x is undefined.
echo ${x:-a} #meaning: if x is undefined, then a. Will print a.
echo $x #Will print blank line because the above did not define x.
echo ${x:=a} #if x is undefined, define it as a. Will print a.
echo $x #will print a.
echo ${x:+b} #if x is defined, then b. Will print b.
x= #undefine x
echo ${x:+b} #will print blank line
x=apple
echo ${x:2:2} #will print pl, i.e. from index 2 print 2 characters
echo ${x:1} #will print pple ⒸNachum Danzig 2021
Variable Indirection
If I create two variables, where the value of one variable happens to be the name
of some other variable, I can use that value as a variable name. This is called
indirection.
> x=apple
> apple=pear
> echo ${x} # prints as normal, apple
apple
> echo ${!x} # indirection, use the value of x , i.e. apple,
as my variable name
pear
As if I wrote echo ${apple}
ⒸNachum Danzig 2021
Script to print all files containing the string “fork”
#!/bin/bash
for i in `ls * `; do
if [ -f $i ] ;then
if ( grep -q "fork" $i ) ; then
echo $i
fi
fi
done

ⒸNachum Danzig 2021

You might also like