Section 7
Section 7
2. Open your file using an editor of your choice ( here we’re using nano )
#!/bin/bash
file_name=”Ahmad”
touch $file_name
Save → ctrl + o
Exit → ctrl + x
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 )
bashrc file
a bash script that is used to hold your
variables, and functions permanently
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
read Name
→ general form
fi
→ Other forms
--------------------------------------------------
if you want to compare numbers using normal
comparison symbols ( > , < , == )
and NOT stuff like ( gt , lt , eq )
== → equal → -eq
Example #1
x=13
-eq # equal
-ne # not equal
-lt # less than
-le # less than or equal
-gt # greater t han
-ge # greater than or equal
x=13
x=13
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
x=100
if I try
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
i=0
while [[ $i -lt 10 ]]
do
echo “i now is ”$i
(( i++ ))
done
i=0
while [[ $i -lt 10 ]] ; do
echo “i now is ”$i
(( i++ ))
done
3 until Loop
i=0
until [[ $i -gt 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:
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