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

0% found this document useful (0 votes)
12 views126 pages

4 Shell Scripting

We've seen how to execute commands in the shell and pipe multiple commands together. Sometimes, we want to run many, many commands together and/or make use of control flow expressions such as conditionals and loops.

Uploaded by

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

4 Shell Scripting

We've seen how to execute commands in the shell and pipe multiple commands together. Sometimes, we want to run many, many commands together and/or make use of control flow expressions such as conditionals and loops.

Uploaded by

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

CS 45, Lecture 4

Shell Scripting
Spring 2023
Akshay Srivatsan, Ayelet Drazen, Jonathan Kula

1
Administrivia

● Assignment 0 is due today. Reach out if you don't think you will be able
to get it done in time.
● Assignment 1 is out! It is due a week from today on Wednesday, April
19th at 11:59 PM.

2
What we know

In the last lecture, we learned how to:


- Use shell commands to manipulate and analyze data
- Write regular expressions
- Run more complex shell commands such as grep, sort, uniq

3
What we will learn today

In today's lecture, we will learn how to:


- Write shell scripts

4
What is Shell Scripting?

We've seen how to execute commands in the shell and pipe multiple
commands together.
Sometimes, we want to run many, many commands together and/or make
use of control flow expressions such as conditionals and loops.
That's where shell scripting comes in.

5
What is Shell Scripting?

A shell script is a text file that


contains a sequence of commands
for a UNIX-based operating
system.
It is called a script because it
combines a sequence of
commands—that would otherwise
have to be typed into a keyboard
one at a time—into a single script.
6
What is Shell Scripting?

Most shells have their own scripting language, each with its own
variables, control flow, and syntax.
What makes shell scripting different from other scripting languages is
that it is optimized for performing shell-related tasks.
Creating command pipelines, saving results into files, and reading from
standard input are baked into in shell scripting, making it easier to use
compared to other scripting languages.

7
Basics of Bash Scripting

Bash scripting refers to writing a script for a bash shell (Bourne Again
SHell).

8
Basics of Bash Scripting

Bash scripting refers to writing a script for a bash shell (Bourne Again
SHell).
You can check what shell you are using by running ps -p $$

9
Basics of Bash Scripting

Bash scripting refers to writing a script for a bash shell (Bourne Again
SHell).
You can check what shell you are using by running ps -p $$
If you are on Linux, your default shell should be a bash shell. If you are on
macOS or Windows, your shell may be different but this shouldn't cause
an issue given that your shell will still know how to "speak" bash.

10
Your Very First Script

Let's write a super simple shell script that says hello!

11
Your Very First Script

Here is a super simple bash script called hello.sh:

#!/usr/bin/env bash

echo "Hello world!"

12
Your Very First Script

Here is a super simple bash script called hello.sh:

#!/usr/bin/env bash

echo "Hello world!"

The shebang is the very


first line of a script

13
Shebang

