Monday, August 24, 2020

Shell Scripting for Beginners

 A shell script is a file containing series of commands .The shell reads this file and run each command as if like it entered on the command line.On a Linux Operating System there are multiple Interpreters which will execute the commands that we pass to them . Default is Bash and this is widely available on various operating systems . Other 's are KSH,SH . Some commands may different on there interpreters but majority will support by all.

1.What is shell Scripting
2.Variables
3.File Manipulations
4.Common Iterations/Loops
5.Useful System Variables
6.Test Operators
7.Numeric Tests
8.String tests
9.Logical Tests
10.Argument Variables
11.Some Advance commands ,utilities ,network and file handling commands

1.What is shell Scripting

  In short and simple a shell script is a file containing series of commands .The shell reads this file and run each command as if like it entered on the command line.
  The benefit of this is to simplify the day to day mundane activities . Shell is a scripting language interpreter.
  Scripts unlock the power of our Linux machine. imagine you have 2 files on with 100 words and another with 100000 words and you need to check first file words on second file and print the word existence and no of times present .
  This is where the power of scripting comes .Once you write a simple script then start using it and enhance further to fit into day to day activities .
#!/bin/bash
echo "Hello World"

First line #!/bin/bash is the interpreter which will execute the commands . Next to that are the commands . echo will print the values in the screen .

2.Variables

 Variable is a keyword to store some values . We have some System predefined variable and user can also define variables in the scripts to store the results of commands .
 Example :
 PATH is the OS define variable .run echo $PATH to print the values 
 VALUE=`date` && echo $VALUE --> here VALUE is user defined variable and it exists till the script finishes .Once finished VALUE variable no more exist.

3.File Manipulations

Below table gives the basic file manipulation operation help full while handling the files.

File CommandsExplaination
> fileCreate/Overwrite file
>> fileAppend to file
>file 2>&1redirect both output and error to file
< fileread from file
file1 | file1pipe output of file1 as input to file2

4.Common Iterations/Loops

Below given blocks are very important because in the scrips for each command that we use we need to check something before executing and store the results some where and iterate through some repetitive steps .

read text file line by line . in many scenarios we need to read file line by line and apply the logic .In the below we are reading and printing with echo.

while read line 
do 
  echo "Line is $line
done < file 

Find matching lines

grep foo file --> print the lines that has foo matching word
egrep 'foo|bar' file -->find multiple word and print the matching lines
grep -i FOO file --> same as above but i for ignore case
grep -v foo file --> -v used to print not matching lines 

Get the output of command to a variable

FILELIST=`ls`
COUNTOFFILES=`ls -lrt |wc -l`

Case is a good way to Iterate avoiding multiple if/elif blocks

case.sh
#!/bin/bash
# case example
 $1 means first argument in the script
# Usage ./case.sh argument
case $1 in
	start)
		echo starting
	;;
	stop)
		echo stoping
	;;
	restart)
		echo restarting
	;;
	*)
		echo don\'t know
	;;
esac

Function declaration and calling ...

multiply() {
	expr $1 \*2
	}
multiply 3 

For loop iterates the in the list of give values and run the logic

for i in 1 2 3 4 5 
do
	echo "In $i loop"
	echo "Do some logic here "
done	

5.Useful System Variables

$? --> what the shell returned or previous command return status
0 means success
other value means failure
This $? verification is very useful to check the status of previous command to take next decession
$* --> all arguements
./abc.sh 1 2 --> in this $* is 3
$# --> No of the variable values . where $0 is name of script , $1 is 1st arguement ,$2 is 2nd arguement

6.Test Operators

# Compare 2 variable and do something 
if [ "$x" -lt "$y" ] ; then
 # Do something
 fi

7.Numeric Tests

Numeric Test operationsExplaination
ltless than
gtgreate than
eqequal
nenot equal
gegreate or equal
leless than or equal

8.String tests

File Test operationsExplaination
ntnewer than
dis a directory
fis a file
rreadable
wwritable
xexecutable

9.Logical & String Tests

Logical & String TestsExplaination
=equal to
zzero lenth
nnot zero length
&&Logical AND
||logical OR
!logical Not

10.Argument Variables

Argument VariablesExplaination
$0Program name or script name
$11st argument
$22nd arguments
$99th argument
$*all aguements
$#No of Arguments

11.Some Advance commands ,utilities ,network and file handling commands

below are some of the commands that we daily use for the day-to-day activities.

Linux CommandsUsage
command1 ||Command2run command1; if it fails, run Command2
command1 && Command2run command1; if it works, run Command2
command1 ; Command2keeping 2 commands on the same line
ls -lStlist files biggest last
ls -lrtlist files newest last
ls -alshow all fils including hidden
sort -nsort numarically
wget URLdownload url
read xread some value from user /keyboard
touch filecreate empty file
cmd |tee file.txtcommand output to stdout also to file.txt
ifconfig -alist all network interfaces .can see ip here
netstat -rshow routers
netstat -tnpl |grep -I listenshows all listening ports on the server
ssh u@hostlogin to host as user u
scp file.txt u@host:\tmpcopy file.txt to host /tmp/ wit u user
alias l='ls -lrt'creating alias to a command
df -hshow dis mount points
find . -type f -name a.txtfind a.txt in current directory
find . -type f -name *.txt -printfind all the txt files
find /foo -type d -lslist all directories under foo
awk -F":" '{ print $1 " " $NF}'print file value in each line of the file delimted with : and NF for last value
tar -cvf abc.tar a.txt b.txtcreate acrchive file
tar -tvf abc.tarcheck the list of file in abc.tar
tar -rvf abc.tar c.txtadd c.txt to existing abc.tar
tar -xvf abc.tarextract abc.tar file
tar -zcvf my_archive.tar.gz *create zip and then tar
tar -zxvf my_archive.tar.gz *extract zip tar
zcatview the file without decompressing it like cat
ps -ef |grep keywordgrep some process running
kill -9 pidkill process with pid
pwdpresent working dir
cdGo to user home
cd ..Go to previous directory
hosnamehostname of the server

Hope the blog gives some useful information for starting writing scripts ...

Featured

Weblogic Domain Migration

 In this blog we will see domain re-configuration which will be done as part of Weblogic migration from lower version to higher version [ Ex...