SlideShare a Scribd company logo
#Lab1a
echo The arguments are
echo $*
echo "The arguments are in the reverse order";
n=$#
while [ $n -gt 0 ]
do
eval "echo $$n" #by using $ we are hiding special meaning of $
n=`expr $n - 1`
done
#/*user@user-Inspiron-N5050:~$ sh lab1a.sh a b c
#The arguments are
#a b c
#The arguments are in the reverse order
#c
#b
#a
#user@user-Inspiron-N5050:~$ sh lab1a.sh 1 2 3
#The arguments are
#1 2 3
#The arguments are in the reverse order
#3
#2
#1
#user@user-Inspiron-N5050:~$
#"Write a shell script that accepts two file names as arguments, check if the permissions for these
files are identical and if the permission are identical, output common permissions and otherwise
output each file name followed by its permissions."
#lab1b
if [ -e $1 ]
then
if [ -e $2 ]
then
x=`ls -l $1|cut -d " " -f1`
y=`ls -l $2|cut -d " " -f1`
if [ $x = $y ]
then
echo "common permission $x"
else
echo "diffrent permissons"
echo "filename is $1 permisson is $x"
echo "filename is $2 permisson is $y"
fi
else
echo "$2 not existing"
fi
else
echo "$1 not existing"
fi
#mit@mit-desktop:~$ gedit lab1b.sh
#mit@mit-desktop:~$ sh lab1b.sh 1 abc /* here abc is a file that not present in our system */
#abc not existing
#mit@mit-desktop:~$ sh lab1b.sh abc 1 /* here abc is a file that not present in our system */
#abc not existing
#mit@mit-desktop:~$ sh lab1b.sh 1 programme2 /* here programme2 is a file that not present in
our system */
#programme2 not existing
#mit@mit-desktop:~$
#"This programme i.e., lab 2 b is written by Ashok"
#write a shell script that accepts a path name & creates all the components in that path name as
directories.
#For ex, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b,
a/b/c, a/b/c/d
echo $1>f1 #echo $1 holds the contents of file f1.
x=`cat f1|tr "/" " "`
for i in $x #$x holds all the command line arguments.
do
mkdir $i
cd $i
done
CONCEPT LAB3A
ADDING A USER
mit@mit-desktop:~$ adduser ashok #if this not work try below command.
mit@mit-desktop:~$ sudo adduser ashok
#this command is used to add user. Here sudo stands
for super user.
DISPLAYS PASSWORD FILE OF THE UNIX SYSTEM
mit@mit-desktop:~$ cat /etc/passwd
#this command is used to password file
of the unix system.
mit:x:1000:1000:mit,,,,:/home/mit:/bin/bash
#mit is user and he as password.
ashok:x:1002:1006:Ashok,,,:/home/ashok:/bin/bash #mit is user and he as password.
UNDERSTANDING GREP COMMAND
mit@mit-desktop:~$ cat > file1
mit ashok mca 1st sem
mit@mit-desktop:~$ grep "1st" file1
mit ashok mca 1st sem
mit@mit-desktop:~$ cat > file2
mitashokmca1stsem
mit@mit-desktop:~$ grep "1st" file2 #grep command is used to search the word "1st" is present or
not.
mitashokmca1stsem
mit@mit-desktop:~$ grep -w "1st" file2 #-w command searches the word iff the contents of the
file separated by spaces.
mit@mit-desktop:~$ grep -w "1st" file1
mit ashok mca 1st sem
mit@mit-desktop:~$ grep -c "1st" file1
1

#-c is used to count the number of occurence of the
given word ex: here 1st is occured for 1 time.