The shebang, also called a sharp exclamation, is the very first line of a
script.
It is the combination of the pound symbol (#) and an exclamation mark
(!).
The shebang is used to specify the interpreter that the given script will
be run with. In our case, we indicate that we want a bash interpreter (i.e.
a bash shell). If you want to run your script with a zsh shell, you simply
change the shebang.

14
Shebang

A note about shebangs:


There are a number of different ways to write your shebang such as
#!/usr/bin/env bash and #!/bin/bash
We recommend that you always use the former as it increases the
portability of your script. The env command tells the system to resolve
the bash command wherever it lives in the system, as opposed to just
looking inside of /bin

15
Running (Your Very First Script)

You can always run a shell script by simply prepending it with a shell
interpreter program:
sh hello.sh bash hello.sh zsh hello.sh

16
Running (Your Very First Script)

You can always run a shell script by simply prepending it with a shell
interpreter program:
sh hello.sh bash hello.sh zsh hello.sh

Interpreter

17
Running (Your Very First Script)

You can also run a script by turning it into an executable program and
then running it.

18
Running (Your Very First Script)

You can also run a script by turning it into an executable program and
then running it.
First, turn the program into an executable using chmod (change mode):
chmod +x hello.sh

19
Running (Your Very First Script)

You can also run a script by turning it into an executable program and
then running it.
First, turn the program into an executable using chmod (change mode):
chmod +x hello.sh

Makes the program executable

20
Running (Your Very First Script)

You can also run a script by turning it into an executable program and
then running it.
First, turn the program into an executable using chmod (change mode):
chmod +x hello.sh

Then run the program:


./hello.sh

21
Bash Scripting: Variables

To assign variables, use the following:


x=foo

22
Bash Scripting: Variables

To assign variables, use the following:


x=foo

You can access the value of x using the following:


$x

23
Bash Scripting: Variables

To assign variables, use the following:


x=foo

You can access the value of x using the following:


$x

Note: you cannot use x = foo (with spaces) because it is


interpreted as trying to run a program x with two arguments: = and foo.

24
Bash Scripting: Strings

Next, we can define strings.


If we want to define a string literal, we will use single quotes:
'$x'

25
Bash Scripting: Strings

Next, we can define strings.


If we want to define a string literal, we will use single quotes:
'$x'
If we want to define a string that allows substitution, we will use double
quotes:
"$x"

26
Bash Scripting: Strings

Here's the difference in behavior:

x=foo x=foo
echo '$x' echo "$x"
# prints $x # prints foo

27
Your Very First Script

Let's use a variable in hello.sh:

#!/usr/bin/env bash

greeting="Hello world!"
echo $greeting

28
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

29
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

if [ CONDITION ]
then
# do something
fi

30
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

num=101
if [ $num -gt 100 ]
then
echo "That's a big number!"
fi

31
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

num=101
if [ $num -gt 100 ] && [ $num -lt 1000 ]
then
echo "That's a big (but not a too big) number!"
fi

32
Bash Scripting: Control Flow

#!/usr/bin/env bash

if [ CONDITION ]
then
# do something
elif [ CONDITION ]
then
# do something else
else
# do something totally different
fi
33
Bash Scripting: Control Flow

#!/usr/bin/env bash

num=101
if [ $num -gt 1000 ]
then
echo "That's a huge number!"
elif [ $num -gt 100 ]
then
echo "That's a big number!"
else
echo "That's a small number."
fi
34
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

while [ CONDITION ]
do
# do something
done

35
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

num=0
while [ $num -lt 100 ]
do
echo $num
num=$((num+1))
done
36
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

for VARIABLE in {1..N}


do
# do something
done

37
Bash Scripting: Control Flow

Like other programming languages, bash scripts also have control flow
directives such as if, for, while, and case.

#!/usr/bin/env bash

num=0
for i in {1..100}
do
echo $num
num=$((num+1))
done
38
Bash Scripting: Exercise

Exercise 1: Write a shell script called num_loop.sh that loops through


every number 1 through 20 and prints each number to standard output.
The script should also conditionally print I'm big! for every number
larger than 10.

39
Bash Scripting: Exercise

#!/usr/bin/env bash

for i in {1..20}
do
echo $i
if [ $i -gt 10 ]
then
echo "I'm big!"
fi
done

40
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.

41
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.
In bash, the variables $1 - $9 refers to the arguments to a script.

42
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.
In bash, the variables $1 - $9 refers to the arguments to a script.

adrazen@ayelet-computer ~ % sh my_script.sh ayelet

43
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.
In bash, the variables $1 - $9 refers to the arguments to a script.

adrazen@ayelet-computer ~ % sh my_script.sh ayelet

This is $1

44
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.
In bash, the variables $1 - $9 refers to the arguments to a script.
The variable $0 refers to the name of the script.

adrazen@ayelet-computer ~ % sh my_script.sh ayelet

This is $1

45
Bash Scripting: Arguments

Let's take a look at how we might use command line arguments to make
our big_num.sh script a little more interesting.
In bash, the variables $1 - $9 refers to the arguments to a script.
The variable $0 refers to the name of the script.

adrazen@ayelet-computer ~ % sh my_script.sh ayelet

This is $0 This is $1

46
Bash Scripting: Arguments

Let's assign num to be the first argument when calling the script.
adrazen@ayelet-computer ~ % sh big_num.sh 102

47
Bash Scripting: Arguments

Let's assign num to be the first argument when calling the script.
adrazen@ayelet-computer ~ % sh big_num.sh 102

This is $1

48
Bash Scripting: Arguments

Let's assign num to be the first argument when calling the script.
adrazen@ayelet-computer ~ % sh big_num.sh 102

#!/usr/bin/env bash

num=101
if [ $num -gt 100 ]
then
echo "That's a big number!"
fi

49
Bash Scripting: Arguments

Let's assign num to be the first argument when calling the script.
adrazen@ayelet-computer ~ % sh big_num.sh 102

#!/usr/bin/env bash

num=$1
if [ $num -gt 100 ]
then
echo "That's a big number!"
fi

50
Bash Scripting: Functions

We can also define functions!

51
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
# calls mkdir (including parent directories)
# calls cd
}

