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 Commands Explaination
> file Create/Overwrite file
>> file Append to file
>file 2>&1 redirect both output and error to file
< file read from file
file1 | file1 pipe 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 operations Explaination
lt less than
gt greate than
eq equal
ne not equal
ge greate or equal
le less than or equal

8.String tests

File Test operations Explaination
nt newer than
d is a directory
f is a file
r readable
w writable
x executable

9.Logical & String Tests

Logical & String Tests Explaination
= equal to
z zero lenth
n not zero length
&& Logical AND
|| logical OR
! logical Not

10.Argument Variables

Argument Variables Explaination
$0 Program name or script name
$1 1st argument
$2 2nd arguments
$9 9th 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 Commands Usage
command1 ||Command2 run command1; if it fails, run Command2
command1 && Command2 run command1; if it works, run Command2
command1 ; Command2 keeping 2 commands on the same line
ls -lSt list files biggest last
ls -lrt list files newest last
ls -al show all fils including hidden
sort -n sort numarically
wget URL download url
read x read some value from user /keyboard
touch file create empty file
cmd |tee file.txt command output to stdout also to file.txt
ifconfig -a list all network interfaces .can see ip here
netstat -r show routers
netstat -tnpl |grep -I listen shows all listening ports on the server
ssh u@host login to host as user u
scp file.txt u@host:\tmp copy file.txt to host /tmp/ wit u user
alias l=’ls -lrt’ creating alias to a command
df -h show dis mount points
find . -type f -name a.txt find a.txt in current directory
find . -type f -name *.txt -print find all the txt files
find /foo -type d -ls list 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.txt create acrchive file
tar -tvf abc.tar check the list of file in abc.tar
tar -rvf abc.tar c.txt add c.txt to existing abc.tar
tar -xvf abc.tar extract abc.tar file
tar -zcvf my_archive.tar.gz * create zip and then tar
tar -zxvf my_archive.tar.gz * extract zip tar
zcat view the file without decompressing it like cat
ps -ef |grep keyword grep some process running
kill -9 pid kill process with pid
pwd present working dir
cd Go to user home
cd .. Go to previous directory
hosname hostname of the server

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

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *