Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
#Lab1a
echo The arguments are
echo $*
echo "The arguments are in the reverse order";
n=$#
while [ $n -gt 0 ]
do
eval "echo...
#"Write a shell script that accepts two file names as arguments, check if the permissions for these
files are identical an...
#"This programme i.e., lab 2 b is written by Ashok"
#write a shell script that accepts a path name & creates all the compo...
CONCEPT LAB3A
ADDING A USER
mit@mit-desktop:~$ adduser ashok #if this not work try below command.
mit@mit-desktop:~$ sudo ...
#lab3a
#write a shell scripts which accepts valid login name as arguments and prints corresponding home
directory,if no ar...
#Write a shell script OR Create a shell script file called File properties that read
#file name entered & out put it's per...
#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 ...
CONCEPT LAB7B
#WAS Script To Understand Argument Variables $0, $1, $2, $3, $* And $#
echo "Programme Name $0"
echo "1st Ar...
#write a shell script that accept a list of filenames as its arguments, count and report occurence of
each word that is pr...
#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|cu...
#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@u...
#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...
#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=`exp...
Upcoming SlideShare
Loading in …5
×

Unix 1st sem lab programs a - VTU Karnataka

2,873 views

Published on

Unix Lab Programs

Published in: Education, Technology
  • Be the first to comment

Unix 1st sem lab programs a - VTU Karnataka

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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:~$

×