52
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter(directory_name) {
mkdir -p directory_name
cd directory_name
}

53
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter(directory_name) {
mkdir -p directory_name
cd directory_name
}

54
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

55
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
56
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
57
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
58
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
59
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh my_folder

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
60
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh my_folder

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter new_folder
61
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh my_folder

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter $1
62
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh my_folder

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter $1
63
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh mcd.sh my_folder

#!/usr/bin/env bash

make_and_enter() {
mkdir -p "$1"
cd "$1"
}

make_and_enter $1
64
Bash Scripting: Exercise

Exercise 2: Write a shell script called my_folder.sh that takes in two


arguments: your name (e.g. ayelet) and your name with the .txt
ending (e.g. ayelet.txt). The script should call a function that creates
a folder by the name of the first argument (e.g. ayelet) and then create
a file inside by the name of the second argument (e.g. ayelet.txt).
For my name, my function would create a folder named ayelet and a file
named ayelet.txt inside of ayelet.

65
Bash Scripting: Exercise

adrazen@ayelet-computer ~ % my_folder.sh ayelet ayelet.txt

#!/usr/bin/env bash

make_my_folder() {
mkdir "$1"
cd "$1"
touch "$2"
}

make_my_folder $1 $2
66
Bash Scripting: Return Values

The notion of exit codes allows for verifying the success or failure of a
previous command.

67
Bash Scripting: Return Values

The notion of exit codes allows for verifying the success or failure of a
previous command.
An exit code or return value is the way scripts or commands can
communicate with each other about how execution went.

68
Bash Scripting: Return Values

The notion of exit codes allows for verifying the success or failure of a
previous command.
An exit code or return value is the way scripts or commands can
communicate with each other about how execution went.
A return value of 0 means that everything went OK. A return value other
than 0 means that an error occurred.

69
Bash Scripting: Return Values

The notion of exit codes allows for verifying the success or failure of a
previous command.
An exit code or return value is the way scripts or commands can
communicate with each other about how execution went.
A return value of 0 means that everything went OK. A return value other
than 0 means that an error occurred.
$? provides the return value from the most recently executed command

70
Bash Scripting: Return Values

If you ever need a placeholder for a command that succeeds or fails, you
can use the true and false commands.

71
Bash Scripting: Return Values

If you ever need a placeholder for a command that succeeds or fails, you
can use the true and false commands.
true is a command that does nothing except return an exit status of 0.
false is a command that does nothing except return an exit status of 1.

72
Bash Scripting: Return Values

adrazen@ayelet-computer ~ % sh success_or_failure.sh

#!/usr/bin/env bash

result=$(($RANDOM % 2))
if [ $result -eq 0 ]
then
true
echo "$?"
else
false
echo "$?"
fi 73
Bash Scripting: Return Values

Return values are useful if you want to conditionally execute commands


based on the execution of the previous command.

74
Bash Scripting: Return Values

Return values are useful if you want to conditionally execute commands


based on the execution of the previous command.
In addition to using if-statements, we can also conditionally execute
commands using && and || .

75
Bash Scripting: Return Values

Return values are useful if you want to conditionally execute commands


based on the execution of the previous command.
In addition to using if-statements, we can also conditionally execute
commands using && and || .
true && echo "Print if things went well!"
# prints "Print if things went well!"

76
Bash Scripting: Return Values

Return values are useful if you want to conditionally execute commands


based on the execution of the previous command.
In addition to using if-statements, we can also conditionally execute
commands using && and || .
true && echo "Print if things went well!"
# prints "Print if things went well!"

false && echo "Print if things went well!"


# no output

77
Bash Scripting: Exercise

Exercise 3: Write a shell script called file_checker.sh that checks if


a file exists or not. The script take in a file name as an argument and try
to run cat on that file. The script should then check the exit code of the
cat command to determine if the file exists or not. If the file exists, the
script should print File exists!. If the file does not exist, the script
should print File does not exist!.
Bonus: change the script to suppress the actual output of cat and only
include your script's output (e.g. File exists! or File does not
exist!).
78
Bash Scripting: Exercise

