0% found this document useful (0 votes)
44 views

Section 7

The document provides instructions on creating and running bash shell scripts. It discusses: 1) Creating a new file with a .sh extension and adding the shebang line #!/bin/bash. 2) Writing commands and variables in the script file. 3) Running the script by executing bash followed by the script name or making it executable and running it directly.

Uploaded by

pspc139
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)
44 views

Section 7

The document provides instructions on creating and running bash shell scripts. It discusses: 1) Creating a new file with a .sh extension and adding the shebang line #!/bin/bash. 2) Writing commands and variables in the script file. 3) Running the script by executing bash followed by the script name or making it executable and running it directly.

Uploaded by

pspc139
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/ 13

Section ​#7

1​ ​Bash Shell ​Scripting


Using the bash shell, variables, and commands can be only written ​one at a
time​.In order to get over this limitation, we use a ​script​.

Script​: A file containing multiple commands, variables,... etc.

Before we start we need to install a


Text Editor called ( ​nano​ )

log in as a “ ​root​ ” THEN


[root@localhost /home]# yum install nano

Steps of creating and running a bash shell script


1. Create a new file ending with the extension (.sh)

[ahmad@localhost /home]$ ​touch​ first_script.sh

2. Open your file using an editor of your choice ( here we’re using ​nano​ )

[ahmad@localhost /home]$ ​nano​ first_script.sh