mit@mit-desktop:~$ grep -n "1st" file1 #-n is used to display in which line the word "1st" is
occured
1:mit ashok mca 1st sem
#lab3a
#write a shell scripts which accepts valid login name as arguments and prints corresponding home
directory,if no argument is specified print a sutiable error msg.
if [ $# -ne 0 ]
then
for i in $*
do
grep -w "$i" /etc/passwd>ash1#ash1 is a file if $i is present in a file /etc/passwd, the
line of the contents written in file "ash1" otherwise not.
if [ -s ash1 ]

#-s checks the file size.

then
echo "$i is valid user";

#if the file size is not zero then this statement
get exexuted.

cat ash1 | cut -d ":" -f6
else
echo "$i is not a valid user"; #if the file size is zero, this is executed.
fi
done
else
echo "no arguments are passed";
fi
#mit@mit-desktop:~$ sh ashu.sh mit
#mit is valid user
#/home/mit
#mit@mit-desktop:~$ sh ashu.sh MIT
#MIT is not a valid user
#mit@mit-desktop:~$ sh ashu.sh Mit
#Mit is not a valid user
#mit@mit-desktop:~$ sh ashu.sh ashok
#ashok is valid user
#/home/ashok
#mit@mit-desktop:~$ sh ashu.sh vinay
#vinay is not a valid user
#mit@mit-desktop:~$ sh ashu.sh #command with out any arguments.
#no arguments are passed
#Write a shell script OR Create a shell script file called File properties that read
#file name entered & out put it's permissions.
#LAB4A
x=1
while [ $x -eq 1 ]
do
echo "enter u r choice";
read ch
echo "u r choice is $ch"
case $ch in #$ch contain some value.
1 ) echo "file permission `ls -l $0|cut -d ' ' -f1`";; # -f1 is first fild & $0 is current file
itself .
2 )echo "link info `ls -l $0|cut -d ' ' -f2`";; #-f2 is 2nd field
3 )echo "owner info `ls -l $0|cut -d ' ' -f3`";;
4 )echo "group info `ls -l $0|cut -d ' ' -f4`";;
5 )echo "file size `ls -l $0|cut -d ' ' -f5`";;
6 )echo "date of creation `ls -l $0|cut -d ' ' -f6`";;
7 )echo "time `ls -l $0|cut -d ' ' -f7`";;
8 )echo "file name `ls -l $0|cut -d ' ' -f8`";;
* )echo "invald option"
esac
echo "Do you want to contine yes=1 or not=0";
read x
done
#WAP To Find Smallest Of 3 Numbers.
#lab6b
read a
read b
read c
small=$a
if [ $b -lt $a ]
then
small=$b
fi
if [ $c -lt $b ]
then
small=$c
fi
echo "$small is smallest number among $a $b & $c"
#ashok@ubuntu:~$ sh small.sh
#1
#2
#3
#1 is smallest number among 1 2 & 3
#ashok@ubuntu:~$ sh small.sh
#99
#2
#55
#2 is smallest number among 99 2 & 55
#ashok@ubuntu:~$ sh small.sh
#99
#1058
#-99
#-99 is smallest number among 99 1058 & -99
#ashok@ubuntu:~$
CONCEPT LAB7B
#WAS Script To Understand Argument Variables $0, $1, $2, $3, $* And $#
echo "Programme Name $0"
echo "1st Argument $1"
echo "2nd Argument $2"
echo "3rd Argument $3"
echo "All Arguments $*"
echo "Total No. Of Arguments $#"
#output
#ashok@ubuntu:~$ sh 7a.sh 1 2 3 4
#Programme Name 7a.sh
#1st Argument 1
#2nd Argument 2
#3rd Argument 3
#All Arguments 1 2 3 4
#Total No. Of Arguments 4
#ashok@ubuntu:~$
#Write A Shell Script to Compute The Sum Of Numbers Passed To It As Arguments On Command
Line And Display The Result.
#lab7b
num=$1
sum=0
a=0
while [ $num -ne 0 ]
do
rem=`expr $num % 10`
num=`expr $num / 10`
sum=`expr $sum + $rem`
done
echo "sum of digits is = $sum"
#output
#ashok@ubuntu:~$ sh 7b.sh 123
#sum of digits is = 6
#ashok@ubuntu:~$ sh 7b.sh 123456789
#sum of digits is = 45
#ashok@ubuntu:~$ sh 7b.sh 222222
#sum of digits is = 12
#ashok@ubuntu:~$
#Line1 $1 holds 1st Argument. num holds value of $1
#write a shell script that accept a list of filenames as its arguments, count and report occurence of
each word that is present
#in the first argument file on other argument files.
#lab9b
for pattren in `cat $1`
#file is variable like "i" for ex in lab2b programme.
do
for file in $*
#file is variable like "i" for ex in lab2b programme.
do
if [ $file != $1 ]
then
x=`grep -iow $pattren $file|wc -l`
echo "$file contains the word $pattren $x timesn"
fi
done
done
#mcaexam@mit:~$ sh lab9b.sh l1 l2 l3
#l2 contains the word abc 2 times
#
#l3 contains the word abc 2 times
#
#l2 contains the word abcd 1 times
#
#l3 contains the word abcd 1 times
#
#mcaexam@mit:~$ gedit lab9b.sh
#mcaexam@mit:~$ cat l1
#abc
#abcd
#mcaexam@mit:~$ cat l2
#abc
#abc
#abcd
#mcaexam@mit:~$ cat l3
#abc abc
#abcd
#mcaexam@mit:~$
#Lab5b.sh
if [ $# -lt 1 ]
then
echo "Enter file name as an argument";
else
for i in $*
do
if [ -e $i ]
then
x=`ls -l $i|cut -d " " -f7`
echo "file is $i and creation time is $x";
else
echo "$i doesn't exist";
fi
done
fi
#user@user-Inspiron-N5050:~$ sh lab5b.sh f1
#f1 doesn't exist
#user@user-Inspiron-N5050:~$ sh lab5b.sh lab5b.sh
#file is lab5b.sh and creation time is 18:52
#user@user-Inspiron-N5050:~$
#delete a word
#lab 8b
echo "enter a word"
read word
for i in $*
do
grep -iwv "$word" $i > temp
cat temp > $i
done
#user@user-Inspiron-N5050:~$ cat f1
#Ashok
#Harish
#Prakash
#user@user-Inspiron-N5050:~$ cat f2
#inay
#Ashok
#Mahendra
#user@user-Inspiron-N5050:~$ sh lab8b.sh f1 f2
#enter a word
#Ashok
#user@user-Inspiron-N5050:~$ cat f1
#Harish
#Prakash
#user@user-Inspiron-N5050:~$ cat f2
#inay
#Mahendra
#user@user-Inspiron-N5050:~$
#labb11b.sh
if [ $3 -ge $2 ]
then
n=`cat $1|wc -l`
if [ $n -ge $3 ]
then
x=`expr $3 - 1`
y=`expr $x - $2`
head -$x $1|tail -$y
else
echo "insufficient number of lines";
fi
else
echo "ending line number should be greater than starting line number ";
fi
#user@user-Inspiron-N5050:~$ cat f1
#hari
#ashok
#prasad
#prakash
#mary
#koppal
#udupi
#ubuntu
#unix
#windows
#user@user-Inspiron-N5050:~$ sh labb11b.sh f1 2 6
#prasad
#prakash
#mary
#user@user-Inspiron-N5050:~$
#lab11a.sh
k=$2
if [ $k -lt 0 ]
then
k=`expr $k * -1`
fi
i=$1
j=1
pow=1
while [ $j -le $k ]
do
pow=`expr $pow * $i`
j=`expr $j + 1`
done
if [ $2 -lt 0 ]
then
echo "scale=3;`expr 1/$pow`"|bc
else
echo "$1 to the power of $2 is $pow"
fi

#user@user-Inspiron-N5050:~$ sh lab11a.sh 2 3
#2 to the power of 3 is 8
#user@user-Inspiron-N5050:~$ sh lab11a.sh 2 -3
#.125
#user@user-Inspiron-N5050:~$

More Related Content

PPTX
Concept learning and candidate elimination algorithm
PDF
Introduction to Deep Learning
PPTX
AI_Session 17 CSP.pptx
PPT
Video object tracking with classification and recognition of objects
PPT
License Plate Recognition
PPTX
Artificial Intelligence
PPTX
Exhaustive Search
PPTX
weak slot and filler structure
Concept learning and candidate elimination algorithm
Introduction to Deep Learning
AI_Session 17 CSP.pptx
Video object tracking with classification and recognition of objects
License Plate Recognition
Artificial Intelligence
Exhaustive Search
weak slot and filler structure

What's hot (20)

PPTX
License Plate recognition
PPTX
Number plate recognition system using matlab.
DOCX
Digtial Image Processing Q@A
PPT
2.4 rule based classification
PPT
Model 1 multimedia graphics and animation introduction (1)
PPTX
1. digital image processing
PDF
R programming groundup-basic-section-i
PPTX
Deep Learning for Natural Language Processing
PPTX
AI3391 Artificial Intelligence Session 17 imperfect real time decisions.pptx
PDF
Nepal Police(Inspector) Computer Engineer/IT MCQ questions
PPTX
AI: Learning in AI
PPTX
Graph coloring using backtracking
PPT
Introduction to design and analysis of algorithm
PPT
Iterative deepening search
PPTX
Data science.chapter-1,2,3
PPTX
Phases of Compiler
PPT
01 knapsack using backtracking
PPTX
Face Detection
PPT
Knowledge Representation & Reasoning
PDF
Deepfake detection
License Plate recognition
Number plate recognition system using matlab.
Digtial Image Processing Q@A
2.4 rule based classification
Model 1 multimedia graphics and animation introduction (1)
1. digital image processing
R programming groundup-basic-section-i
Deep Learning for Natural Language Processing
AI3391 Artificial Intelligence Session 17 imperfect real time decisions.pptx
Nepal Police(Inspector) Computer Engineer/IT MCQ questions
AI: Learning in AI
Graph coloring using backtracking
Introduction to design and analysis of algorithm
Iterative deepening search
Data science.chapter-1,2,3
Phases of Compiler
01 knapsack using backtracking
Face Detection
Knowledge Representation & Reasoning
Deepfake detection
Ad

Similar to Unix 1st sem lab programs a - VTU Karnataka (20)

PPT
Talk Unix Shell Script 1
PPT
Talk Unix Shell Script
DOC
Linux Lab Manual.doc
PDF
PDF
Shell scripting
PDF
Bash Scripting Workshop
PPT
Shell programming
PDF
One-Liners to Rule Them All
PPT
Shell Scripts
PDF
Complete Guide for Linux shell programming
PPTX
Shell scripting
PPT
01c shell
DOCX
The Korn Shell is the UNIX shell (command execution program, often c.docx
PDF
Devops for beginners
PPTX
a shell script to print file names in directory showing date of creation & se...
PPTX
First steps in C-Shell
PPTX
Hxjd djdidjdjdLBQ-Udemydhdudje dhdudb.pptx
PPTX
LBQbzhdhd dhdudjeh djdidjehr-Udemy 2.pptx
PDF
Quick start bash script
PPT
Best training-in-mumbai-shell scripting
Talk Unix Shell Script 1
Talk Unix Shell Script
Linux Lab Manual.doc
Shell scripting
Bash Scripting Workshop
Shell programming
One-Liners to Rule Them All
Shell Scripts
Complete Guide for Linux shell programming
Shell scripting
01c shell
The Korn Shell is the UNIX shell (command execution program, often c.docx
Devops for beginners
a shell script to print file names in directory showing date of creation & se...
First steps in C-Shell
Hxjd djdidjdjdLBQ-Udemydhdudje dhdudb.pptx
LBQbzhdhd dhdudjeh djdidjehr-Udemy 2.pptx
Quick start bash script
Best training-in-mumbai-shell scripting
Ad

More from iCreateWorld (6)

PDF
Unix 1st sem lab programs b - VTU Karnataka
PDF
Data structure doubly linked list programs
PDF
Data structure circular list
PDF
Data structure singly linked list programs VTU Exams
DOCX
Location Tracking of Android Device Based on SMS.
PPTX
1 location tracking of android device based on sms
Unix 1st sem lab programs b - VTU Karnataka
Data structure doubly linked list programs
Data structure circular list
Data structure singly linked list programs VTU Exams
Location Tracking of Android Device Based on SMS.
1 location tracking of android device based on sms

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Unix 1st sem lab programs a - VTU Karnataka

  • 1. #Lab1a echo The arguments are echo $* echo "The arguments are in the reverse order"; n=$# while [ $n -gt 0 ] do eval "echo $$n" #by using $ we are hiding special meaning of $ n=`expr $n - 1` done #/*user@user-Inspiron-N5050:~$ sh lab1a.sh a b c #The arguments are #a b c #The arguments are in the reverse order #c #b #a #user@user-Inspiron-N5050:~$ sh lab1a.sh 1 2 3 #The arguments are #1 2 3 #The arguments are in the reverse order #3 #2 #1 #user@user-Inspiron-N5050:~$
  • 2. #"Write a shell script that accepts two file names as arguments, check if the permissions for these files are identical and if the permission are identical, output common permissions and otherwise output each file name followed by its permissions." #lab1b if [ -e $1 ] then if [ -e $2 ] then x=`ls -l $1|cut -d " " -f1` y=`ls -l $2|cut -d " " -f1` if [ $x = $y ] then echo "common permission $x" else echo "diffrent permissons" echo "filename is $1 permisson is $x" echo "filename is $2 permisson is $y" fi else echo "$2 not existing" fi else echo "$1 not existing" fi #mit@mit-desktop:~$ gedit lab1b.sh #mit@mit-desktop:~$ sh lab1b.sh 1 abc /* here abc is a file that not present in our system */ #abc not existing #mit@mit-desktop:~$ sh lab1b.sh abc 1 /* here abc is a file that not present in our system */ #abc not existing #mit@mit-desktop:~$ sh lab1b.sh 1 programme2 /* here programme2 is a file that not present in our system */ #programme2 not existing #mit@mit-desktop:~$
  • 3. #"This programme i.e., lab 2 b is written by Ashok" #write a shell script that accepts a path name & creates all the components in that path name as directories. #For ex, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d echo $1>f1 #echo $1 holds the contents of file f1. x=`cat f1|tr "/" " "` for i in $x #$x holds all the command line arguments. do mkdir $i cd $i done
  • 4. CONCEPT LAB3A ADDING A USER mit@mit-desktop:~$ adduser ashok #if this not work try below command. mit@mit-desktop:~$ sudo adduser ashok #this command is used to add user. Here sudo stands for super user. DISPLAYS PASSWORD FILE OF THE UNIX SYSTEM mit@mit-desktop:~$ cat /etc/passwd #this command is used to password file of the unix system. mit:x:1000:1000:mit,,,,:/home/mit:/bin/bash #mit is user and he as password. ashok:x:1002:1006:Ashok,,,:/home/ashok:/bin/bash #mit is user and he as password. UNDERSTANDING GREP COMMAND mit@mit-desktop:~$ cat > file1 mit ashok mca 1st sem mit@mit-desktop:~$ grep "1st" file1 mit ashok mca 1st sem mit@mit-desktop:~$ cat > file2 mitashokmca1stsem mit@mit-desktop:~$ grep "1st" file2 #grep command is used to search the word "1st" is present or not. mitashokmca1stsem mit@mit-desktop:~$ grep -w "1st" file2 #-w command searches the word iff the contents of the file separated by spaces. mit@mit-desktop:~$ grep -w "1st" file1 mit ashok mca 1st sem mit@mit-desktop:~$ grep -c "1st" file1 1 #-c is used to count the number of occurence of the given word ex: here 1st is occured for 1 time. mit@mit-desktop:~$ grep -n "1st" file1 #-n is used to display in which line the word "1st" is occured 1:mit ashok mca 1st sem
  • 5. #lab3a #write a shell scripts which accepts valid login name as arguments and prints corresponding home directory,if no argument is specified print a sutiable error msg. if [ $# -ne 0 ] then for i in $* do grep -w "$i" /etc/passwd>ash1#ash1 is a file if $i is present in a file /etc/passwd, the line of the contents written in file "ash1" otherwise not. if [ -s ash1 ] #-s checks the file size. then echo "$i is valid user"; #if the file size is not zero then this statement get exexuted. cat ash1 | cut -d ":" -f6 else echo "$i is not a valid user"; #if the file size is zero, this is executed. fi done else echo "no arguments are passed"; fi #mit@mit-desktop:~$ sh ashu.sh mit #mit is valid user #/home/mit #mit@mit-desktop:~$ sh ashu.sh MIT #MIT is not a valid user #mit@mit-desktop:~$ sh ashu.sh Mit #Mit is not a valid user #mit@mit-desktop:~$ sh ashu.sh ashok #ashok is valid user #/home/ashok #mit@mit-desktop:~$ sh ashu.sh vinay #vinay is not a valid user #mit@mit-desktop:~$ sh ashu.sh #command with out any arguments. #no arguments are passed
  • 6. #Write a shell script OR Create a shell script file called File properties that read #file name entered & out put it's permissions. #LAB4A x=1 while [ $x -eq 1 ] do echo "enter u r choice"; read ch echo "u r choice is $ch" case $ch in #$ch contain some value. 1 ) echo "file permission `ls -l $0|cut -d ' ' -f1`";; # -f1 is first fild & $0 is current file itself . 2 )echo "link info `ls -l $0|cut -d ' ' -f2`";; #-f2 is 2nd field 3 )echo "owner info `ls -l $0|cut -d ' ' -f3`";; 4 )echo "group info `ls -l $0|cut -d ' ' -f4`";; 5 )echo "file size `ls -l $0|cut -d ' ' -f5`";; 6 )echo "date of creation `ls -l $0|cut -d ' ' -f6`";; 7 )echo "time `ls -l $0|cut -d ' ' -f7`";; 8 )echo "file name `ls -l $0|cut -d ' ' -f8`";; * )echo "invald option" esac echo "Do you want to contine yes=1 or not=0"; read x done
  • 7. #WAP To Find Smallest Of 3 Numbers. #lab6b read a read b read c small=$a if [ $b -lt $a ] then small=$b fi if [ $c -lt $b ] then small=$c fi echo "$small is smallest number among $a $b & $c" #ashok@ubuntu:~$ sh small.sh #1 #2 #3 #1 is smallest number among 1 2 & 3 #ashok@ubuntu:~$ sh small.sh #99 #2 #55 #2 is smallest number among 99 2 & 55 #ashok@ubuntu:~$ sh small.sh #99 #1058 #-99 #-99 is smallest number among 99 1058 & -99 #ashok@ubuntu:~$
  • 8. CONCEPT LAB7B #WAS Script To Understand Argument Variables $0, $1, $2, $3, $* And $# echo "Programme Name $0" echo "1st Argument $1" echo "2nd Argument $2" echo "3rd Argument $3" echo "All Arguments $*" echo "Total No. Of Arguments $#" #output #ashok@ubuntu:~$ sh 7a.sh 1 2 3 4 #Programme Name 7a.sh #1st Argument 1 #2nd Argument 2 #3rd Argument 3 #All Arguments 1 2 3 4 #Total No. Of Arguments 4 #ashok@ubuntu:~$ #Write A Shell Script to Compute The Sum Of Numbers Passed To It As Arguments On Command Line And Display The Result. #lab7b num=$1 sum=0 a=0 while [ $num -ne 0 ] do rem=`expr $num % 10` num=`expr $num / 10` sum=`expr $sum + $rem` done echo "sum of digits is = $sum" #output #ashok@ubuntu:~$ sh 7b.sh 123 #sum of digits is = 6 #ashok@ubuntu:~$ sh 7b.sh 123456789 #sum of digits is = 45 #ashok@ubuntu:~$ sh 7b.sh 222222 #sum of digits is = 12 #ashok@ubuntu:~$ #Line1 $1 holds 1st Argument. num holds value of $1
  • 9. #write a shell script that accept a list of filenames as its arguments, count and report occurence of each word that is present #in the first argument file on other argument files. #lab9b for pattren in `cat $1` #file is variable like "i" for ex in lab2b programme. do for file in $* #file is variable like "i" for ex in lab2b programme. do if [ $file != $1 ] then x=`grep -iow $pattren $file|wc -l` echo "$file contains the word $pattren $x timesn" fi done done #mcaexam@mit:~$ sh lab9b.sh l1 l2 l3 #l2 contains the word abc 2 times # #l3 contains the word abc 2 times # #l2 contains the word abcd 1 times # #l3 contains the word abcd 1 times # #mcaexam@mit:~$ gedit lab9b.sh #mcaexam@mit:~$ cat l1 #abc #abcd #mcaexam@mit:~$ cat l2 #abc #abc #abcd #mcaexam@mit:~$ cat l3 #abc abc #abcd #mcaexam@mit:~$
  • 10. #Lab5b.sh if [ $# -lt 1 ] then echo "Enter file name as an argument"; else for i in $* do if [ -e $i ] then x=`ls -l $i|cut -d " " -f7` echo "file is $i and creation time is $x"; else echo "$i doesn't exist"; fi done fi #user@user-Inspiron-N5050:~$ sh lab5b.sh f1 #f1 doesn't exist #user@user-Inspiron-N5050:~$ sh lab5b.sh lab5b.sh #file is lab5b.sh and creation time is 18:52 #user@user-Inspiron-N5050:~$
  • 11. #delete a word #lab 8b echo "enter a word" read word for i in $* do grep -iwv "$word" $i > temp cat temp > $i done #user@user-Inspiron-N5050:~$ cat f1 #Ashok #Harish #Prakash #user@user-Inspiron-N5050:~$ cat f2 #inay #Ashok #Mahendra #user@user-Inspiron-N5050:~$ sh lab8b.sh f1 f2 #enter a word #Ashok #user@user-Inspiron-N5050:~$ cat f1 #Harish #Prakash #user@user-Inspiron-N5050:~$ cat f2 #inay #Mahendra #user@user-Inspiron-N5050:~$
  • 12. #labb11b.sh if [ $3 -ge $2 ] then n=`cat $1|wc -l` if [ $n -ge $3 ] then x=`expr $3 - 1` y=`expr $x - $2` head -$x $1|tail -$y else echo "insufficient number of lines"; fi else echo "ending line number should be greater than starting line number "; fi #user@user-Inspiron-N5050:~$ cat f1 #hari #ashok #prasad #prakash #mary #koppal #udupi #ubuntu #unix #windows #user@user-Inspiron-N5050:~$ sh labb11b.sh f1 2 6 #prasad #prakash #mary #user@user-Inspiron-N5050:~$
  • 13. #lab11a.sh k=$2 if [ $k -lt 0 ] then k=`expr $k * -1` fi i=$1 j=1 pow=1 while [ $j -le $k ] do pow=`expr $pow * $i` j=`expr $j + 1` done if [ $2 -lt 0 ] then echo "scale=3;`expr 1/$pow`"|bc else echo "$1 to the power of $2 is $pow" fi #user@user-Inspiron-N5050:~$ sh lab11a.sh 2 3 #2 to the power of 3 is 8 #user@user-Inspiron-N5050:~$ sh lab11a.sh 2 -3 #.125 #user@user-Inspiron-N5050:~$