Raish Vhora: K.P.Patel School of Management & Computer Studies (KSMCS)
Raish Vhora: K.P.Patel School of Management & Computer Studies (KSMCS)
uiopasdfghjklzxcvbnmqwertyuiopasd
fghjklzxcvbnmqwertyuiopasdfghjklzx
K.P.Patel School of Management &
cvbnmqwertyuiopasdfghjklzxcvbnmq
Computer Studies (KSMCS)
Programming Skills-V(OS)
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
Raish Vhora
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmrtyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuiopas
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
2) ls
ls - list directory contents
OUTPUT
digit@ubuntu:~$ ls
Desktop Downloads Firefox_wallpaper.png Pictures Templates VLC
Documents examples.desktop Music
3) who
who - show who is logged on
OUTPUT
digit@ubuntu:~$ man who
who
who
digit tty7 2010-12-08 23:36 (:0)
digit pts/0 2010-12-09 00:12 (:0.0)
4) cal
cal, ncal — displays a calendar and the date of Easter
OUTPUIT
digit@ubuntu:~$ cal
December 2010
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
5) ps
ps - report a snapshot of the current processes.
OUTPUT
digit@ubuntu:~$ ps
2 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
6) wc
wc - print newline, word, and byte counts for each file
digit@ubuntu:~/VLC$ wc install.sh
33 89 603 install.sh
7) cat
cat - concatenate files and print on the standard output
digit@ubuntu:~/VLC$ cat f1.txt
Nadiad
Anand
Baroda
8) uname
uname - print system information
digit@ ubuntu:~/VLC$ uname
Linux
9) pwd
pwd - print name of current/working directory
digit@ubuntu:~/VLC$ pwd
/home/digit/VLC
10) mkdir
mkdir - make directories
digit@ubuntu:~/VLC$ mkdir KSMCS
3 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
11) rmdir
rmdir - remove empty directories
digit@ubuntu:~/VLC$ rmdir KSMCS
12) cd
cd – to change the current directory
digit@ubuntu:~/VLC$ cd KSMCS
13) cp
cp - copy files and directories
digit@ubuntu:~/VLC$ cp f1.txt ff.txt
14) rm
rm - remove files or directories
digit@ubuntu:~/VLC$ rm KSMCS
15) mv
mv - move (rename) files
mv KSMCS MCA
16) diff
diff - compare files line by line
digit@ubuntu:~/VLC$ diff f1.txt f2.txt
17) chmod
chmod - change file mode bits
digit@ubuntu:~/VLC$ chmod +ux prg1.sh
18) grep
grep, egrep, fgrep, rgrep - print lines matching a pattern
grep -c $i temp
19) sed
sed - stream editor for filtering and transforming text
sed -n "$eno"'!p' $fnm > te.lst
20) head
head - output the first part of files
digit@ubuntu:~/VLC$ head f1.txt
4 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
Nadiad
Anand
Baroda
21) tail
tail - output the last part of files
digit@ubuntu:~/VLC$ tail f1.txt
Nadiad
Anand
Baroda
22) cut
cut - remove sections from each line of files
cut -d ' ' -f1
23) paste
paste - merge lines of files
paste f1.txt f2.txt
24) sort
sort - sort lines of text files
digit@ubuntu:~/VLC$ sort f1.txt
Anand
Baroda
Nadiad
25) find
find - search for files in a directory hierarchy
digit@ubuntu:~/VLC$ find f1.txt ff
5 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
read ch
if [ "$ch" = "+" ];
then
echo "$a + $b =" `expr $a + $b` ;
elif [ "$ch" = "-" ];
then
echo "$a - $b =" `expr $a - $b` ;
elif [ "$ch" = "*" ];
then
echo "$a * $b =" `expr $a \* $b` ;
elif [ "$ch" = "/" ];
then
echo "$a / $b =" `expr $a / $b` ;
fi
b) Accept the string and checks whether the string is palindrome or not.
clear
echo "enter string:- \c"
read s
l=${#s}
echo "length of string is:- "$l
rev=""
while [ $l -ge 1 ]
do
ch=`echo $s | cut -c$l`
rev=$rev$ch
l=`expr $l - 1`
done
echo "reverse is" $rev
if [ $rev = $s ]
then
echo "palindrom"
else
echo "not palindrom"
fi
c) Accept number and check the number is even or odd, finds the length of the
number, sum of the digitsin the number.
clear
echo "Enter The Number :\c"
read n
l=${#n}
echo "\nThe Length Of The Nunber Is : "$l
6 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
t=`expr $n % 2`
if [ $t -eq 0 ]
then
echo "The Number Is Even"
else
echo "The Number Is Odd"
fi
while [ $n -gt 0 ]
do
r=`expr $n % 10`
sum=`expr $sum + $r`
n=`expr $n / 10`
done
if [ $s = $s1 ]
then
echo "No Need For Change"
else
temp=$s
s=$s1
s1=$temp
fi
echo "The Old String Is :"$s
e) Accept filename and displays last modification time if file exists, otherwise
display appropriate message.
clear
echo "Enter The File Name :\c"
read name
if [ -f $name ]
then
t=`ls -l $name | tr -s ' ' | cut -d ' ' -f6,7`
7 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
done
cat reverse
3. Write a script to find the global complete path for any file.
clear
echo "enter file name:- "
read fname
8 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
if [ ! -d $dir1 ]
then
echo "$dir1 directory does not exists"
exit
fi
if [ ! -d $dir2 ]
then
echo "$dir2 directory does not exists"
9 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
exit
fi
if [ ! -d $dir3 ]
then
echo "$dir3 directory does not exists"
exit
fi
for i in $*
do
c=`grep -c $i temp`
if [ $c -eq 0 ]
then
cp "$dir1/$i" $dir3
else
if [ "$dir1/$i" -nt "$dir2/$i" ]
then
echo "$dir1/$i copied to $dir3"
cp "$dir1/$i" $dir3
else
echo "$dir2/$i copied to $dir3"
cp "$dir2/$i" $dir3
fi
fi
done
6. Write a script to compare identically named files in two different directories
and if they are same, copy one of them in a third directory.
clear
echo "enter source directory 1 :- \c"
read dir1
if [ ! -d $dir1 ]
then
echo "$dir1 directory does not exists"
exit
10 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
fi
if [ ! -d $dir2 ]
then
echo "$dir2 directory does not exists"
exit
fi
if [ ! -d $dir3 ]
then
echo "$dir3 directory does not exists"
exit
fi
set ` ls $dir1 `
` ls $dir2 > temp `
for i in $*
do
c=` grep -c $i temp `
if [ $c -eq 1 ]
then
c=` cmp "$dir1/$i" "$dir2/$i" | wc -l `
if [ $c -eq 0 ]
then
echo "$dir1/$i copied to $dir3"
cp "$dir1/$i" $dir3
fi
fi
done
7. Write a script to delete zero sized files from a given directory (and all its sub-
directories).
$ find . -empty -maxdepth 1 -exec rm {} \;
to find zero size file
find . -size 0c
11 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
8. Write a script to display the name of those files (in the given directory) which
are having multiple links.
clear
echo "enter directory name:- \c"
read dname
if [ -d $dname ]
then
`ls -l $dname | tr -s ' ' | cut -d ' ' -f2,8 > temp`
l=`cat temp | wc -l`
i=2
while [ $i -le $l ]
do
str=`head -$i temp | tail -l`
link=`echo $str | cut -d ' ' -f1`
fname=`echo $str | cut -d ' ' -f2`
if [ $link -gt 1 ]
then
echo "file $fname has $link link"
fi
i=`expr $i + 1`
done
else
echo "not exist"
fi
9. Write a script to display the name of all executable files in the given directory.
clear
echo "enter directory home:- \c"
read dname
if [ -d $dname ]
then
set `ls $dname`
for i in $*
do
if [ -x $i ]
then
echo $i
fi
done
12 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
else
echo "file does not exist"
fi
or
clear
if [ -d $dname ]
then
ls $dname
for i in $*
do
if [ -x $i ]
then
echo $i
ls $i
fi
done
else
echo "\n Does Not Exits"
fi
10.Write a script to display the date, time and a welcome message (like Good
Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24
hours notation.
month=`date +%B`
day=`date +%d`
year=`date +%Y`
hour=`date +%k`
minutes=`date +%M`
if [ $hour -gt 12 ]
then
var="p.m"
else
var="a.m"
fi
13 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
11. Write a script to display the directory in the descending order of the size of
each file.
clear
if [ -d $name ]
then
echo "file in descending order"
echo "size <-> filename"
`ls -l $dname | tr -s ' ' | cut -d ' ' -f5,8 > temp`
sort -grk1 temp
else
echo "directory does not exist"
fi
14 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
read a
which $a
13. Write a script for generating a mark sheet after reading data from a file.
File contains student roll no, name , marks of three subjects.
if [ $# -ne 3 ]
then
echo "You have to Enter 3 arguments for marks....."
else
m1=$1
m2=$2
m3=$3
15 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
clear
case $ch in
1)
dir;;
2)
ls;;
3)
echo "\n Enter Your Directory Name :\c"
read dname
mkdir $dname;;
4)
cd;;
5)
mv -i $name $dname;;
6)
16 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
mv -i $name $dname;;
7)
rmdir $name;;
*)
echo"Invalid Choice :"
esac
15. Write a script which reads a text file and output the following
Count of character, words and lines.
File in reverse.
Frequency of particular word in the file.
Lower case letter in place of upper case letter.
clear
if [ -f $fname ]
then
17 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
case $c in
1)
2)
rev $fname;;
3)
4)
echo " Lower Into UpperCase :"
t=`cat $fname | tr "a-z" "A-Z"`
echo $t
18 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
5)exit;;
esac
else
echo "\n File Does Not Exit "
fi
16. Write a shell script to check whether the named user is currently logged in or
not.
echo "enter the user name:- \c"
read unm
19 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
fnm=emp.lst
read ch
case "$ch" in
1)
cat $fnm
;;
2)
read eno
if [ $1 -ne 0 ] ; then
20 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
break
else
continue
fi
done
;;
3)
read eno
continue
else
break
fi
done
read ename
read addr
21 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
read age
read gend
read desg
read sal
4)
read eno
if [ $1 -ne 0 ] ; then
read ans
case "$ans" in
[yY]*)
22 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
cp te.lst $fnm
esac
break
else
continue
fi
done
;;
5)
read eno
read cho
case "$cho" in
23 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
fild=2;;
fild=3;;
fild=4;;
fild=6;;
fild=7;;
6)
break;;
fild=0;;
esac
read new
cp te.lst $fnm
fi
;;
24 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
esac
done
case $ch in
1)
if [ $str = $str1 ]
then
echo "Both Strings Are Same "
else
echo "Both Strings Are Different"
fi
25 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
;;
2)
echo "\n Enter The First String :\c "
read s1
str3=$s1$s2
echo $str3
;;
3)
echo "\n Enter The String :\c "
read str
l=${#str}
echo "\n The Length Of String Is "$l
;;
4)
;;
5)
echo "\n Enter The String :\c "
read s
l=${#s}
rev=""
while [ $l -ge 1 ]
do
26 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
;;
6)exit;;
esac
19. Write a script to calculate gross salary for any number of employees
Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.
clear
27 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
read s
l=${#s}
echo "length of string is:- "$l
rev=""
while [ $l -ge 1 ]
do
ch=`echo $s | cut -c$l`
rev=$rev$ch
l=`expr $l - 1`
done
echo "reverse is" $rev
if [ $rev = $s ]
then
echo "palindrom"
else
echo "not palindrom"
fi
21. Write a script to check whether a given number is palindrome or not.
clear
if [ $rev -eq $t ]
then
echo palindrom
else
echo not palindrom
fi
28 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
if [ -f $fname ]
then
echo "==================================================="
echo "Original File Content is "
echo "==================================================="
cat $fname
echo "==================================================="
echo "File words in ascending order is"
echo "==================================================="
set `cat $fname`
for i in $*
do
echo "$i" >> temp
done
sort temp
rm temp
echo "==================================================="
else
echo "File does not exist"
fi
23. Write a script to display all lines of a file in ascending order.
echo "Enter File Name \c"
read fname
if [ -f $fname ]
then
echo "==================================================="
echo "Original File Content is "
echo "==================================================="
cat $fname
echo "==================================================="
echo "File lines in ascending order is"
echo "==================================================="
cat $fname | sort
else
echo "File does not exist"
fi
24. Write a script to display the last modified file.
29 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
clear
fname=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f8`
ctime=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f6,7`
25. Write a shell script to add the statement #include <stdio.h> at the beginning of
every C source file in current directory containing printf and fprintf.
while true; do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break
echo "Invalid path! Try Again!"
done
path=${path%/}
myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`
if [ $? -gt 1 ]; then # grep exits with status 1 when no matches were found.
echo -n "No Matches were found. " && exit
fi
temp=$(mktemp tmp.XXXXXXXXX)
for i in $myargs # Here, grep has the exit status 0.
do
echo "Do you want to add '#include <stdio.h>' to $i?"
read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > "$temp" # i for insertion, 1 for 1st line. $i is the file to
insert. and all output will be redirected to $temp
;;
n|N|NO|no|No|nope)
echo "Alright! Next.."
shift
;;
*)
echo "Invalid input."
30 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
;;
esac
done
if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 $path/*.c | less
fi
rm $temp
26. Write a script that behaves both in interactive and non-interactive mode.
When no arguments are supplied, it picks up each C program from current
directory and lists the first 10 lines. It then prompts for deletion of the file. If the
user supplies arguments with the script, then it works on those files only.
clear
if [ $# -eq 0 ]
then
set `ls *.c`
for i in $*
do
if [ -f $i ]
then
echo "file name:- $i"
head 10 $i | cat
echo "do you want delete file $i [y/n]:- \c"
read ans
if [ $ans = 'Y' -o $ans = 'y' ]
then
rm $i
fi
echo "enter any key to continue"
read ans
fi
done
else
for i in $*
31 Raish Vhora
K.P.Patel School of Management & Computer Studies MCA
(KSMCS)
do
if [ -f $i ]
then
echo "file name:- $i"
head 10 $i | cat
echo "do you want delete file $i [y/n]:- \c"
read ans
if [ $ans = 'Y' -o $ans = 'y' ]
then
rm $i
fi
echo "enter any key to continue"
read ans
fi
done
fi
27. Write a script that deletes all leading and trailing spaces in all lines in a file.
Also remove blank lines from a file. Locate lines containing only printf but not
fprintf.
echo “Enter the file name”
read a'
OR
32 Raish Vhora