Bash Scripting Session - 2
Bash Scripting Session - 2
Bash Scripting Session - 2
BASH SCRIPTING
ESL
Prepared by:
Ali Mohamed
Ali El–Sherbeny
Mahmoud Saad
OUTLINES
■ MOTIVATION (10 MINS)
■ REVIEW ON FIRST SESSION
1. WILD CARDS
2. PIPING
3. SOME COMMANDS
■ NEW COMMANDS(CAT/MORE/LESS/ECHO/READ]
■ USER ADMINISTRATION
OUTLINES
■ QUESTIONS TIME
■ BREAK (15 MINS)
■ FILE PERMISSION
■ CREATE VARIABLES
■ POSITIONAL PARAMETERS
■ FIRST SCRIPT RUN
■ ARITHMETIC& LOGICAL
Review on some commands :
Function command
Know present working directory (where am I) pwd
Change to another place cd
Go to parent directory cd ..
Go to previous directory cd -
Go to relative path cd directory or cd ./directory
Go to home directory cd or cd~
List contents of current directory ls , ls -alh , ls --all
View contents in tree format tree
Get help with the command man , whatis , help
Forgot part of the command man –k part of the command
Move files to new place or rename files mv oldFileName NetFileName
mv FileName NewPlaceOrDirectory
Copy files in same / other directories cp filename NewCopiedFileName
Review
Function command
Remove directories rmdir , rm -r
Create new file touch filename
Create new directory mkdir
Cat command
■ Less filename
more filename
pg filename
Find
whatis
cut
sort
option Function
-n Numerical sort
-r Reverse sort (descending order)
-f Sort upper and lower case together
-k fieldnuumber Sort according to the field number
specified
wc
Format : wc Filename
options Function
-l Count lines
-w Count words
-c Count bytes
-m Count chars
PIPING
| : pipe sign .
Ecamples :
Redirection is changing the standard input or output to another input or output you specify.
1. OutPut redirection :
3. Error Redirection :
Note :
0 represent standard input
1 represent standard output
2 represent standard error
Example :
lsa 2>filename
Solution :
cat > textfile
sort 0<textfile 1>outputFile
Task
■ Required :
SOLUTION :
ls –lh > test
wc –l < test 1>>test
Variables :
- we use the "$" sign while accessing the variable but we don't use it when we give a value
to this variable .
-values assigned through variables through "read" or you assign a value to them instantly.
Example :
■ If we want to assign the output of a command as a value for the variable we use the ``
quotations .
Example:
set hi hello hey
x = `echo $*`
echo $x
Positional parameters (command line
arguments)
■ - A facility in the shell called positional parameters. Positional parameters are a series
of special variables ($0 through $9) that contain the contents of the command line.
-Arguments passed to a shell script while calling it in the command line are called
positional parameters .
- They can be set by 2 ways :
1. The "set" command
example :
set esl 2018
now $1 saves the word "esl" , $2 saves the word 2018
2. Passing them when calling script :
bash scriptname esl 2018
$1 saves the word "esl" , $2 saves the word 2018 , same as above but they can be used
within the script .
Positional Parameters
■ After any command is executed , it returns exit status that shows if this command is
executed suuccesfully or failed while execution .
- It's saves in $? , you can check it to make sure the commands run well .
-
At exit status = 0 Means command run well
At exit status = 1 Means command failed , some commands
return numbers other than 1 to determine
reason of failiure .
Operators :
■ 1. Mathematical operators :
operator meaning
+ add
- subtract
/ division
\* multiply
% Remainder of division
Mathematical Operators :
■ Normally echo reads the inputs as if they're string . Therefore using the rthimitic
operators directly won't work . Therefore we use commands or processes to solve this :
a) expr
Deals with integer arguments , gives integer outputs .
DONOT FORGET SPACES WHILE USING IT .
example:
x=`expr 2 + 2`
exho $x
Continue : MAthematical operators
Examples :
a=10
c=$(($a+4))
((d=$a+$c))
Continue : Mathematical operators
■ C) bc
Examples :
echo 20 + 5 / 2 | bc #must leave spaces to work
echo 'scale=4;20+5/2' |bc #spaces here are fine
Logical operators
■ For numbers :
operator meaning
-eq Equal ?
-ne Not equal ?
-lt Less than ?
-le Less than or equal ?
-gt Greater than ?
-ge Greater than or equal ?
Logical Operators :
■ For strings
Operator Meaning
Str1 == str2 Equal ?
Str1 != str2 Not equal ?
-z str Null string ?
-n str Not null string ?
>>by this command any file start with any > matches any occurrence of character
set of arbitary characters then followed by enclosed in the square brackets
. Then followed by any set of $ls –l ESL-201[678].log
arbitary characters then followed by htm
List all file named >>
then followed by any arbitary set of
characters (ESL-2016 & ESL-2017 & ESL-2018)
(A7_.html & B&G.html & .htm ) is >> $ ls ESL-201[6-8].log
deleted Is this command the same as above? [just try]
FILE PERMISSIONS
$ls –l (list using long format)
1) File type
- (File) d(directory)
2)permission
This command used to read the user input from the keyboard
___If you won’t specify the variable where to store the data.
By default the data is stored in the variable called REPLY. So we can invoke the data
as $REPLY
__If you specify a variable so The variable become place holder for the text you enter.
The whole unit of text is stored in the variable you provide
1)$read –s
This option -s means secret or secure whatever is used to read the sensitive data like passwords.
Generally when you type entering the data it appears in the terminal. But with this option is used
the text won’t appear in terminal
2)$read –n
This option -n allows the user to enter only the specific length of characters. It won't allow you
to enter more than the given number. After reaching the length you have given it automatically
ends the reading mode. You again no need to press the Enter key
3)$read -p “prompt(hint)_text”
Here the user read the data along with some hint text . The hint text helps the user what he/she
has to enter.(-p here stands for the prompt).
Ex: read –p "Enter Your Name:" name
if name variable not existed so the user input is stored in REPLAY variable
???
Q2
???
Q3
???
FIRST SCRIPT RUN
Editors
1)GUI based Editors
- gedit
$nano scrpit.sh
$touch script.sh
$nano scrpit.sh
First Script
#!/bin/bash
+ : add permission
- : remove permission
w: Write
r: Read
x: Execute