#!/usr/bin/env bash

cat $1
if [ $? -eq 0 ]
then
echo "File exists!"
else
echo "File does not exist!"
fi

79
Bash Scripting: Exercise

#!/usr/bin/env bash

cat $1 &> /dev/null


if [ $? -eq 0 ]
then
echo "File exists!"
else
echo "File does not exist!"
fi

80
Bash Scripting: Exercise

#!/usr/bin/env bash

cat $1 && echo "File exists!"


cat $1 || echo "File does not exist!"

81
Bash Scripting: Exercise

#!/usr/bin/env bash

cat $1 &> /dev/null && echo "File exists!"


cat $1 &> /dev/null || echo "File does not exist!"

82
Bash Scripting: Command Substitution

Command substitution is another useful feature of bash scripting.


You might want to run a command and then use its output as a variable
to some other piece of code.

83
Bash Scripting: Command Substitution

Command substitution is another useful feature of bash scripting.


You might want to run a command and then use its output as a variable
to some other piece of code.
Example:
#!/usr/bin/env bash

for element in $(ls ~/Desktop)


do
echo "Desktop contains file named $element"
done
84
Bash Scripting: Extra Syntax

Bash scripting has some specific syntax that is worth calling out.
If you're ever stuck, look something up 😁

85
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

86
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]
then then
# do something # do something
fi fi

87
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]
then then
# do something # do something
fi fi

What's the difference?

88
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]

89
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]

Single brackets are a


reference to the the test
command

90
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]

Single brackets are a Double brackets are bash


reference to the the test specific. (Also works for zsh)
command

91
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ 1 < 2 ] if [[ 1 < 2 ]]
then then
echo "Correct!" echo "Correct!"
fi fi

92
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ 1 < 2 ] if [[ 1 < 2 ]]
then then
echo "Correct!" echo "Correct!"
fi fi

93
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ 1 < 2 ] if [[ 1 < 2 ]]
then then
echo "Correct!" echo "Correct!"
fi fi

2: No such file or
directory

94
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ 1 < 2 ] if [[ 1 < 2 ]]
then then
echo "Correct!" echo "Correct!"
fi fi

2: No such file or
directory

95
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ 1 < 2 ] if [[ 1 < 2 ]]
then then
echo "Correct!" echo "Correct!"
fi fi

2: No such file or Correct!


directory

96
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]