3. Add ( ​#!/bin/bash​ ) to the first line of your file.

4. Move to the next line and add some commands.

#!/bin/bash

file_name=”Ahmad”

touch ​$​file_name
Save →​ ctrl + o
Exit → ​ctrl + x

5. Run ( ​bash first_script.sh​ ), assuming the script name is


“first_script.sh”

6. To make sure everything worked just fine .. run

[ahmad@localhost /home]$ ​ls

7. Another way to run your script is to add an ​execute permission ​to the
script and then running it as follows ( ​./first_script.sh​ )

[ahmad@localhost /home]$ ​chmod​ ​u+x​ first_script.sh

[ahmad@localhost /home]$ ​./​first_script.sh

bashrc​ file
a bash script that is used to hold your
variables, and functions ​permanently

System wide version → ​/etc/bashrc

Runs each time you login with any username

User specific version → ​/home/ahmad/.bashrc


{{ replace ​ahmad​ with your username }}

Runs each time you create a new session

==> Adding a welcome message to the user Ahmad

[ahmad@localhost /]$ ​cd ~


[ahmad@localhost /home]$ ​nano ​.bashrc

Move to the last line of the file


# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't ..
# export SYSTEMD_PAGER=
# User specific aliases and functions

# Add the following Line


echo “ Hello ”$USER

save ( ctrl + O ) THEN exit ( ctrl + x )

logout and log in again you’ll see a welcoming message “ Hello Ahmad ”.

Assignments
1. ​Write a shell script that ​reads​ some input from
the user and prints it back to the user again.

#!/bin/sh

echo “Enter your name: ”

read Name

echo “You entered”$Name

# Notice we didn’t need + sign to concatenate

2. ​edit​ the “/etc/bashrc” file to ​add a welcoming


message​ and try multiple users to login and make
sure the message is displayed to everyone
2​ ​If ​Statement

→ general form

if​ ​[​ ​expression​ ​]​;​ ​then

echo “​True​ side of the if statement”


# You can add as many instructions as
# needed

else # else is ​OPTIONAL

echo “​False​ side of the if statement”


# You can add as many instructions as
# needed

fi

→ Other forms

if​ ​[​ ​expression​ ​] ​# remove ​Semicolon


​then # move ​then t​ o new
line
echo “​True​ side of the if statement”
else
echo “​False​ side of the if statement”
fi
if​ ​[[​ ​expression​ ​]] # ​ Double ​Brackets
​then
echo “​True​ side of the if statement”
else
echo “​False​ side of the if statement”
fi

Double Bracket is called ​EXTENDED brackets


it has more features than single brackets
for example .. it can be used for testing ​Regular
expressions​ ( more on this later )

--------------------------------------------------
if you want to compare ​numbers​ using ​normal
comparison symbols​ ( > , < , == )
and NOT stuff like ( gt , lt , eq )

> → g​reater ​t​han → -gt

< → l​ess ​t​han → -lt

== → eq​ual → -eq

if​ ​((​ ​expression​ ​)) #​ ​Integer ​Comparison


​then
echo “​True​ side of the if statement”
else
echo “​False​ side of the if statement”
fi

Example ​#1
x=13

if​ ​[​ ​$​x​ ​-lt​ ​50 ​] ​# ​single ​Brackets


​then
echo “$x​ ​is less than 50”
else
echo “$x​ ​is NOT less than 50”
fi

-eq # ​eq​ual
-ne # ​n​ot ​e​qual
-lt # ​l​ess ​t​han
-le # ​l​ess than or ​e​qual
-gt # ​g​reater t​ ​han
-ge # ​g​reater than or ​e​qual

x=13

if​ ​[[​ ​$​x​ ​-lt​ ​50 ​]] ​# ​double ​Brackets


​then
echo “$x​ ​is less than 50”
else
echo “$x​ ​is NOT less than 50”
fi

x=13

if​ ​((​ ​$​x​ ​<​ ​50 ​)) ​# ​integer ​comparison


​then
echo “$x​ ​is less than 50”
else
echo “$x​ ​is NOT less than 50”
fi

Example ​#2
in fact we can use the ​ <​ ​
symbol inside the
double brackets​ notation .. but it ​won’t​ give us the
expected results.

x=13

if​ ​[[​ ​$​x​ ​<​ ​50 ​]]


​then
echo “$x​ ​is less than 50”
else
echo “$x​ ​is NOT less than 50”
fi

this will print “ ​13 is less than 50​ ” as expected


But what if we ​change​ it to something like this

x=​100

if​ ​[[​ ​$​x​ ​<​ ​20​ ​]]


​then
echo “$x​ ​is less than 20”
else
echo “$x​ ​is NOT less than 20”
fi

this will ​ALSO​ print “ 1​ 3 is less than 50​ ”

Apparently .. this is wrong.

in fact it considers the two numbers to be


STRINGS
and compares them “ character by character ”
that’s why it considers “ ​1​00 “ is less than “ ​2​0 “

Example ​#3 ( ​Strings​ )


if​ ​[[​ “cat”​ ​==​ ​“cat” ]​ ]
​then
echo “True”
else
echo “False”
fi
I can use “ ​==​ “ OR “ ​-eq ​“ with ​double​ brackets

if​ ​[​ “cat”​ ​==​ ​“cat” ​] # RIGHT


​then
echo “True”
else
echo “False”
fi
I can use only “ ​==​ “ with ​single​ brackets

if I try

if​ ​[​ “cat”​ ​-eq​ ​“cat” ]​ # WRONG


​then
echo “True”
else
echo “False”
fi

it won’t work.

In General
inside the single and double square brackets:
→ use (​ ​==​, ​<​, ​>​ ) for comparing ​Strings
→ use ( ​-eq​, ​-lt​, ​-gt​, ... ) for comparing
Numbers

3​ ​while ​Loop

has the same syntax used in an ​if​ statement.

i=0
while​ ​[[​ $i ​-lt ​10 ​]]
​do
echo “i now is ”$i
(( i++ ))
done

another form with a ​semicolon​.

i=0
while​ ​[[​ $i ​-lt ​10 ​]] ;​ ​do
echo “i now is ”$i
(( i++ ))
done
3​ ​until ​Loop

the ​opposite​ of the while loop .. it keeps running


as long as the condition is ​FALSE​.

i=0
until​ ​[[​ $i ​-​g​t ​10 ​]] ;​ ​do
echo “i now is ”$i
(( i++ ))
done

4​ f​ or ​Loop
a for loop ​iterates​ through a list of items.
this list could be in any form, for example:

# a list of items separated by a ​space


→ 1 2 3 4 5 6 7 8 9 10

# the brace expansion we used before


# the next generates the following sequence
# 1, 2, 3, 4, 5, 6, 7, 8
→ {1..8}

# we can also specify an ​interval ​( each step


# is by 2 not 1 )
# the next generates the following sequence
# 1, 3, 5, 7, 9
→ {1..10​..2​}

for​ i ​in​ 1 2 3
do
echo “i now is ”$i
done
prints:
i now is 1
i now is 2
i now is 3

for​ i ​in​ {1..3}


do
echo “i now is ”$i
done
prints:
i now is 1
i now is 2
i now is 3

for​ i ​in​ {1..8..3}


do
echo “i now is ”$i
done
prints:
i now is 1
i now is 4
i now is 7

For loops can ​also ​iterate through the ​OUTPUT​ of


another command:

for​ i ​in​ ​$( ​ls​ )


do
echo “current file is: ”$i
done

You might also like