[Go to site: main page, start]

0% found this document useful (0 votes)
13 views114 pages

Shell Scripting

Shell scripting is a text file containing a sequence of commands for UNIX-based systems, optimized for shell-related tasks. Bash scripting, a specific type of shell scripting, allows users to write scripts for the bash shell, utilizing features like variables, control flow, and command-line arguments. The document also covers basic script creation, execution, and the use of functions in bash scripting.

Uploaded by

cristianosukumar
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)
13 views114 pages

Shell Scripting

Shell scripting is a text file containing a sequence of commands for UNIX-based systems, optimized for shell-related tasks. Bash scripting, a specific type of shell scripting, allows users to write scripts for the bash shell, utilizing features like variables, control flow, and command-line arguments. The document also covers basic script creation, execution, and the use of functions in bash scripting.

Uploaded by

cristianosukumar
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

Shell S c r i p t i n g

1
W h a t i s Shell S c r i pt ing?

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.

2
W h a t i s Shell S c r i pt ing?

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.

3
Basics of Bash Scripting

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

4
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 $$

6
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.

6
Your V e r y F i r s t S c r i p t

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

7
Your V e r y F i r s t S c r i p t

Here is a super simple bash script called [Link]:

#!/usr/bin/env bash

echo "Hello world!"

8
Your V e r y F i r s t S c r i p t

Here is a super simple bash script called [Link]:

#!/usr/bin/env bash

echo "Hello world!"

The shebang is the very


first line of a script

9
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.

10
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

11
Running (Your V e r y F i r s t S c r i pt)

You can always run a shell script by simply prepending it with a shell
interpreter program:

sh [Link] bash [Link] zsh [Link]

12
Running (Your V e r y F i r s t S c r i pt)

You can always run a shell script by simply prepending it with a shell
interpreter program:

sh [Link] bash [Link] zsh [Link]

Interpreter

13
Running (Your V e r y F i r s t S c r i pt)

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

14
Running (Your V e r y F i r s t S c r i pt)

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 [Link]

16
Running (Your V e r y F i r s t S c r i pt)

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 [Link]

Makes the program executable

16
Running (Your V e r y F i r s t S c r i pt)

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 [Link]

Then run the program:


./[Link]

17
B a s h S c r i p t i n g : Variables

To assign variables, use the following:


x=foo

18
B a s h S c r i p t i n g : Variables

To assign variables, use the following:


x=foo

You can access the value of x using the following:


$x

19
B a s h S c r i p t i n g : 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.

20
B a s h S c r i p t i n g : S t r i n gs

Next, we can define strings.

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


'$x'

21
B a s h S c r i p t i n g : S t r i n gs

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"

22
B a s h S c r i p t i n g : S t r i n gs

Here's the difference in behavior:

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

23
Your V e r y F i r s t S c r i p t

Let's use a variable in [Link]:

#!/usr/bin/env bash

greeting="Hello world!"
echo $greeting

24
Bash Scripting: Control Flow

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

26
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

26
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

27
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

28
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
29
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
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

while [ CONDITION ]
do
# do something
done

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=0
while [ $num -lt 100 ]
do
echo $num
num=$((num+1))
done
32
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

33
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
34
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.

36
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

36
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.

37
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.

38
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

39
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

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.
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

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.
The variable $0 refers to the name of the script.

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

This is $0 This is $1

42
Bash Scripting: Arguments

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

43
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

44
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

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

#!/usr/bin/env bash

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

46
Bash Scripting: Functions

We can also define functions!

47
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

48
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

49
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

50
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

51
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

make_and_enter new_folder
52
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

make_and_enter new_folder
53
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/usr/bin/env bash

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

make_and_enter new_folder
54
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link]

#!/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 [Link] my_folder

#!/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 [Link] my_folder

#!/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 [Link] my_folder

#!/usr/bin/env bash

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

make_and_enter $1
58
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link] my_folder

#!/usr/bin/env bash

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

make_and_enter $1
59
Bash Scripting: Functions

We can also define functions!


adrazen@ayelet-computer ~ % sh [Link] my_folder

#!/usr/bin/env bash

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

make_and_enter $1
60
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. [Link]). 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. [Link]).

For my name, my function would create a folder named ayelet and a file
named [Link] inside of ayelet.

61
Bash Scripting: Exercise

adrazen@ayelet-computer ~ % my_folder.sh ayelet [Link]

#!/usr/bin/env bash

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

make_my_folder $1 $2
62
B a s h S c r i p t i n g : R e t u r n Values

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

63
B a s h S c r i p t i n g : R e t u r n 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.

64
B a s h S c r i p t i n g : R e t u r n 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.

66
B a s h S c r i p t i n g : R e t u r n 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

66
B a s h S c r i p t i n g : R e t u r n Values

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

67
B a s h S c r i p t i n g : R e t u r n 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.

68
B a s h S c r i p t i n g : R e t u r n 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
B a s h S c r i p t i n g : R e t u r n Values

Return values are useful if you want to conditionally execute commands


based on the execution of the previous command.

70
B a s h S c r i p t i n g : R e t u r n 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 || .

71
B a s h S c r i p t i n g : R e t u r n 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!"

72
B a s h S c r i p t i n g : R e t u r n 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

73
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!).
74
Bash Scripting: Exercise

#!/usr/bin/env bash

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

76
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

76
Bash Scripting: Exercise

#!/usr/bin/env bash

cat $1 && echo "File exists!"


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

77
Bash Scripting: Exercise

#!/usr/bin/env bash

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


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

78
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.

79
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
80
Bash Scripting: Extra Syntax

Bash scripting has some specific syntax that is worth calling out.

If you're ever stuck, look something up

81
B a s h S c r i p t i n g : [ vs [ [

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

82
B a s h S c r i p t i n g : [ 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

83
B a s h S c r i p t i n g : [ 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?

84
B a s h S c r i p t i n g : [ vs [ [

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

if [ condition ] if [[ condition ]]

86
B a s h S c r i p t i n g : [ 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

60
B a s h S c r i p t i n g : [ 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

61
B a s h S c r i p t i n g : [ 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

62
B a s h S c r i p t i n g : [ 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

63
B a s h S c r i p t i n g : [ 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

64
B a s h S c r i p t i n g : [ 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

65
B a s h S c r i p t i n g : [ 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

66
B a s h S c r i p t i n g : [ vs [ [

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

if [ condition ] if [[ condition ]]

67
B a s h S c r i p t i n g : [ 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.)

68
B a s h S c r i p t i n g : [ 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 ||.

66
B a s h S c r i p t i n g : C o m p ari s on

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


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

96
B a s h S c r i p t i n g : C o m p ari s on

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

97
B a s h S c r i p t i n g : C o m p ari s on

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

98
B a s h S c r i p t i n g : C o m p ari s on

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

99
B a s h S c r i p t i n g : C o m p ari s on

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

100
B a s h S c r i p t i n g : C o m p ari s on

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

101
B a s h S c r i p t i n g : C o m p ari s on

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


s1 = s2 for checking if s1 is equal to s2

102
B a s h S c r i p t i n g : C o m p ari s on

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

103
B a s h S c r i p t i n g : C o m p ari s on

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

104
B a s h S c r i p t i n g : C o m p ari s on

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

106
B a s h S c r i p t i n g : C o m p ari s on

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

106
B a s h S c r i p t i n g : C o m p ari s on

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

107
Bash Scripting: Arithmetic

To do arithmetic, we need to follow bash syntax.

108
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))

109
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

110
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 )

111
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!

112
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
A d va n c e d 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

You might also like