97
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]
In general, single brackets are recognized by more scripting languages and
are POSIX compliant. (Won't work with sh interpreter unless linked to bash.)

98
Bash Scripting: [ vs [[

When you have an if-statement, you need to encapsulate the condition. You
can do this in two ways:

if [ condition ] if [[ condition ]]
In general, single brackets are recognized by more scripting languages and
are POSIX compliant. (Won't work with sh interpreter unless linked to bash.)
Double brackets are less portable, but they align with what you would expect
from high level coding languages. You can use comparison operators such
as < or > and logical operators such as && or ||.

99
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b

100
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b
a -ne b for checking if a is not equal to b

101
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b
a -ne b for checking if a is not equal to b
a -gt b for checking if a is greater than b

102
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b
a -ne b for checking if a is not equal to b
a -gt b for checking if a is greater than b
a -ge b for checking if a is greater than or equal to b

103
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b
a -ne b for checking if a is not equal to b
a -gt b for checking if a is greater than b
a -ge b for checking if a is greater than or equal to b
a -lt b for checking if a is less than b

104
Bash Scripting: Comparison

In order to compare numbers in a bash script, use the following:


a -eq b for checking if a is equal to b
a -ne b for checking if a is not equal to b
a -gt b for checking if a is greater than b
a -ge b for checking if a is greater than or equal to b
a -lt b for checking if a is less than b
a -le b for checking if a is less than or equal to b

105
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2

106
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2
s1 != s2 for checking if s1 is not equal to s2

107
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2
s1 != s2 for checking if s1 is not equal to s2
s1 < s2 for checking if s1 is less than s2 by lexicographical order

108
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2
s1 != s2 for checking if s1 is not equal to s2
s1 < s2 for checking if s1 is less than s2 by lexicographical order
s1 > s2 for checking if s1 is greater than to s2 by lexicographical order

109
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2
s1 != s2 for checking if s1 is not equal to s2
s1 < s2 for checking if s1 is less than s2 by lexicographical order
s1 > s2 for checking if s1 is greater than to s2 by lexicographical order
-n s1 for checking if s1 has a length greater than 0

110
Bash Scripting: Comparison

In order to compare strings in a bash script, use the following:


s1 = s2 for checking if s1 is equal to s2
s1 != s2 for checking if s1 is not equal to s2
s1 < s2 for checking if s1 is less than s2 by lexicographical order
s1 > s2 for checking if s1 is greater than to s2 by lexicographical order
-n s1 for checking if s1 has a length greater than 0
-z s1 for checking if s1 has a length of 0

111
Bash Scripting: Arithmetic

To do arithmetic, we need to follow bash syntax.

112
Bash Scripting: Arithmetic

To do arithmetic, we need to follow bash syntax.


To add two numbers 1 and 2, and then assign to a variable a:
a=$((1+2))

113
Bash Scripting: Arithmetic

To do arithmetic, we need to follow bash syntax.


To add two numbers 1 and 2, and then assign to a variable a:
a=$((1+2))

You can also use the let keyword:


let a=1+2

114
Bash Scripting: Arithmetic

To do arithmetic, we need to follow bash syntax.


To add two numbers 1 and 2, and then assign to a variable a:
a=$((1+2))

You can also use the let keyword:


let a=1+2

You can use the expr keyword:


a=$( expr 1 + 2 )

115
Bash Scripting: Exercise

Exercise 4: Write a shell script called timely_greeting.sh that greets


you based on the current time. The script should call the date
command, extract the current hour (look into using %H) and then print
the following greeting based on the time.
If it is between 5AM (05:00) and 12PM (12:00): Good morning!
If it is between 12PM (12:00) and 6PM (18:00): Good afternoon!
If it is between 6PM (18:00) and 5AM (5:00): Good night!

116
Bash Scripting: Exercise

#!/usr/bin/env bash

time=$(date +%H)
if [ $time -gt 5 ] && [ $time -lt 12 ]
then
echo "Good morning!"
elif [ $time -gt 12 ] && [ $time -lt 18 ]
then
echo "Good evening!"
elif [ $time -gt 18 ] && [ $time -lt 5 ]
then
echo "Good night!"
fi 117
Advanced Running

You can turn your shell script into a "command" by moving it to ~/bin.
For example if you have a script called hello, you could do the
following:
adrazen@ayelet-computer ~ % mv hello ~/bin/

You can then run the command by just calling hello:


adrazen@ayelet-computer ~ % hello

Note: this probably won't work yet on your computer but we will learn
about it in a later lecture.
118
Spot the Problem!

In preparation for this Assignment 1 and Assignment 2, let's go through some


common mistakes and odd behavior.

119
Spot It!

If we run uniq on the file on the left, will the output look like what's on the right?
INPUT OUTPUT
apple apple
banana banana
apple orange
orange kiwi
orange strawberry
kiwi
orange
strawberry
strawberry
apple
120
Spot It!

If we run uniq on the file on the left, will the output look like what's on the right?
INPUT OUTPUT
apple apple


banana banana
apple orange
orange kiwi
orange orange
kiwi strawberry
orange apple
strawberry
strawberry
apple
121
Spot It!

If we run uniq on the file on the left, will the output look like what's on the right?
INPUT OUTPUT
apple apple


apple banana
apple orange
banana kiwi
orange strawberry
orange
orange
kiwi
strawberry
strawberry
122
Spot It!

Will the following command work to replace all instances of the string world
with the string wOrLd?
cat worlds.txt | sed 's/world/wOrLd/'

worlds.txt
Hello world
It's a small world
The world is my oyster
On top of the world
A world away
Do someone a world of good
123
Spot It!

Will the following command work to replace all instances of the string world
with the string wOrLd? → No.
cat worlds.txt | sed 's/world/wOrLd/g'

worlds.txt
Hello world
It's a small world
The world is my oyster
On top of the world
A world away
Do someone a world of good
124
Spot It!

What's wrong with the following snippet of bash code?


#!/usr/bin/env bash

num=101
if [$num -gt 100]
then
echo "That's a big number!"
fi

125
Spot It!

What's wrong with the following snippet of bash code?


#!/usr/bin/env bash

num=101
if [ $num -gt 100 ]
then
echo "That's a big number!"
fi

126

You might also like