Shell Scripting S2: How to Write & Run First Shell Script and Importance of Shabang
Session - 2. How to Write & Run First Shell Script and Importance of Shabang
How to print script name
#!/bin/sh
Ech “ name of the script $0”
#!/bin/sh
echo " scriptname is $#"
###Shll script
-rw-r--r--
rw- ==>User permissions
r--==> group permissions
r-- ⇒ other permissions
####Importance of sha-bag:
#---> sharp
!---> explanation or bang symbol
#! → Sharp bang ====> sha-bang==>shebang
Which purpose :
By using sha-bang ,we can specify the interpreter (command) which is responsible to execute the script
Without shabang :
`$/usr/bin/python3 ./test.py
Python3 ./test.py
With shabang :
./test.py
#! /bin/rm
echo “ this is test “`
./magic.sh =====> it wil remove the file as shabang it pointing to rm
Variables :
$variablename
${variablename}
echo Name⇒ Name
echo $Name⇒ Valid
echo ‘$Name’==> not valid
echo “$Name” ===> Valid
If we use single quotes then variable substitution won't be happened
###Command substitution :
Old style: ===> back quote symbol
command`
New style : $(command)
Thse are two types of command substitution
Back quote its not single quote
To display today date
date⇒ it will display both date and time
Date ===+D%==>display only date
Date +t%==> display only time
echo “ today date is : date +%D’”
echo "the no of file in dir is : $(ls | wc -l)"
the no of file in dir is : 8
##To display number of lines present in test.sh
‘Cat test.sh | wc -l’
Comments in shell script :
to comment
Cntrl +l ⇒ clear screen
Increase the font : ctrl+shift+plus
Decrease the font : ctrl+ minus
Noice. You can escape characters in a post using backslashes.
example
In:
Out:
# comment
Versus
In:
Out:
comment