[go: up one dir, main page]

0% found this document useful (0 votes)
55 views33 pages

Section 6

This document summarizes shells, shell commands, variables, and control flow structures in Bash shell scripting. It describes the Bash shell and common shell commands like ls, date, and history commands. It also covers variables, command substitution, arithmetic operations, environment variables, user-defined variables, and scripts. Control structures like if/else, while, for, case, and break are explained along with examples of how to use comparison operators and read input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views33 pages

Section 6

This document summarizes shells, shell commands, variables, and control flow structures in Bash shell scripting. It describes the Bash shell and common shell commands like ls, date, and history commands. It also covers variables, command substitution, arithmetic operations, environment variables, user-defined variables, and scripts. Control structures like if/else, while, for, case, and break are explained along with examples of how to use comparison operators and read input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Shells

Bash Shell
os
• App
• Shell(terminal)appl
• Kernel
shell
• Bash
• Csh
• Ksh
• Zsh

• Every shell is binary file


shortcuts
• Ctrl+c =kill process
• clear =save old commands
• Reset = equal new terminal
• Ls ; date # execute two command
• Ls && date #execute second command if first true
• Ls || date #execute first or second
History
• History
• !hi
• !7
• !-3
• !! execute last command
Auto Completion
• First we type the first (3,4,2,..) letters of our command
• Pass
• ch
• Then we press tab
$
1. Variable substitution
2. Command
3. Arithmetic operation
1-Variable substitution

#a=100
#echo $a
#name=ahmed
#echo $name
#name=“ahmed khaled“
#echo $name
2-Command substitution
#date

Date now is output of command date


#echo “date now is $(date)”
Arithmetic operation

• 5+10
#echo “sum is $[5+10]”
#echo “sum is $((5+10))”
#a=5
#b=10
#echo $[a+b]
variables
1. Environment variables(variables declared by the Operating
system that may be edited later by the user)

EX: UID
SHELL
LOGNAME

2. User Defined variables


x y name phone
⮚echo $UID # to print value echo $LOGNAME
******Script: A file containing multiple commands, variables,...
etc

#! /bin/bash
Date ; cal

#end

• Save with extension filename.sh


execute
• To run your script is to add an execute permission to the script and
then running it
chmod u+x testscript
• 1-sh testscript.sh

• Another way
• 2-./testscript
• 3-/absolutepath/testscript
To read value from user
• #read variablename
• User enter value and saved in variable name
• #echo $variablename
************Task
If statement
• If [ $UID –eq 0 ]
• then
• Echo “ your name: $LOGNAME”

• Fi
Comparison operator
• -eq=eqal
• -ne=not equal
• -gt=greater than
• -lt=less than
• -ge=greater than or equal
• -le=less than or equal
• If [ $UID –eq 0 ]
• then
• echo “ your name root”
• else
• echo “ your name: $LOGNAME”
• fi
• If [ $UID –eq 0 ]
• then
• echo “ your name: $LOGNAME”
• else if [ $UID -ge 500 ]
• then
• echo “ your name: $LOGNAME”
• fi
• fi
• If [ $UID –eq 0 ]
• then
• echo “ your name: $LOGNAME”
• elif [ $UID -ge 500 ]
• then
• echo “ your name: $LOGNAME”

• fi
• Elif =else if fi

• Task

• Copy if you root


• Message if not root
• If [ condition 1 ] && [condition 2]
• If [ condition 1 ] || [condition 2]
while
while [ condition ]
do
………
increment (decrement)
done
d= 1
while [ $d -lt 5 ]
do
echo “ welcome linux”
let d=$d+1 or d=$(( $d+1))
• or ((d++))
done
Lab
• Create 5 files in one folder using while
Read input
• echo -n“ enter any value”
• read value
• echo “ your value is : $value”
for
for x in a b
do
echo $x
done

for ((x=1;x<10;x++))
• For var in {value1.. Value2 .. Increment}
do
……….
done
case
Case var in

Valu1) instruction;;
Value2) instruction;;
*) instruction ;; # default value

esac
example
echo “ enter value”
read val
case $val in
Y|y) echo “true”;;
N|n) echo “false”;;
*) echo “in correct”;;
esac
Break + case
• Case $val in
• 1) break;;
• *) Echo “enter new valu”;;
• Esac

You might also like