&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
********************************************************************************************************************************
1)
---------------------------------------------------------------------------------------------
#Basic salary of a person is input through the keyboard. His dearness allowance
is 40% of basic salary and house rent in 20% of basic salary. Write a program
to calculate the gross pay.
-----------------------------Script---------------------------
echo "Enter the basic salary : "
read salary
dearness=`echo "$salary * 0.40" | bc`
hrent=`echo "$salary * 0.20" | bc`
salary=`echo "$salary + $dearness + $hrent" | bc`
echo "Dearness Allowance = $dearness "
echo "House Rent = $hrent"
echo "Gross Salary = $salary"
-----------------------------output---------------------------
Enter the basic salary :
10000
Dearness Allowance = 4000.00
House Rent = 2000.00
Gross Salary = 16000.00
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2)
---------------------------------------------------------------------------------
#The distance between two cities is input through the keyboard (in km). Write
a program to convert this distance into metres, feet, inches and centimeters
and display the results.
-----------------------------Script---------------------------
echo -e "Enter the Distance Between 2 cities : \c"
read distance
meter=`expr $distance \* 1000`
centi=`expr $meter \* 100`
inch=`echo -e "scale=2 \n $centi / 2.5" | bc`
feet=`echo -e "scale=2 \n $inch * 12" | bc`
echo "Meter = " $meter
echo "Centimeter = " $centi
echo "Inch = " $inch
echo "Feet = " $feet
-----------------------------output---------------------------
Enter the Distance Between 2 cities : 2
Meter = 2000
Centimeter = 200000
Inch = 80000.00
Feet = 960000.00
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3)
-----------------------------------------------------------------------------------------
#The length and breadth of a rectangle and radius of a circle are entered
through the keyboard. Calculate the perimeter and area of rectangle and area
and circumference of the circle.
-----------------------------Script---------------------------
echo "Give Details of the Rectangle"
echo -e "Enter Length : \c"
read length
echo -e "Enter Breadth : \c"
read breadth
area=`echo -e "scale=2 \n $length * $breadth" | bc`
para=`echo -e "scale=2 \n 2 * ($length + $breadth)" | bc`
echo "Area = " $area
echo "Parameter = " $para
echo "------------------------------"
echo "Give details of the Circle"
echo -e "Enter Radius : \c"
read radius
area=`echo -e "scale=2 \n $radius * $radius * 3.14" | bc`
cirum=`echo -e "scale=2 \n $radius * 2 * 3.14" | bc`
echo "Area = " $area
echo "Circumference = " $cirum
-----------------------------output---------------------------
Give Details of the Rectangle
Enter Length : 2
Enter Breadth : 2
Area = 4
Parameter = 8
------------------------------
Give details of the Circle
Enter Radius : 10
Area = 314.00
Circumference = 62.80
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
4)
-------------------------------------------------------------------------------------
#If a five digit number is entered through keyboard, calculate the sum of
its digits.
-----------------------------Script---------------------------
echo -e "Please enter a 5 digit number : \c"
read number
len=`expr length $number`
if [ $len -ne 5 ]
then
echo "Sorry the digit entered is not of 5 digit"
exit
fi
sum=0
i=1
while [ $i -le 5 ]
do
digit=`expr substr $number $i 1`
sum=`expr $sum + $digit`
i=`expr $i + 1`
done
echo "Sum of the digits = " $sum
-----------------------------output---------------------------
Please enter a 5 digit number : 12345
Sum of the digits = 15
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
5)
--------------------------------------------------------------------------------------
#The file /etc/passwd contains info about all users. Write a program which
would receive the logname during execution, obtain information about it from
the file and display the information on screen in some appropriate format.
(Hint : Use cut)
-----------------------------Script---------------------------
echo "Enter the logname : "
read lname
uid=`grep $lname /etc/passwd | cut -d ":" -f 3`
echo "User Id is : " $uid
gid=`grep $lname /etc/passwd | cut -d ":" -f 4`
echo "Group Id is : " $gid
dwd=`grep $lname /etc/passwd | cut -d ":" -f 6`
echo "Default Working Directry is : " $dwd
dws=`grep $lname /etc/passwd | cut -d ":" -f 7`
echo "Default Working directory is : " $dws
-----------------------------output---------------------------
Enter the logname :
mca114
User Id is : 680
Group Id is : 680
Default Working Directry is : /home/dca19
Default Working directory is : /bin/bash
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
6)
-------------------------------------------------------------------------------------------
#The script will receive the filename or filename with its full path. The
script should obtain information about this file as given by "ls -l" and
display it in proper format.
e.g. - Filname : , File access permission : , Number of links : ,
Owner of the file : , Group to which belongs : , Size of file : ,
File modification date : , File modification time :
sh sh6.sh out.sh
-----------------------------Script---------------------------
str=`ls -l $1`
echo "file Name : " $1
set -- $str
echo "File permissions : " $1
echo "Number of links : " $2
echo "Owner of the file : " $3
echo "Group : " $4
echo "Size of file : " $5
-----------------------------output---------------------------
file Name : out.sh
File permissions : -rwxrwxrwx
Number of links : 1
Owner of the file : dca19
Group : dca19
Size of file : 242
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
7)
------------------------------------------------------------------------------------
#If cost price and selling price of an item are entered through keyboard, write
a program to determine whether the seller has made profit or loss. Also
determine how much profit / loss is made.
-----------------------------Script---------------------------
echo -e "Enter Cost Price : \c"
read cost
echo -e "Enter Sales Price : \c"
read sale
diff=`echo -e "scale=2 \n $sale - $cost" | bc`
if [ $diff -gt 0 ]
then
echo "Profit = " $diff
elif [ $diff -lt 0 ]
then
echo "loss = " $diff
else
echo "No profit No loss"
fi
-----------------------------output---------------------------
Enter Cost Price : 100
Enter Sales Price : 120
Profit = 20
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
8)
-------------------------------------------------------------------------------
#Check whether the entered no. is odd or even.
-----------------------------Script---------------------------
mod=`expr $1 % 2 `
if [ $mod = 0 ]
then
echo "This is an even number"
else
echo "This is an odd number"
fi
-----------------------------output---------------------------
This is an even number
-----------------------------END----------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
9)
-----------------------------------------------------------------------------------------
#Check whether the entered number is prime or not.
-----------------------------Script---------------------------
echo "Enter a number : "
read num
div=`expr $num / 2`
while [ $div -gt 1 ]
do
if [ `expr $num % $div` -eq 0 ]
then
echo "Not Prime"
exit
fi
div=`expr $div - 1`
done
echo "Prime Number"
-----------------------------output---------------------------
Enter a number :
13
Prime Number
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
10)
------------------------------------------------------------------------------------
#Check whether the entered year is a leap year or not.
-----------------------------Script---------------------------
echo "Enter the Year : "
read year
if [ `expr $year % 4` -eq 0 ] && [ `expr $year % 100` -ne 0 ] || [ `expr $year %
400` -eq 0 ]
then
echo "The Year is a Leap year"
else
echo "The Year is not Leap Year"
fi
-----------------------------output---------------------------
Enter the Year :
2000
The Year is a Leap year
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/*--------------------------------------------------------------------------*/
11)
#Write a shell script which deletes all the lines containing a specified word
#in one or more files supplied as arguments to it.
-----------------------------Script---------------------------
echo -e "Give word to suppress line: \c"
read wrd
while [ $# -ge 1 ]
do
grep -v $wrd $1 > tmp.txt
cp tmp.txt $1
shift
done
-----------------------------output---------------------------
cat tmp.tmp
1
2
3
4
5
6
7
8
9
10
Give word to suppress line: 5
cat tmp.tmp
1
2
3
4
6
7
8
9
10
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
12)
/*--------------------------------------------------------------------------*/
#Write a script that reports in the descending order of their size, names and
#sizes of all files whose size exceeds 1000 bytes, in a specified directory
#(supplied as an argument). The total no. of search files should also be
#displayed.
-----------------------------Script---------------------------
list=`ls -S $1 `
i=0
for file in $list
do
det=`ls -l $1/$file `
size=`echo $det | cut -d " " -f5`
if [ $size -gt 40 ]
then
echo $file
fi
i=`expr $i + 1`
done
echo "No. of file : " $i
-----------------------------output---------------------------
sh sh42.sh scripts
script.txt
sh47.sh
sh34.sh
sh3.sh
sh38.sh
sh49.sh
sh5.sh
sh39.sh
sh43.sh
sh4.sh
sh2.sh
sh46.sh
sh48.sh
shtest.sh
sh1.sh
sh7.sh
out.sh
sh44.sh
sh45.sh
sh12.sh
sh10.sh
sh9.sh
sh42.sh
sh6.sh
sh15.sh
sh50.sh
sh14.sh
sh41.sh
sh11.sh
sh13.sh
sh37.sh
sh40.sh
sh8.sh
t1.sh
ftest1.txt
No. of file : 37
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
13)
/*--------------------------------------------------------------------------*/
#Write a shell script that accepts a list of filenames as its arguments, counts
#and reports the occurrence of each word that is present in the first argument
#file on other argument files.
-----------------------------Script---------------------------
if [ $# -lt 2 ]
then
echo "enter atleast two arguement"
exit
fi
terminal=`tty`
exec < $1
shift 1
while read line
do
echo "searching word :- $line"
for i in $*
do
grep $line $i
done
done
exec < $terminal
-----------------------------output---------------------------
sh script43.sh word.txt script12.sh script23.sh script29.sh
searching word :- this
searching word :- is
echo " yes he is login"
echo " NO, he is not login"
echo "friend is login"
echo "file name is :- $i"
searching word :- if
if [ $? -eq 0 ]
if [ $? -eq 0 ]
if [ $? -eq 0 ]
searching word :- do
do
done
do
done
searching word :- done
done
done
-----------------------------END------------------------------
14 )
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/*--------------------------------------------------------------------------*/
#Write a script that takes certain filenames as its arguments and searches for
#a specific word on these files one by one. It stops as soons as the search
#word is found on a file and reports the name of the file. In case the search
#word is not found in any of the files, a suitable message should be displayed.
-----------------------------Script---------------------------
echo -e "Enter the word to search for : /c"
read sword
for i in $*
do
grep $sword $i > /dev/null
if [ $? -eq 0 ]
then
echo "file name is :- $i"
exit
fi
done
-----------------------------output---------------------------
sh script44.sh script2.sh script24.sh UnixTest.txt
Enter the word to search for : /c
UNIX
file name is :- UnixTest.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
15)
/*--------------------------------------------------------------------------*/
#Write a script that accepts any number of arguments and prints them in
#reverse order.
-----------------------------Script---------------------------
echo "" >tmp.tmp
count=0
for i in $*
do
echo -e $i " " >>tmp.tmp
count=`expr $count + 1`
done
j=0
while [ $j -lt $count ]
do
j=`expr $j + 1`
line=`tail -n$j tmp.tmp`
line=`echo $line | cut -d " " -f1`
echo $line
done
-----------------------------output---------------------------
4
3
2
1
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
16)
/*--------------------------------------------------------------------------*/
#Write a script that reports the logging in of a specified user within one
#minute after he / she logs in. The script automatically terminates if the
#specified user does not login during a specified period of time.
-----------------------------Script---------------------------
echo -e "Please enter the username : \c"
read username
echo -e "Enter the Period to search for : \c"
read period
ctime=`date`
count=0
while [ $count -lt $period ]
do
count=`expr $count + 1 `
who | grep $username > /dev/null
if [ $? -eq 0 ]
then
echo "User has logged in"
exit
fi
sleep 60
done
-----------------------------output---------------------------
Please enter the username : mca114
Enter the Period to search for : 2
User has logged in
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
17)
/*--------------------------------------------------------------------------*/
#Write a script that determines the period for which a specified user is
#working on the system.
-----------------------------Script---------------------------
echo -e "Give user name: \c"
read uname
temp=`who | grep $uname`
echo $temp
tm=`echo $temp | cut -d " " -f5`
dt=`date +%T`
thh=`echo $tm | cut -d ":" -f1`
tmm=`echo $tm | cut -d ":" -f2`
dhh=`echo $dt | cut -d ":" -f1`
dmm=`echo $dt | cut -d ":" -f2`
echo tm : $tm dt: $dt thh: $thh tmm: $tmm dhh: $dhh dmm: $dmm
if [ $dhh -lt $thh ] ; then
dhh=`expr $dhh + 12 `
fi
thh=`expr $thh \* 60 `
dhh=`expr $dhh \* 60 `
thh=`expr $thh + $tmm `
dhh=`expr $dhh + $dmm `
diff=`expr $dhh - $thh `
diffhh=`expr $diff / 60 `
diff=`expr $diff % 60 `
echo differance:- $diffhh : $diff
-----------------------------output---------------------------
Give user name: mca114
mca114 pts/1 Jan 11 02:58 (192.168.1.106) mca114 pts/6 Jan 11 06:55 (192.168.1.2
05)
tm : 02:58 dt: 07:12:59 thh: 02 tmm: 58 dhh: 07 dmm: 12
differance:- 4 : 14
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
18)
/*--------------------------------------------------------------------------*/
#Create a front screen for Payroll system.
-----------------------------Script---------------------------
clear
tput cup 5 28
tput bold
echo -e "\033[7mWelcome to Payroll system"
echo -e "\033[7m "
tput cup 10 28
echo "1. Add Employee"
tput cup 12 28
echo "2. Edit Employee"
tput cup 14 28
echo "3. Generate Payslip"
tput cup 16 28
echo "4. Exit"
echo -e "\033[0m "
-----------------------------output---------------------------
Welcome to Payroll system
1. Add Employee
2. Edit Employee
3. Generate Payslip
4. Exit
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
19)
/*--------------------------------------------------------------------------*/
#Write a script that receives certain number of filenames as arguments and
#checks if such files already exist in the present working directory. If they
#do, then it will be reported. if any of these files do not exist then it
#checks whether a sub-directory callled mydir exists in the current directory
#or not. If it does not exist then it will be created and in it the files
#supplied as arguments and those that do not already exist in the pwd are
#created. If mydir already exists then it should be reported along with the
#number of files that are currently present in the mydir.
-----------------------------Script---------------------------
if [ $# -eq 0 ]
then
echo "please give the arguements."
else
for file in $*
do
ls $file > /dev/null
if [ $? -eq 0 ]
then
echo "file $file exist"
else
ls mydir > /dev/null
if [ $? -eq 1 ]
then
mkdir mydir
fi
echo "" > mydir/$file
fi
done
fi
-----------------------------output---------------------------
[mca112@rcclinux mca112]$ cd mydir
[mca112@rcclinux mydir]$ ls
tmp.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
20)
/*--------------------------------------------------------------------------*/
#Write a script to read a string. If it does not have 10 characters, then
#display appropriate message.
-----------------------------Script---------------------------
echo -e "Give string: \c"
read str
tm=`expr length $str`
if [ $tm -ne 10 ] ; then
echo Give 10 character string
else
echo Correct string
fi
-----------------------------output---------------------------
Give string: 1234567890
Correct string
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
21)
----------------------------------------------------------------------------
. Write a script which has the functionality similar to head and tail.
-----------------------------Script---------------------------
if [ $# -eq 3 ]
then
n=$2
file=$3
elif [ $# -eq 1 ]
then
n=10
file=$1
else
exit
fi
i=$n
echo "Please give choice "
echo "1 Head"
echo "2 Tail"
read choice
terminal=`tty`
exec < $file
if [ $choice -eq 2 ]
then
len=`cat $file | wc -l`
dif=`expr $len - $n`
while [ $dif -gt 0 ]
do
read line
dif=`expr $dif - 1`
done
fi
while read line
do
i=`expr $i - 1`
if [ $i -lt 0 ]
then
exit
fi
echo $line
done
exec < $terminal
-----------------------------output---------------------------
Please give choice
1 Head
2 Tail
2
bhavesh jadav
payal shah
sonal mishra
dayna d'costa
hetal bhalsod
kaveri singh
mayur desai
ananad panchal
priynaka panchal
ruchi mehta
-----------------------------END------------------------------
-----------------------------output---------------------------
Please give choice
1 Head
2 Tail
1
vishal patel
dharti patel
neha patel
ankit joshi
alpesh masani
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
22)
-------------------------------------------------------------------------------------
Write a script which reports name and size of all files in a directory, whose
size exceeds 1000. The filenames should be printed in the descending order of
their sizes. The total no. of files must be reported.
-----------------------------Script---------------------------
count=0
x=`ls -S`
for i in $x
do
fil=`ls -lS $i`
size=`echo $fil | cut -d ' ' -f5`
if [ $size -gt 300 ]
then
echo $i $size
count=`expr $count + 1`
fi
done
echo "total no of file $count"
-----------------------------output---------------------------
script5.sh 371
script17.sh 363
script7.sh 319
script2.sh 305
total no of file 4
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
23)
----------------------------------------------------------------------------------
-#A friend of yours has promsied to log in a particular time. You want to
contact him as soon as he logs in. Write a script which checks after every
minute whether the friend has logged in or not. The logname should be supplied
at command prompt.
-----------------------------Script---------------------------
echo "enter friend's login id"
read fid
while true
do
who | grep $fid> /dev/null
if [ $? -eq 0 ]
then
echo "friend is loggedin"
exit
else
echo "friend not loggedin"
fi
sleep 30
done
-----------------------------output---------------------------
enter friend's login id
dca21
friend is loggedin
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
24)
--------------------------------------------------------------------------
#Print the prime nos. from 1 to 300.
-----------------------------Script---------------------------
no=3
echo "2"
while [ $no -le 100 ]
do
i=2
flag=1
while [ $i -ne $no ] && [ $flag -eq 1 ]
do
if [ `expr $no % $i` -eq 0 ]
then
flag=0
else
i=`expr $i + 1`
fi
done
if [ $flag -eq 1 ]
then
echo $no
fi
no=`expr $no + 1`
done
-----------------------------output---------------------------
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
25)
--------------------------------------------------------------------
#Display all the combinations of 1, 2 and 3.
-----------------------------Script---------------------------
for i in 1 2 3
do
for j in 1 2 3
do
for k in 1 2 3
do
echo " $i $j $k"
done
done
done
-----------------------------output---------------------------
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
26)
-------------------------------------------------------------------------------
#Write a script for renaming each file in the directory such that it will have
the current shell PID as an extension. The shell script should ensure that
the directories do not get renamed.
-----------------------------Script---------------------------
cshell=`echo $SHELL | cut -d "/" -f3`
cpid=`ps | grep $cshell | cut -d " " -f1`
flist=`ls`
for file in $flist
do
if [ -f $file ]
then
mv $file $file.$cpid
fi
done
-----------------------------output---------------------------
ls
sh26.sh tf1.txt tf2.txt tmp1 tmp2 tmp3
sh sh26.sh
ls
sh26.sh.21491 tf1.txt.21491 tf2.txt.21491 tmp1 tmp2 tmp3
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
27)
------------------------------------------------------------------------------------
#A file called wordfile consists of several words. Write a shell script which
will receive a list of filenames, the first of which would be wordfile. The
shell script should report all occurences of each word in wordfile in the rest
of the files supplied as arguments.
-----------------------------Script---------------------------
if [ $# -lt 2 ]
then
echo "enter atleast two arguement"
exit
fi
terminal=`tty`
if [ $1 = word.txt ]
then
exec < $1
shift 1
while read line
do
echo "searching word :- $line"
for i in $*
do
grep $line $i
done
done
else
echo "first argument must be word file"
exit
fi
-----------------------------output---------------------------
sh script27.sh word.txt script12.sh script23.sh script29.sh
searching word :- this
searching word :- is
echo " yes he is login"
echo " NO, he is not login"
echo "friend is login"
echo "file name is :- $i"
searching word :- if
if [ $? -eq 0 ]
if [ $? -eq 0 ]
if [ $? -eq 0 ]
searching word :- do
do
done
do
done
searching word :- done
done
done
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
28)
-----------------------------------------------------------------------------------
#Write a shell script which deletes all the lines containing the word "unix"
in the files supplied as arguments.
-----------------------------Script---------------------------
for i in $*
do
echo $i
grep -v "unix" $i > temp.txt
cat temp.txt > $i
done
-----------------------------output---------------------------
$ cat UnixTest
This is a unix test
the word
unix is not present here
lets see if it works
unix is present here
not here
$ cat UnixTest
the word
lets see if it works
not here
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
29)
----------------------------------------------------------------------------------
#The word "unix" is present in only some of the files supplied as arguments to
the shell script. Your script should search each of these files in turn and
stop at the first file that it encounters containing the word unix. The
filename should be displayed on the screen.
-----------------------------Script---------------------------
for i in $*
do
grep "unix" $i > /dev/null
if [ $? -eq 0 ]
then
echo "file name is :- $i"
exit
fi
done
-----------------------------output---------------------------
sh script29.sh script2.sh script24.sh UnixTest.txt
file name is :- UnixTest.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
30)
--------------------------------------------------------------------------------
#A shell script receives even number of filenames. Suppose four filenames are
supplied then the first should get copied into second file, the third file
should get copied into fourth and so on. If odd number of filenames are
supplied display error message.
-----------------------------Script---------------------------
if [ `expr $# % 2` -eq 1 ]
then
echo "odd no of argument"
exit
else
i=1
while [ $i -lt $# ]
do
if [ -f $2 -a -f $1 ]
then
cp $1 $2
echo $1 $2
shift 2
fi
done
fi
-----------------------------output---------------------------
u1.txt u3.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
31)
----------------------------------------------------------------------------------------
The script displays a list of all files in the current directory to which you
have read, write and execute permissions.
-----------------------------Script---------------------------
for file in *.*
do
if [ -f $file ]
then
ls -l $file | cut -d '-' -f2 | grep rwx > /dev/null
if [ $? -eq 0 ] ; then
echo $file
fi
fi
done
-----------------------------output---------------------------
out.sh
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
32)
--------------------------------------------------------------------------------------
#The script receives any number of filenames as arguments. It should check
whether every argument supplied is a file or directory. If it is a directory
it should be reported. If it is a filename then name of the file as well as
the number of lines present in it should be reported.
-----------------------------Script---------------------------
while [ $# -ge 1 ]
do
if [ -f $1 ] ; then
echo "file name: $1 line: `cat $1 |wc -l `"
else
echo it is a directory $1
fi
shift
done
-----------------------------output---------------------------
file name: ass11.sh line: 11
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
33)
-----------------------------------------------------------------------------
#A script will receive any number of filenames as arguments. It should check
whether such files exist. If they do, then it should be reported, if not
then check if a subdirectory "mydir" exists of not in the current directory,
if it doesn't exist then it should be created and in it the files supplied
as arguments should be created.
-----------------------------Script---------------------------
if [ $# -eq 0 ]
then
echo "please give the arguements."
else
for file in $*
do
ls $file > /dev/null
if [ $? -eq 0 ]
then
echo "file $file exist"
else
ls mydir > /dev/null
if [ $? -eq 1 ]
then
mkdir mydir
fi
echo "" > mydir/$file
fi
done
fi
-----------------------------output---------------------------
[mca112@rcclinux mca112]$ cd mydir
[mca112@rcclinux mydir]$ ls
tmp.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
34)
-----------------------------------------------------------------------------
#Accept the marks of 5 subjects and calculate the percentage and grade.
-----------------------------Script---------------------------
i=1
total=0
grade="pass"
while [ $i -lt 6 ]
do
echo -e "Enter Marks of Subject " $i " \c"
read marks
if [ $marks -lt 40 ]
then
grade="fail"
fi
total=`expr $total + $marks`
i=`expr $i + 1`
done
percentage=`echo -e "$total / 5 " | bc`
if [ $grade = "pass" ]
then
if [ $percentage -lt 50 ]
then
grade="3rd class"
elif [ $percentage -lt 60 ]
then
grade="2nd class"
elif [ $percentage -lt 70 ]
then
grade="1st class"
else
grade="Distinction"
fi
fi
echo -e "Total = " $total
echo "Percentage = " $percentage
echo "Grade = " $grade
-----------------------------output---------------------------
Enter Marks of Subject 1 50
Enter Marks of Subject 2 65
Enter Marks of Subject 3 50
Enter Marks of Subject 4 50
Enter Marks of Subject 5 50
Total = 265
Percentage = 53
Grade = 2nd class
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
35)
---------------------------------------------------------------------------
#Print Armstrong nos. from 1 to 500.
-----------------------------Script---------------------------
i=1
while [ $i -le 500 ]
do
cnt=1
total=0
while [ $cnt -le `expr length $i` ]
do
dgt=`expr substr $i $cnt 1`
dcube=`echo $dgt ^ 3 | bc`
total=`expr $dcube + $total`
cnt=`echo " $cnt + 1" | bc`
done
if [ $total -eq $i ]
then
echo Number $i is armstrong numbner
fi
i=`expr $i + 1 `
done
-----------------------------output---------------------------
Number 1 is armstrong numbner
Number 153 is armstrong numbner
Number 370 is armstrong numbner
Number 371 is armstrong numbner
Number 407 is armstrong numbner
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
36)
----------------------------------------------------------------------------------
#Accept the measure (angles) of a triangle and display the type of triangle.
(e.g. - Acute, Right, Obtuse).
-----------------------------Script---------------------------
echo -e "Give angle A: \c"
read a
echo -e "Give angle B: \c"
read b
echo -e "Give angle C: \c"
read c
if [ `expr $a + $b + $c` -eq 180 ] ; then
if [ $a -eq 90 -o $b -eq 90 -o $c -eq 90 ]
then
echo "Given triangle is Right angle"
elif [ $a -eq 60 -a $b -eq 60 -a $c -eq 60 ]
then
echo "Given triangle is equilator"
elif [ $a -gt 90 -o $b -gt 90 -o $c -gt 90 ]
then
echo "Given triangle is obtuse"
else
echo "Given triangle is acute"
fi
else
echo "Invalid angles"
fi
-----------------------------output---------------------------
Give angle A: 60
Give angle B: 60
Give angle C: 60
Given triangle is equilator
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
37)
----------------------------------------------------------------------------
#Display all the numbers from 1 to 100 which are divisible by 7.
-----------------------------Script---------------------------
i=0
while [ $i -le 100 ]
do
if [ `expr $i % 7 ` -eq 0 ] ; then
echo $i
fi
i=`expr $i + 1`
done
-----------------------------output---------------------------
0
7
14
21
28
35
42
49
56
63
70
77
84
91
98
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
38)
--------------------------------------------------------------------------------
#Find the largest and smallest of 3 different numbers.
-----------------------------Script---------------------------
echo -e "Give number1: \c"
read num1
echo -e "Give number2: \c"
read num2
echo -e "Give number3: \c"
read num3
if [ $num1 -gt $num2 ] ; then
if [ $num1 -gt $num3 ] ; then
echo NUMBER 1: $num1 is greater
else
echo NUMBER 3: $num3 is greater
fi
else
if [ $num2 -gt $num3 ] ; then
echo NUMBER 2 : $num2 is greater
else
echo NUMBER 3 : $num3 is greater
fi
fi
-----------------------------output---------------------------
Give number1: 10
Give number2: -20
Give number3: 15
NUMBER 3: 15 is greater
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
39)
------------------------------------------------------------------------
#Find H.C.F. and L.C.M. of given 2 numbers.
-----------------------------Script---------------------------
echo -e "Give number: \c "
read num1
echo -e "Give number: \c"
read num2
mul=`expr $num1 \* $num2`
div=2
LCM=1
while [ $div -le $mul ]
do
if [ `expr $div % $num1` -eq 0 -a `expr $div % $num2 ` -eq 0 ]
then
LCM=$div
break
fi
div=`expr $div + 1`
done
hcf=`expr $mul / $LCM`
echo LCM $LCM
echo HCF $hcf
-----------------------------output---------------------------
Give number: 15
Give number: 25
LCM 75
HCF 5
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
40)
---------------------------------------------------------------------------
#Display the dates falling on Sundays of the current month.
-----------------------------Script---------------------------
if [ $# -eq 0 ] ; then
cal | tail -n6 | cut -c1,2
else
cal $1 $2 |tail -n6 |cut -c1,2
fi
-----------------------------output---------------------------
6
13
20
27
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/*--------------------------------------------------------------------------*/
41)
-------------------------------------------------------------------------------
#Write a shell script which deletes all the lines containing a specified word
in one or more files supplied as arguments to it.
-----------------------------Script---------------------------
echo -e "Give word to suppress line: \c"
read wrd
while [ $# -ge 1 ]
do
grep -v $wrd $1 > tmp.txt
cp tmp.txt $1
shift
done
-----------------------------output---------------------------
cat tmp.tmp
1
2
3
4
5
6
7
8
9
10
Give word to suppress line: 5
cat tmp.tmp
1
2
3
4
6
7
8
9
10
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
42)
/*--------------------------------------------------------------------------*/
#Write a script that reports in the descending order of their size, names and
sizes of all files whose size exceeds 1000 bytes, in a specified directory
(supplied as an argument). The total no. of search files should also be
displayed.
-----------------------------Script---------------------------
list=`ls -S $1 `
i=0
for file in $list
do
det=`ls -l $1/$file `
size=`echo $det | cut -d " " -f5`
if [ $size -gt 40 ]
then
echo $file
fi
i=`expr $i + 1`
done
echo "No. of file : " $i
-----------------------------output---------------------------
sh sh42.sh scripts
script.txt
sh47.sh
sh34.sh
sh3.sh
sh38.sh
sh49.sh
sh5.sh
sh39.sh
sh43.sh
sh4.sh
sh2.sh
sh46.sh
sh48.sh
shtest.sh
sh1.sh
sh7.sh
out.sh
sh44.sh
sh45.sh
sh12.sh
sh10.sh
sh9.sh
sh42.sh
sh6.sh
sh15.sh
sh50.sh
sh14.sh
sh41.sh
sh11.sh
sh13.sh
sh37.sh
sh40.sh
sh8.sh
t1.sh
ftest1.txt
No. of file : 37
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
43)
/*--------------------------------------------------------------------------*/
#Write a shell script that accepts a list of filenames as its arguments, counts
and reports the occurrence of each word that is present in the first argument
file on other argument files.
-----------------------------Script---------------------------
if [ $# -lt 2 ]
then
echo "enter atleast two arguement"
exit
fi
terminal=`tty`
exec < $1
shift 1
while read line
do
echo "searching word :- $line"
for i in $*
do
grep $line $i
done
done
exec < $terminal
-----------------------------output---------------------------
sh script43.sh word.txt script12.sh script23.sh script29.sh
searching word :- this
searching word :- is
echo " yes he is login"
echo " NO, he is not login"
echo "friend is login"
echo "file name is :- $i"
searching word :- if
if [ $? -eq 0 ]
if [ $? -eq 0 ]
if [ $? -eq 0 ]
searching word :- do
do
done
do
done
searching word :- done
done
done
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
44)
/*--------------------------------------------------------------------------*/
#Write a script that takes certain filenames as its arguments and searches for
a specific word on these files one by one. It stops as soons as the search
word is found on a file and reports the name of the file. In case the search
word is not found in any of the files, a suitable message should be displayed.
-----------------------------Script---------------------------
echo -e "Enter the word to search for : /c"
read sword
for i in $*
do
grep $sword $i > /dev/null
if [ $? -eq 0 ]
then
echo "file name is :- $i"
exit
fi
done
-----------------------------output---------------------------
sh script44.sh script2.sh script24.sh UnixTest.txt
Enter the word to search for : /c
UNIX
file name is :- UnixTest.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
45)
/*--------------------------------------------------------------------------*/
#Write a script that accepts any number of arguments and prints them in
reverse order.
-----------------------------Script---------------------------
echo "" >tmp.tmp
count=0
for i in $*
do
echo -e $i " " >>tmp.tmp
count=`expr $count + 1`
done
j=0
while [ $j -lt $count ]
do
j=`expr $j + 1`
line=`tail -n$j tmp.tmp`
line=`echo $line | cut -d " " -f1`
echo $line
done
-----------------------------output---------------------------
4
3
2
1
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
46)
/*--------------------------------------------------------------------------*/
#Write a script that reports the logging in of a specified user within one
minute after he / she logs in. The script automatically terminates if the
specified user does not login during a specified period of time.
-----------------------------Script---------------------------
echo -e "Please enter the username : \c"
read username
echo -e "Enter the Period to search for : \c"
read period
ctime=`date`
count=0
while [ $count -lt $period ]
do
count=`expr $count + 1 `
who | grep $username > /dev/null
if [ $? -eq 0 ]
then
echo "User has logged in"
exit
fi
sleep 60
done
-----------------------------output---------------------------
Please enter the username : mca114
Enter the Period to search for : 2
User has logged in
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
47)
/*--------------------------------------------------------------------------*/
#Write a script that determines the period for which a specified user is
working on the system.
-----------------------------Script---------------------------
echo -e "Give user name: \c"
read uname
temp=`who | grep $uname`
echo $temp
tm=`echo $temp | cut -d " " -f5`
dt=`date +%T`
thh=`echo $tm | cut -d ":" -f1`
tmm=`echo $tm | cut -d ":" -f2`
dhh=`echo $dt | cut -d ":" -f1`
dmm=`echo $dt | cut -d ":" -f2`
echo tm : $tm dt: $dt thh: $thh tmm: $tmm dhh: $dhh dmm: $dmm
if [ $dhh -lt $thh ] ; then
dhh=`expr $dhh + 12 `
fi
thh=`expr $thh \* 60 `
dhh=`expr $dhh \* 60 `
thh=`expr $thh + $tmm `
dhh=`expr $dhh + $dmm `
diff=`expr $dhh - $thh `
diffhh=`expr $diff / 60 `
diff=`expr $diff % 60 `
echo differance:- $diffhh : $diff
-----------------------------output---------------------------
Give user name: mca114
mca114 pts/1 Jan 11 02:58 (192.168.1.106) mca114 pts/6 Jan 11 06:55 (192.168.1.2
05)
tm : 02:58 dt: 07:12:59 thh: 02 tmm: 58 dhh: 07 dmm: 12
differance:- 4 : 14
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
48)
/*--------------------------------------------------------------------------*/
#Create a front screen for Payroll system.
-----------------------------Script---------------------------
clear
tput cup 5 28
tput bold
echo -e "\033[7mWelcome to Payroll system"
echo -e "\033[7m "
tput cup 10 28
echo "1. Add Employee"
tput cup 12 28
echo "2. Edit Employee"
tput cup 14 28
echo "3. Generate Payslip"
tput cup 16 28
echo "4. Exit"
echo -e "\033[0m "
-----------------------------output---------------------------
Welcome to Payroll system
1. Add Employee
2. Edit Employee
3. Generate Payslip
4. Exit
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
49)
/*--------------------------------------------------------------------------*/
#Write a script that receives certain number of filenames as arguments and
checks if such files already exist in the present working directory. If they
do, then it will be reported. if any of these files do not exist then it
checks whether a sub-directory callled mydir exists in the current directory
or not. If it does not exist then it will be created and in it the files
supplied as arguments and those that do not already exist in the pwd are
created. If mydir already exists then it should be reported along with the
number of files that are currently present in the mydir.
-----------------------------Script---------------------------
if [ $# -eq 0 ]
then
echo "please give the arguements."
else
for file in $*
do
ls $file > /dev/null
if [ $? -eq 0 ]
then
echo "file $file exist"
else
ls mydir > /dev/null
if [ $? -eq 1 ]
then
mkdir mydir
fi
echo "" > mydir/$file
fi
done
fi
-----------------------------output---------------------------
[mca112@rcclinux mca112]$ cd mydir
[mca112@rcclinux mydir]$ ls
tmp.txt
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
50)
/*--------------------------------------------------------------------------*/
#Write a script to read a string. If it does not have 10 characters, then
display appropriate message.
-----------------------------Script---------------------------
echo -e "Give string: \c"
read str
tm=`expr length $str`
if [ $tm -ne 10 ] ; then
echo Give 10 character string
else
echo Correct string
fi
-----------------------------output---------------------------
Give string: 1234567890
Correct string
-----------------------------END------------------------------
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1)
Write a shell script to accept numbers and perform addition, subtraction, division and multiplication.
echo "Enter Number 1 : "
read a
echo "Enter Number 2 : "
read b
ans=`expr $a + $b`
echo "Addition : " $ans
ans=`expr $a - $b`
echo "Subtraction : " $ans
ans=`expr $a / $b`
echo "Division : " $ans
ans=`expr $a \* $b`
echo "Multiplication : " $ans
=================OUTPUT====================================
"dca22.sh" 17L, 251C written
[dca1621@agni ~]$ sh dca22.sh
Enter Number 1 :
22
Enter Number 2 :
2
Addition : 24
Subtraction : 20
Division : 11
Multiplication : 44
[dca1621@agni ~]$
================================================================
2)
Write a shell script to accept the string and checks whether the string is palindrome or not.
clear
echo "Enter String : \c"
read str
len=`echo $str|wc -c`
len=`expr $len - 1`
echo "length of String : "$len
i=1
while [ $i -le $len ]
do
revstr=`echo $str|cut -c$i`$revstr
i=`expr $i + 1`
done
if [ "$revstr" = "$str" ]
then
echo "Your string is palindrome"
else
echo "Your string is not palindrome"
fi
--------------OUTPUT-----------------------
"dca22.sh" [New] 22L, 310C written
[dca1621@agni ~]$ sh dca22.sh
dca22.sh: line 1: lear: command not found
Enter String : \c
vishal
length of String : 6
Your string is not palindrome
"dca22.sh" [New] 22L, 310C written
[dca1621@agni ~]$ sh dca22.sh
dca22.sh: line 1: lear: command not found
Enter String : \c
vishal
length of String : 6
Your string is not palindrome
[dca1621@agni ~]$ dhamo
bash: dhamo: command not found...
[dca1621@agni ~]$ sh dca22.sh
dca22.sh: line 1: lear: command not found
Enter String : \c
dharti
length of String : 6
Your string is not palindrome
[dca1621@agni ~]$
[dca1621@agni ~]$ sh dca22.sh
dca22.sh: line 1: lear: command not found
Enter String : \c
aaa
length of String : 3
Your string is palindrome
[dca1621@agni ~]$
-----------------------------------------------------
3)
Write a shell script to Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.
clear
echo "Enter Number : "
read no
count=0
total=0
if [ `expr $no % 2` -eq 0 ]
then
echo "NUMBER IS EVEN"
else
echo "NUMBER IS ODD"
fi
while [ $no -ne 0 ]
do
a=`expr $no % 10`
no=`expr $no / 10`
total=`expr $total + $a`
count=`expr $count + 1`
done
echo "Sum of All Digit : $total"
echo "Total No. of Digit : $count"
------------OUTPUT--------------------------------
"dca22.sh" 16L, 190C written
[dca1621@agni ~]$ sh dca22.sh
dca22.sh: line 1: dca22.sh: command not found
dca22.sh: line 2: [dca1621@agni: command not found
dca22.sh: line 3: Enter: command not found 11,18 All
dca22.sh: line 4: 22: command not found 10,21 All
dca22.sh: line 5: Enter: command not found 9,15 All
dca22.sh: line 6: 2: command not found 8,18 All
dca22.sh: line 7: Addition: command not found 7,15 All
dca22.sh: line 8: Subtraction: command not found 6,1 All
dca22.sh: line 9: Division: command not found 5,17 All
dca22.sh: line 10: Multiplication: command not found 4,3 All
dca22.sh: line 11: [dca1621@agni: command not found 3,17 All
[dca1621@agni ~]$ 2,30 All
---------------------------------------------------
4)
Accept strings and replace a string by another string
clear
echo "Enter string : \c"
read str
echo "\n\nWhat you want to search from [ $str ] : \c"
read sStr
echo "\n\nEnter string to replace : \c"
read rStr
str=`echo $str | sed s/$sStr/$rStr/`
echo "\n\nstring replaced successfully \n New string is : $str "
---------OUTOUT---------------------------
Enter string : \c
vishal
\n\nWhat you want to search from [ vishal ] : \c
s
\n\nEnter string to replace : \c
a
\n\nstring replaced successfully \n New string is : viahal
------------------------------------------
5)
Write a shell script to accept filename and displays last modification time if file exists, otherwise display appropriate message.
clear
echo "Enter filename"
read fileName
if [ -f $fileName ]
then
echo "Modification time of [ $fileName ] is \c "
ls -l $fileName | cut -c 37-41
else
echo "file [ $fileName ] not exist in \c "
pwd
fi
--------------OUTPUT----------------
Enter filename
dca22.sh
Modification time of [ dca22.sh ] is \c
4 1
[dca1621@agni ~]$
Enter filename
vishal.sh
file [ vishal.sh ] not exist in \c/home/dca1621 [dca1621@agni ~]$
-------------------------------------
6)
write a shell script to fetch the data from a file and display data into another file in reverse order.
echo "Enter File Name : \c "
read fileName
if [ -f $fileName ]
then
str=`cat $fileName`
len=`echo $str|wc -c`
i=$len
while [ $i -ge 1 ]
do
temp=$temp`echo $str|cut -c $i`
i=`expr $i - 1`
done
echo $temp>Result_ex2f.txt
cat Result_ex2f.txt
else
echo "file [ $fileName ] not exist in \c "
pwd
fi
---------------OUTPUT--------------------
Enter File Name : \c 8,19 All
dca21.shT -- 7,3 All
file [ dca21.sh ] not exist in \c 6,19 All
/home/dca1621 5,1 All
[dca1621@agni ~]$ sh dca22.sh 4,1 All
Enter File Name : \c 3,1 All
dca22.sh 2,13 All
if dwp " c\ ni tsixe ton ] emaNelif$ [ elif" ohce esle txt.f2xe_tluseR tac txt.f
2xe_tluseR>pmet$ ohce enod `1 - i$ rpxe`=i `i$ c- tuc|rts$ ohce`pmet$=pmet od ]
1 eg- i$ [ elihw nel$=i `c- cw|rts$ ohce`=nel `emaNelif$ tac`=rts neht ] emaNeli
f$ f- [ fi emaNelif daer " c\ : emaN eliF retnE" ohce
[dca1621@agni ~]$
--------------------------------------------
7)
Write a shell script to find the global complete path for any file.
clear
echo "Enter File Name : \c "
read fileName
if [ -f $fileName ]
then
str=`find $fileName`
path=`pwd`
echo "Full path of file is $path/$str"
else
echo "file [ $fileName ] not exist in \c "
pwd
fi
--------------------OUTPUT--------------------------------
Enter File Name : \c
dca22.sh
Full path of file is /home/dca1621/dca22.sh
[dca1621@agni ~]$
----------------------------------------------------------
8)
Write a shell script to broadcast a message to a specified user or a group of users logged on any terminal.
echo "Enter user Name."
read usrName
echo "\t\t\tNOTE : Press [ Ctrl+d ] To Broadcast"
write $usrName
-------------------OUTPUT---------------------------------
[dca1621@agni ~]$ sh dca.sh
Enter user Name.
vishal
\t\t\tNOTE : Press [ Ctrl+d ] To Broadcast
write: vishal is not logged in
------------------------------------------------------------
9)
Write a script to copy the file system from two directories to
a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
# read directory name
echo "Enter First Directory : \c"
read dir1
echo "Enter Second Dirctory : \c"
read dir2
# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
if [ ! -d $dir2 ]
then
echo "DIRECTORY [ $dir2 ] NOT EXIST"
exit 1
fi
# copy all file names in to a text file
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir TargetDir
for FileFromDir1 in `cat dir1.txt`
do
flag=0
for FileFromDir2 in `cat dir2.txt`
do
# compare file name is same or not
if [ "$FileFromDir1" = "$FileFromDir2" ]
then
#f file name from directory1 is match to file name of directory2 [ flage is true ]
flag=1
# short both file by time and select first file
record=`ls -lt $dir1/$FileFromDir1 $dir2/$FileFromDir2 | head -n 1`
# copy [ Dir/filename ] of selected file
filePath=`echo $record | cut -d " " -f 8`
# copy selected file to the tageted directory
cp $filePath TargetDir
fi
done
#if file name from directory1 is does NOT match to file name of directory2 [ copy to TargetDir ]
if [ $flag -eq 0 ]
then
cp $dir1/$FileFromDir1 TargetDir
fi
done
# copy rest of file from directory2 to TargetDir
for FileFromDir2 in `cat dir2.txt`
do
flag=0
for FileFromDir1 in `cat dir1.txt`
do
if [ "$FileFromDir2" = "$FileFromDir1" ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
cp $dir2/$FileFromDir2 TargetDir
fi
done
-------------------OUTPUT---------------------------------
Swap file ".dca1.sh.swp" already exists!
[dca1621@agni ~]$
[dca1621@agni ~]$ sh dca1.sh
dca1.sh: line 1: d: command not found
Enter First Directory : \c
vishal
Enter Second Dirctory : \c
patel
mkdir: cannot create directory `TargetDir': File exists
dca1.sh: line 57: unexpected EOF while looking for matching ``'
dca1.sh: line 58: syntax error: unexpected end of file
[dca1621@agni ~]$
------------------------------------------------------------
----------------------------------------------------
10)
Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
# read directory name
echo "Enter First Directory : \c"
read dir1
echo "Enter Second Dirctory : \c"
read dir2
# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
if [ ! -d $dir2 ]
then
echo "DIRECTORY [ $dir2 ] NOT EXIST"
exit 1
fi
# copy all file names in to a text file
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir TargetDir
for FileFromDir1 in `cat dir1.txt`
do
flag=0
for FileFromDir2 in `cat dir2.txt`
do
# compare file name is same or not
if [ "$FileFromDir1" = "$FileFromDir2" ]
then
#f file name from directory1 is match to file name of directory2 [ flage is true ]
flag=1
# short both file by time and select first file
record=`ls -lt $dir1/$FileFromDir1 $dir2/$FileFromDir2 | head -n 1`
# copy [ Dir/filename ] of selected file
filePath=`echo $record | cut -d " " -f 8`
# copy selected file to the tageted directory
cp $filePath TargetDir
fi
done
#if file name from directory1 is does NOT match to file name of directory2 [ copy to TargetDir ]
if [ $flag -eq 0 ]
then
cp $dir1/$FileFromDir1 TargetDir
fi
done
# copy rest of file from directory2 to TargetDir
for FileFromDir2 in `cat dir2.txt`
do
flag=0
for FileFromDir1 in `cat dir1.txt`
do
if [ "$FileFromDir2" = "$FileFromDir1" ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
cp $dir2/$FileFromDir2 TargetDir
fi
done
-------------------OUTPUT---------------------------------
"dcaa.sh" 77L, 1351C written
[dca1621@agni ~]$ sh dcaa.sh
dcaa.sh: line 1: d: command not found
Enter First Directory : \c
vishal
Enter Second Dirctory : \c
patel
mkdir: cannot create directory `TargetDir': File exists
[dca1621@agni ~]$
------------------------------------------------------------
11)
Write a shell script to delete zero sized files from a given directory (and all its sub-directories)
|
clear
echo "Enter Dirctory : \c"
read dir1
# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
#nevigate to given directory
cd $dir1
# create list of element we have in inputed directory
`echo ls`>fileList
#counter for sub directory
d=0
#counter for zero sized file
f=0
for fileName in `cat fileList`
do
if [ ! -d $fileName ]
then
#find the size of file
size=`ls -s $fileName`
size=`echo $size|cut -d " " -f 1`
if [ $size -eq 0 ]
then
f=`expr $f + 1`
rm $fileName
fi
else
# delete sub directory
d=`expr $d + 1`
rm -r $fileName
fi
done
t=`expr $f + $d`
echo "Total Deleted element : "$t
echo "Total Deleted file : "$f
echo "Total Deleted Sub directory : "$d
-------------------OUTPUT---------------------------------
Enter Dirctory :vishal
try
Total Deleted element : 0
Total Deleted file : 0
Total Deleted Sub directory : 0
[dca1622@agni ~]$
------------------------------------------------------------
12)
Write a script to display the name of those files (in the given directory) which are having multiple links.
clear
echo "Enter Directory : "
read dir
# check inputed name is directory or not
if [ ! -d $dir ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
#count total number of lines
len=`ls -l $dir | wc -l`
i=2
echo "File With Multiple Link are "
while [ $i -le $len ]
do
# select one by one record from entire group of records
record=`ls -l $dir | head -n $i | tail -n 1`
#grab the file name
f=`echo $record | cut -d " " -f 8`
#grab the total hard link to that file
link=`echo $record | cut -d " " -f 2`
#if multiple links then print file with link
if [ $link -gt 1 ]
then
echo "$f = $link"
fi
#increment the counter
i=`expr $i + 1`
done
-------------------OUTPUT---------------------------------
"dca1.sh" 36L, 641C written
[dca1619@agni ~]$ sh dca1.sh
Enter Directory :
dca19
DIRECTORY [ ] NOT EXIST
------------------------------------------------------------
13)
Write a script to display the name of all executable files in the given directory.
clear
echo "Enter Directory Name : "
read dir
ls $dir>fileList.txt
count=0
if [ -d $dir ]
then
for fileName in `cat fileList.txt`
do
if [ -x $dir/$fileName ]
then
echo "$fileName"
count=`expr $count + 1`
fi
done
fi
echo "Total Executable Files : $count"
rm fileList.txt
-------------------OUTPUT---------------------------------
~
~
~
"dca1.sh" 21L, 329C written
[dca1619@agni ~]$ sh dca1.sh
Enter Directory Name :
dir
ls: cannot access dir: No such file or directory
Total Executable Files : 0
------------------------------------------------------------
14)
#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.
currentHour=`date +%H`
currentTime=`date +"%I : %M : %S %p"`
if [ $currentHour -lt 12 ]
then
msg="Good Morning"
elif [ $currentHour -ge 12 ] && [ $currentHour -lt 16 ]
then
msg="Good Afternoon"
elif [ $currentHour -ge 16 ] && [ $currentHour -lt 21 ]
then
msg="Good Evening"
else
msg="Good Night"
fi
#format of time [ HH : MM : SS : AM/PM]
echo "$msg ,\nCurrent Time is $currentTime"
-------------------OUTPUT---------------------------------
~
~
~
"dca1.sh" 21L, 411C written
[dca1619@agni ~]$ sh dca1.sh
Good Morning ,\nCurrent Time is 11 : 31 : 36 AM
[dca1619@agni ~]$ currentHour=`date +%H`
------------------------------------------------------------
15)
Write a script to implement the following commands:Tree (of DOS) which (of UNIX)
Wednesday, November 07, 2012 | Posted by Bipin Rupadiya |
Tree (of DOS)
echo
if [ "$1" != "" ] #if parameter exists, use as base folder
then cd "$1"
fi
pwd
ls -R | grep ":$" | \
sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ `ls -F -1 | grep "/" | wc -l` = 0 ] # check if no folders
then echo " -> no sub-directories"
fi
echo
exit
-------------------OUTPUT---------------------------------
/home/dca1621
.
|-patel
|-TargetDir
|-vishal
[dca1621@agni ~]$
------------------------------------------------------------
16)
Write a script for generating a mark sheet after
reading data from a file. File contains student roll no, name , marks of three subjects.
studInfo.txt
hitesh,30,70,80
mitesh,30,80,40
jitesh,30,35,40
ritesh,100,100,100
Ex13.sh
clear
len=`cat studInfo.txt | wc -l`
i=1
echo "\n\n\t\t\t [ STUDENT MARKSHEET ]\n"
echo "__________________________________________________________________________"
echo "#\t NAME \t \t \t TOTAL \t \t PERCENTAGE \t GRADE "
echo "__________________________________________________________________________"
while [ $i -le $len ]
do
record=`head -n $i studInfo.txt | tail -n 1`
total=0
j=2
isFail=0
while [ $j -le 4 ]
do
marks=`echo $record | cut -d "," -f $j`
if [ $marks -lt 40 ]
then
isFail=1
fi
total=`expr $marks + $total`
j=`expr $j + 1`
done
name=`echo $record | cut -d "," -f 1`
per=`expr $total / 3`
if [ $isFail = 0 ]
then
if [ $per -ge 85 ] && [ $per -le 100 ]
then
grade="AA"
elif [ $per -ge 75 ] && [ $per -le 84 ]
then
grade="AB"
elif [ $per -ge 65 ] && [ $per -le 74 ]
then
grade="BB"
elif [ $per -ge 55 ] && [ $per -le 64 ]
then
grade="BC"
elif [ $per -ge 45 ] && [ $per -le 54 ]
then
grade="CC"
else
grade="FF"
fi
else
grade="FF"
fi
echo "$i \t $name \t \t $total \t \t $per % \t \t $grade"
i=`expr $i + 1`
done
echo "__________________________________________________________________________\n"
-------------------OUTPUT---------------------------------
"p117.sh" [New] 56L, 1466C written
[dca1621@agni ~]$ sh p117.sh
p117.sh: line 1: ear: command not found
\n\n\t\t\t [ STUDENT MARKSHEET ]\n
__________________________________________________________________________
#\t NAME \t \t \t TOTAL \t \t PERCENTAGE \t GRADE
__________________________________________________________________________
1 \t hitesh \t \t 180 \t \t 60 % \t \t FF
2 \t mitesh \t \t 150 \t \t 50 % \t \t FF
3 \t jitesh \t \t 105 \t \t 35 % \t \t FF
__________________________________________________________________________\n
[dca1621@agni ~]$
------------------------------------------------------------
17)
Write a script to make following file and directory management operations
OR
Write a script to make following file and directory management operations menu based:
Display current directory
List directory
Make directory
Change directory
Copy a file
Rename a file
Delete a file
Edit a file
File Name : ex14.sh
clear
choice=y
while [ "$choice" = "y" ]
do
echo "____________________________________"
echo " MAIN MENU "
echo "____________________________________"
echo "1 - DISPLAY CURRENT DIRECTORY"
echo "2 - LIST DIRECTORY"
echo "3 - MAKE DIRECTORY"
echo "4 - CHANGE DIRECTORY"
echo "5 - COPY A FILE"
echo "6 - RENAME A FILE"
echo "7 - DELETE A FILE"
echo "8 - EDIT A FILE"
echo "9 - EXIT"
echo "____________________________________"
echo "ENTER THE CHOICE :- "
read choice
case $choice in
1) pwd;;
2) ls -l;;
3) echo "Enter Directory Name "
read dir_name
mkdir $dir_name
echo "[ $dir_name ] Sucessfully Created Directory"
;;
4) echo "Enter the Absolute Path to change the directory: "
read apath
cd $apath
echo "Working path changed successfully!"
pwd
;;
5) echo "Enter name of file: "
read filename
echo "Copy where? "
read apath
cp $filename $apath
echo "File $filename copied successfully to $apath"
;;
6) echo "Enter Current File Name: "
read oname
echo "Enter New File Name: "
read nname
mv $oname $nname
;;
7) echo "Enter file Name : "
read fdel
if [ -f $fdel ]; then
rm -i $fdel
fi
;;
8) echo "Enter filename to open it in Text Editor: "
read filename
cat $filename
cat >> $filename
#vi $filename
;;
9) exit
;;
*) echo "Invalid Choice ....."
;;
esac
echo "Do u want to continue.....? [ y / n ]"
read choice
case $choice in
Y|y) choice=y;;
N|n) choice=n;;
*) choice=y;;
esac
done
-------------------OUTPUT---------------------------------
"p118.sh" 82L, 2390C written
[dca1621@agni ~]$ sh p118.sh
p118.sh: line 1: ear: command not found
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
1
/home/dca1621
Do u want to continue.....? [ y / n ]
y
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
2
total 92
-rw-rw-r-- 1 dca1621 dca1621 308 Apr 27 12:00 \
-rw-rw-r-- 1 dca1621 dca1621 160 May 7 10:46 dca16.sh
-rw-rw-r-- 1 dca1621 dca1621 385 May 7 11:01 dca17.sh
-rw-rw-r-- 1 dca1621 dca1621 0 Apr 27 10:55 dca1sh
-rw-rw-r-- 1 dca1621 dca1621 934 May 6 11:56 dca1.sh
-rw------- 1 dca1621 dca1621 0 Apr 25 11:23 dca1.sh.save
-rw------- 1 dca1621 dca1621 19 Apr 25 11:23 dca1.sh.save.1
-rw-rw-r-- 1 dca1621 dca1621 331 May 6 13:33 dca21.sh
-rw-rw-r-- 1 dca1621 dca1621 209 May 4 12:02 dca22.sh
-rw-rw-r-- 1 dca1621 dca1621 120 Apr 30 12:38 dca2.sh
-rw-rw-r-- 1 dca1621 dca1621 495 May 7 10:33 dca5.sh
-rw-rw-r-- 1 dca1621 dca1621 480 May 7 10:40 dca6.sh
-rw-rw-r-- 1 dca1621 dca1621 1351 May 6 13:27 dcaa.sh
-rw-rw-r-- 1 dca1621 dca1621 1360 May 4 12:09 dca.sh
-rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir1.txt
-rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir2.txt
-rw-rw-r-- 1 dca1621 dca1621 1466 May 7 11:05 p117.sh
-rw-rw-r-- 1 dca1621 dca1621 2390 May 7 11:09 p118.sh
drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 patel
-rw-rw-r-- 1 dca1621 dca1621 294 May 4 12:01 Result_ex2f.txt
-rw-rw-r-- 1 dca1621 dca1621 19 May 7 10:46 studentinfo.txt
drwxrwxr-x 2 dca1621 dca1621 4096 May 7 10:42 student.txt
-rw-rw-r-- 1 dca1621 dca1621 48 May 7 11:03 studInfo.txt
drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 TargetDir
drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 vishal
Do u want to continue.....? [ y / n ]
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
3
Enter Directory Name
vishal
mkdir: cannot create directory `vishal': File exists
[ vishal ] Sucessfully Created Directory
Do u want to continue.....? [ y / n ]
y
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
4
Enter the Absolute Path to change the directory:
Working path changed successfully!
/home/dca1621
Do u want to continue.....? [ y / n ]
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
7
Enter file Name :
rm: missing operand
Try `rm --help' for more information.
Do u want to continue.....? [ y / n ]
____________________________________
MAIN MENU
____________________________________
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
____________________________________
ENTER THE CHOICE :-
9
[dca1621@agni ~]$
[dca1621@agni ~]$ 9
bash: 9: command not found...
[dca1621@agni ~]$
------------------------------------------------------------
18)
Write a script which reads a text file and output key attribute of file
OR
Write a script which reads a text file and output the following
1). Count of character, words and lines.
2). File in reverse.
3). Frequency of particular word in the file.
4). Lower case letter in place of upper case letter.
File Name : ex15.sh
echo "Enter File Name \c"
read fname
choice=y
while [ "$choice" = "y" ]
do
echo " ------------------------------------"
echo " 1. Count character,words and lines"
echo " 2. Reverse File"
echo " 3. Found frequency of word"
echo " 4. Convert upper to lower"
echo " 5. Convert lower to upper"
echo " 6. Exit
echo " ------------------------------------"
echo "Enter Choice: \c"
read ch
case $ch in
1)
echo "Total Characters:\c"
wc -c $fname
echo "Total Words:\c"
wc -w $fname
echo "Total Lines:\c"
wc -l $fname
;;
2)
rev $fname
;;
3)
echo "Find your word :\c"
read w
echo "Frequency:\c"
grep -c "$w" $fname
;;
4)
echo "Enter Text : \c"
read text
echo $text | tr "[A-Z]" "[a-z]"
;;
5)
echo "Enter Text : \c"
read text
echo $text | tr "[a-z]" "[A-Z]"
;;
6) exit
;;
*) echo "Invalid Choice ....."
;;
esac
echo "Do u want to continue.....? [y/n]"
read choice
case $choice in
Y|y) choice=y;;
N|n) choice=n;;
*) choice=y;;
esac
done
-------------------OUTPUT---------------------------------
"p119.sh" 60L, 1177C written
[dca1621@agni ~]$ sh p119.sh
Enter File Name \c
studentinfo.txt
p119.sh: line 51: unexpected EOF while looking for matching `"'
p119.sh: line 61: syntax error: unexpected end of file
[dca1621@agni ~]$ sh p119.sh
Enter File Name \c
3
p119.sh: line 51: unexpected EOF while looking for matching `"'
p119.sh: line 61: syntax error: unexpected end of file
[dca1621@agni ~]$
------------------------------------------------------------
19)
Write a Shell Script for Simple Database Management System Operation OR
Write a Script for Simple Database Management System Operation.
Database File Contains Following Fields.
EMP_NO
EMP_NAME
EMP_ADDRESS
EMP_AGE
EMP_GENDER
EMP_DESIGNATION
EMP_BASIC_SALARY
Provide Menu Driven Facility For
VIEW RECORD BASED ON QUERY
ADD RECORD
DELETE RECORD
MODIFY RECORD.
COUNT TOTAL NUMBER OF RECORDS
EXIT
File Name : Employee.txt
1,bipin,xyz,27,Male,Professor,10000,true
2,hitesh,xyz,27,Male,Accountant,10000,false
3,ritesh,xyz,27,Male,HOD,20000,true
4,mitesh,xyz,27,Male,Clerk,2000,true
5,jitesh,xyz,27,Male,DBA,5000,true
File Name : ex17.sh
clear
AutoNumber()
{
local eno=0
f=0
for j in `cat Employee.txt`
do
eno=$(echo "$j" | cut -d "," -f 1)
f=1
done
if [ $f = 1 ]
then
eno=`expr $eno + 1`
else
eno=1
fi
echo $eno
}
Insert()
{
clear
eno=$1
echo "Enter Employee No: $eno"
echo "Enter Employee Name: \c"
read enm
echo "Enter Employee Address: \c"
read eadd
echo "Enter Employee Age : \c"
read eage
echo "Enter Employee Gender: \c"
read egen
echo "Enter Employee Designation : \c"
read edes
echo "Enter Employee Basic Salary : \c"
read ebal
echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true" >> Employee.txt
echo " Insert Sucessfully "
}
Display()
{
clear
echo "__________________________________________________"
echo " Employee Details "
echo "__________________________________________________"
echo "__________________________________________________"
echo "#ENO \t ENAME \t\t EADDR \t\t\t EAGE \t EGEN \t EDES \t\t EBAL"
for j in `cat Employee.txt`
do
eno=$(echo "$j" | cut -d "," -f 1)
enm=$(echo "$j" | cut -d "," -f 2)
eadd=$(echo "$j" | cut -d "," -f 3)
eage=$(echo "$j" | cut -d "," -f 4)
egen=$(echo "$j" | cut -d "," -f 5)
edes=$(echo "$j" | cut -d "," -f 6)
ebal=$(echo "$j" | cut -d "," -f 7)
tfval=$(echo "$j" | cut -d "," -f 8)
if [ $tfval = "true" ]
then
echo "___________________________________________"
echo "$eno \t $enm \t\t $eadd \t\t $eage \t $egen \t $edes \t $ebal"
fi
done
echo "__________________________________________________"
}
Search()
{
clear
echo "Enter Employee NO: \c"
read no
echo "__________________________________________________"
echo " Employee Details "
echo "__________________________________________________"
flag=0
for j in `cat Employee.txt`
do
eno=$(echo "$j" | cut -d "," -f 1)
enm=$(echo "$j" | cut -d "," -f 2)
eadd=$(echo "$j" | cut -d "," -f 3)
eage=$(echo "$j" | cut -d "," -f 4)
egen=$(echo "$j" | cut -d "," -f 5)
edes=$(echo "$j" | cut -d "," -f 6)
ebal=$(echo "$j" | cut -d "," -f 7)
tfval=$(echo "$j" | cut -d "," -f 8)
if [ $no -eq $eno ] && [ $tfval = "true" ]
then
flag=1
echo "________________________________________"
echo " ENo : $eno EName : $enm "
echo "________________________________________"
echo " EAdd : $eadd "
echo " EAge : $eage "
echo " EGen : $egen "
echo "________________________________________"
echo " EDes : $edes "
echo "________________________________________"
echo " ESal : $ebal "
echo "________________________________________"
fi
done
if [ $flag = 0 ]
then
echo " No Record Found "
fi
echo "__________________________________________________"
}
Delete()
{
clear
f=0
echo "Enter Employee NO: \c"
read no
for j in `cat Employee.txt`
do
eno=$(echo "$j" | cut -d "," -f 1)
enm=$(echo "$j" | cut -d "," -f 2)
eadd=$(echo "$j" | cut -d "," -f 3)
eage=$(echo "$j" | cut -d "," -f 4)
egen=$(echo "$j" | cut -d "," -f 5)
edes=$(echo "$j" | cut -d "," -f 6)
ebal=$(echo "$j" | cut -d "," -f 7)
if [ $no -eq $eno ]
then
f=1
line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,false")
fnm=`cat Employee.txt`
d=$(echo "$fnm" | sed s/$j/$line/g )
echo $d > Employee.txt
echo " Delete Successfully "
fi
done
if [ f = 0 ]
then
echo " No Record Found "
fi
}
Update()
{
clear
echo "Enter Employee NO: \c"
read no
for j in `cat Employee.txt`
do
eno=$(echo "$j" | cut -d "," -f 1)
enm=$(echo "$j" | cut -d "," -f 2)
eadd=$(echo "$j" | cut -d "," -f 3)
eage=$(echo "$j" | cut -d "," -f 4)
egen=$(echo "$j" | cut -d "," -f 5)
edes=$(echo "$j" | cut -d "," -f 6)
ebal=$(echo "$j" | cut -d "," -f 7)
if [ $no -eq $eno ]
then
echo "______________Enter New Record______________"
echo "Enter Employee No: $eno"
echo "Enter Employee Name: \c"
read enm
echo "Enter Employee Address: \c"
read eadd
echo "Enter Employee Age : \c"
read eage
echo "Enter Employee Gender: \c"
read egen
echo "Enter Employee Designation : \c"
read edes
echo "Enter Employee Basic Salary : \c"
read ebal
line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true")
#line=$(echo "$eno,$snm,$m1,$m2,$m3,$total,$per,true")
fnm=`cat Employee.txt`
d=$(echo "$fnm" | sed s/$j/$line/g )
echo $d > Employee.txt
echo " Update Sucessfully "
fi
done
}
while [ true ]
do
echo " _______________________________"
echo " 1. Insert "
echo " 2. Delete "
echo " 3. Update "
echo " 4. Display "
echo " 5. Search "
echo " 6. Exit "
echo " _______________________________"
echo "Enter Choice: \c"
read ch
case $ch in
1)
nxtSrNo=$(AutoNumber)
Insert $nxtSrNo
;;
2) Delete ;;
3) Update ;;
4) Display ;;
5) Search ;;
6) break;;
*) echo " Wrong Choice "
esac
done
-------------------OUTPUT---------------------------------
"p120.sh" [New] 190L, 7895C written
[dca1621@agni ~]$ sh p120.sh
p120.sh: line 1: o: command not found
__________________________________________________
__________________________________________________
#ENO \t ENAME \t\t EADDR \t\t\t EAGE \t EGEN \t EDES \t\t EBAL
___________________________________________
1 \t bipin \t\t xyz \t\t 27 \t Male \t Professor \t 10000
___________________________________________
3 \t ritesh \t\t xyz \t\t 27 \t Male \t HOD \t 20000
___________________________________________
4 \t mitesh \t\t xyz \t\t 27 \t Male \t Clerk \t 2000
___________________________________________
5 \t jitesh \t\t xyz \t\t 27 \t Male \t DBA \t 5000
__________________________________________________
p120.sh: line 24: syntax error near unexpected token `}'
p120.sh: line 24: `}'
[dca1621@agni ~]$
------------------------------------------------------------
20)
Write a shell script to Perform Following String Operations OR
Definition : Write A Script To Perform Following String Operations Using Menu:
1. COMPARE TWO STRINGS.
2. JOIN TWO STRINGS.
3. FIND THE LENGTH OF A GIVEN STRING.
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING.
File Name : ex18.sh
clear
choice=y
while [ "$choice" = "y" ]
do
echo "____________________________________________"
echo "1. COMPARE TWO STRINGS"
echo "2. JOIN TWO STRINGS"
echo "3. FIND THE LENGTH OF A GIVEN STRING"
echo "4. OCCURRENCE OF CHARACTER AND WORDS"
echo "5. REVERSE THE STRING"
echo "6. EXIT"
echo "____________________________________________"
echo "Enter Choice: \c"
read ch
echo "____________________________________________"
case $ch in
1)
echo "Enter String1: \c"
read str1
echo "Enter String2: \c"
read str2
if [ $str1 = $str2 ]
then
echo "String is equal"
else
echo "String is not equal"
fi
;;
2)
echo "Enter String1: \c"
read str1
echo "Enter String2: \c"
read str2
str3=$str1$str2
echo "Join String: \c" $str3
;;
3)
len=0
echo "Enter String1: \c"
read str1
len=$(echo "$str1" | wc -c)
len=`expr $len - 1`
echo "Length: " $len
;;
4)
echo "Enter String: \c"
read str
echo "Enter Word to find : \c"
read word
echo $str | cat > str1.txt
grep -o $word str1.txt | cat > str2.txt
count=`grep -c $word str2.txt`
echo "Count: \c"$count
;;
5)
echo "Enter String1: \c"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
while [ $len -gt 0 ]
do
rev=`expr $str | cut -c $len`
ans=$ans$rev
len=`expr $len - 1`
done
echo "Reverse String: \c"$ans
;;
6) exit ;;
*) echo "Invalid Choice ....." ;;
esac
echo "Do u want to continue.....? [y/n]"
read choice
case $choice in
Y|y) choice=y;;
N|n) choice=n;;
*) choice=y;;
esac
done
-------------------OUTPUT---------------------------------
"p121.sh" 87L, 1898C written
[dca1621@agni ~]$ sh p121.sh
p121.sh: line 1: ear: command not found
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
1
____________________________________________
Enter String1: \c
vishal
Enter String2: \c
patel
String is not equal
Do u want to continue.....? [y/n]
y
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
2
____________________________________________
Enter String1: \c
vishal
Enter String2: \c
patel
Join String: \c vishalpatel
Do u want to continue.....? [y/n]
y
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
3
____________________________________________
Enter String1: \c
vishal patel
Length: 12
Do u want to continue.....? [y/n]
y
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
4
____________________________________________
Enter String: \c
vishal
Enter Word to find : \c
s
Count: \c1
Do u want to continue.....? [y/n]
y
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
5
____________________________________________
Enter String1: \c
vishal
Reverse String: \clahsiv
Do u want to continue.....? [y/n]
y
____________________________________________
1. COMPARE TWO STRINGS
2. JOIN TWO STRINGS
3. FIND THE LENGTH OF A GIVEN STRING
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING
6. EXIT
____________________________________________
Enter Choice: \c
6
____________________________________________
[dca1621@agni ~]$
------------------------------------------------------------
21)
Write a shell script to mathematics calculation OR
Definition: Write a script to calculate gross salary for any number of employees
Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.
File Name : ex19.sh
clear
echo "Enter Basic Salary :\c"
read bs
if [ $bs -lt 1500 ]
then
hra=`echo $bs \* 10 / 100 | bc`
da=`echo $bs \* 15 /100 | bc`
else
hra=500
da=`echo $bs \* 98/100 | bc`
fi
gs=`echo $bs + $hra + $da | bc`
echo " DA : $da"
echo " HRA : $hra"
echo "GROSS SALARY : $gs"
-------------------OUTPUT---------------------------------
"p122.sh" [New] 16L, 279C written
[dca1621@agni ~]$ sh p122.sh
p122.sh: line 1: ear: command not found
Enter Basic Salary :\c
25000
DA : 24500
HRA : 500
p122.sh: line 16: unexpected EOF while looking for matching `"'
p122.sh: line 17: syntax error: unexpected end of file
[dca1621@agni ~]$
------------------------------------------------------------
22)
Write a script to check whether a given string is palindrome or not
File Name : ex20.sh
clear
echo "Enter String : \c"
read s
p=$s
m=`expr $s | wc -c`
i=`expr $m - 1`
while [ $i -ge 1 ]
do
k=`expr $s | cut -c$i`
r=$r$k
i=`expr $i - 1`
echo $k
done
echo $r
if [ "$p" != "$r" ]
then
echo "It is not palindrome"
else
echo "It is palindrome"
fi
-------------------OUTPUT---------------------------------
"p123.sh" [New] 25L, 340C written
[dca1621@agni ~]$ sh p123.sh
p123.sh: line 1: ear: command not found
Enter String : \c
vishal
l
a
h
s
i
v
lahsiv
It is not palindrome
[dca1621@agni ~]$
------------------------------------------------------------
23)
sort files in unix by size OR
#Write a script to display the directory in the descending order of the size of each file.
ls -lS
OR
#Write a script to display the directory in the Ascending order of the size of each file.
ls -lSr
#option [ -l ] is use for log listing
#option [ -S ] to sort by size
#option [ -r ] use for revers ordering
-------------------OUTPUT---------------------------------
"p124.sh" [New] 5L, 120C written
[dca1621@agni ~]$ sh p124.sh
p124.sh: line 1: -lSr: command not found
[dca1621@agni ~]$ ls
\ dca21.sh dir1.txt p121.sh str2.txt
dca16.sh dca22.sh dir2.txt p122.sh studentinfo.txt
dca17.sh dca2.sh Employee.txt p123.sh student.txt
dca1sh dca5.sh p117.sh p124.sh studInfo.txt
dca1.sh dca6.sh p118.sh patel TargetDir
dca1.sh.save dcaa.sh p119.sh Result_ex2f.txt vishal
dca1.sh.save.1 dca.sh p120.sh str1.txt
[dca1621@agni ~]$ -ls
bash: -ls: command not found...
[dca1621@agni ~]$ ls -ls
total 132
4 -rw-rw-r-- 1 dca1621 dca1621 308 Apr 27 12:00 \
4 -rw-rw-r-- 1 dca1621 dca1621 160 May 7 10:46 dca16.sh
4 -rw-rw-r-- 1 dca1621 dca1621 385 May 7 11:01 dca17.sh
0 -rw-rw-r-- 1 dca1621 dca1621 0 Apr 27 10:55 dca1sh
4 -rw-rw-r-- 1 dca1621 dca1621 934 May 6 11:56 dca1.sh
0 -rw------- 1 dca1621 dca1621 0 Apr 25 11:23 dca1.sh.save
4 -rw------- 1 dca1621 dca1621 19 Apr 25 11:23 dca1.sh.save.1
4 -rw-rw-r-- 1 dca1621 dca1621 331 May 6 13:33 dca21.sh
4 -rw-rw-r-- 1 dca1621 dca1621 209 May 4 12:02 dca22.sh
4 -rw-rw-r-- 1 dca1621 dca1621 120 Apr 30 12:38 dca2.sh
4 -rw-rw-r-- 1 dca1621 dca1621 495 May 7 10:33 dca5.sh
4 -rw-rw-r-- 1 dca1621 dca1621 480 May 7 10:40 dca6.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1351 May 6 13:27 dcaa.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1360 May 4 12:09 dca.sh
4 -rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir1.txt
4 -rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir2.txt
4 -rw-rw-r-- 1 dca1621 dca1621 193 May 7 11:24 Employee.txt
4 -rw-rw-r-- 1 dca1621 dca1621 1466 May 7 11:05 p117.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1173 May 7 11:13 p118.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1177 May 7 11:18 p119.sh
8 -rw-rw-r-- 1 dca1621 dca1621 7895 May 7 11:26 p120.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1898 May 7 11:33 p121.sh
4 -rw-rw-r-- 1 dca1621 dca1621 279 May 7 11:36 p122.sh
4 -rw-rw-r-- 1 dca1621 dca1621 340 May 7 11:38 p123.sh
4 -rw-rw-r-- 1 dca1621 dca1621 120 May 7 11:39 p124.sh
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 patel
4 -rw-rw-r-- 1 dca1621 dca1621 294 May 4 12:01 Result_ex2f.txt
4 -rw-rw-r-- 1 dca1621 dca1621 7 May 7 11:34 str1.txt
4 -rw-rw-r-- 1 dca1621 dca1621 2 May 7 11:34 str2.txt
4 -rw-rw-r-- 1 dca1621 dca1621 19 May 7 10:46 studentinfo.txt
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 7 10:42 student.txt
4 -rw-rw-r-- 1 dca1621 dca1621 48 May 7 11:03 studInfo.txt
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 TargetDir
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 vishal
[dca1621@agni ~]$ ls -lsr
total 132
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 vishal
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 TargetDir
4 -rw-rw-r-- 1 dca1621 dca1621 48 May 7 11:03 studInfo.txt
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 7 10:42 student.txt
4 -rw-rw-r-- 1 dca1621 dca1621 19 May 7 10:46 studentinfo.txt
4 -rw-rw-r-- 1 dca1621 dca1621 2 May 7 11:34 str2.txt
4 -rw-rw-r-- 1 dca1621 dca1621 7 May 7 11:34 str1.txt
4 -rw-rw-r-- 1 dca1621 dca1621 294 May 4 12:01 Result_ex2f.txt
4 drwxrwxr-x 2 dca1621 dca1621 4096 May 6 11:53 patel
4 -rw-rw-r-- 1 dca1621 dca1621 120 May 7 11:39 p124.sh
4 -rw-rw-r-- 1 dca1621 dca1621 340 May 7 11:38 p123.sh
4 -rw-rw-r-- 1 dca1621 dca1621 279 May 7 11:36 p122.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1898 May 7 11:33 p121.sh
8 -rw-rw-r-- 1 dca1621 dca1621 7895 May 7 11:26 p120.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1177 May 7 11:18 p119.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1173 May 7 11:13 p118.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1466 May 7 11:05 p117.sh
4 -rw-rw-r-- 1 dca1621 dca1621 193 May 7 11:24 Employee.txt
4 -rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir2.txt
4 -rw-rw-r-- 1 dca1621 dca1621 143 May 6 13:42 dir1.txt
4 -rw-rw-r-- 1 dca1621 dca1621 1360 May 4 12:09 dca.sh
4 -rw-rw-r-- 1 dca1621 dca1621 1351 May 6 13:27 dcaa.sh
4 -rw-rw-r-- 1 dca1621 dca1621 480 May 7 10:40 dca6.sh
4 -rw-rw-r-- 1 dca1621 dca1621 495 May 7 10:33 dca5.sh
4 -rw-rw-r-- 1 dca1621 dca1621 120 Apr 30 12:38 dca2.sh
4 -rw-rw-r-- 1 dca1621 dca1621 209 May 4 12:02 dca22.sh
4 -rw-rw-r-- 1 dca1621 dca1621 331 May 6 13:33 dca21.sh
4 -rw------- 1 dca1621 dca1621 19 Apr 25 11:23 dca1.sh.save.1
0 -rw------- 1 dca1621 dca1621 0 Apr 25 11:23 dca1.sh.save
4 -rw-rw-r-- 1 dca1621 dca1621 934 May 6 11:56 dca1.sh
0 -rw-rw-r-- 1 dca1621 dca1621 0 Apr 27 10:55 dca1sh
4 -rw-rw-r-- 1 dca1621 dca1621 385 May 7 11:01 dca17.sh
4 -rw-rw-r-- 1 dca1621 dca1621 160 May 7 10:46 dca16.sh
4 -rw-rw-r-- 1 dca1621 dca1621 308 Apr 27 12:00 \
[dca1621@agni ~]$
------------------------------------------------------------
24)
Write a script to check whether a given number is palindrome or not
File Name : ex21.sh
clear
echo "Enter No : \c"
read no
m=$no
rev=0
while [ $no -gt 0 ]
do
r=`expr $no % 10`
rev=`expr $rev \* 10 + $r`
no=`expr $no / 10`
done
if [ $m = $rev ]
then
echo " [ $m] is Palindrome"
else
echo " [ $m ] is not Palindrome"
fi
-------------------OUTPUT---------------------------------
Enter No : \c
45
[ 45 ] is not Palindrome
[dca1621@agni ~]$
[dca1621@agni ~]$
------------------------------------------------------------
25)
Write a script to display all words of a file in ascending order.
File Name : ex22.sh
echo "Enter File name:\c"
read fn
for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.txt
-------------------OUTPUT---------------------------------
"p126.sh" [New] 8L, 102C written
[dca1621@agni ~]$ sh p126.sh
Enter File name:\c
vishal
cat: vishal: Is a directory
sort: open failed: f2.txt: No such file or directory
rm: cannot remove `f2.txt': No such file or directory
[dca1621@agni ~]$ sh p126.sh
Enter File name:\c
studentinfo.txt
ritesh,100,100,100
[dca1621@agni ~]$
------------------------------------------------------------
26)
Write a script to display all lines of a file in ascending order
File Name : ex23.sh
clear
echo "Enter File Name :\c"
read filename
sort $filename
-------------------OUTPUT---------------------------------
Enter File Name :\c
studentinfo.txt
ritesh,100,100,100
[dca1621@agni ~]$
[dca1621@agni ~]$
------------------------------------------------------------
27)
Write a script to display the last modified file
File Name : ex24.sh
clear
echo "Enter Directory Name :\c"
read dir1
cd $dir1
ls -lt | head -2 | tail -1 |cut -d " " -f 10
-------------------OUTPUT---------------------------------
Enter Directory Name :\c
patel
[dca1621@agni ~]$
[dca1621@agni ~]$
[dca1621@agni ~]$
------------------------------------------------------------
28)
Write a shell script to add the statement #include at the beginning of every C source file in current directory containing printf and fprintf.
clear
grep -l -e "printf" -e "fprintf" *.c > temp.txt
#grep
# -l, --files-with-matches
# Suppress normal output; instead print the name of each input
# file from which output would normally have been printed.
# -e PATTERN, --regexp=PATTERN
# Use PATTERN as the pattern. This can be used to specify
# multiple search patterns
for i in `cat temp.txt`
do
sed '1i\#include<stdio .h=".h">' $i > temp2.txt
cat temp2.txt > $i
echo "Successfully Added..."
done
# sed i :
# -i[SUFFIX], --in-place[=SUFFIX]
# edit files in place (makes backup if extension supplied)
# sed l :
# -l N, --line-length=N
# specify the desired line-wrap length for the `l' command
</stdio>
-------------------OUTPUT---------------------------------
p126.sh: line 3: gt: command not found
p126.sh: line 3: temp.txt: command not found
grep: *.c: No such file or directory
cat: temp.txt: No such file or directory
[dca1621@agni ~]$
------------------------------------------------------------
29)
Write a script to learn command line argument
Definition : 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.
File Name : ex26.sh
flag=1
if [ $# -ne 0 ]
then
# if user provide arguments
echo $* >temp
flag=1
else
# if user not provide arguments
ans=`ls *.c`
if [ "" == "$ans" ]
then
echo "There are no c file"
flag=0
else
ls *.c>temp
flag=1
fi
fi
if [ $flag -eq 1 ]
then
for filename in `cat temp`
do
if [ -f $filename ]
then
echo "_______________ $filename _______________"
sed -n -e "1,10 p" $filename
rm -i $filename
fi
done
rm temp
fi
-------------------OUTPUT---------------------------------
"dca1211.sh" [New] 31L, 827C written
[dca1621@agni ~]$ sh dca1211.sh
ls: cannot access *.c: No such file or directory
There are no c file
[dca1621@agni ~]$
------------------------------------------------------------
30)
unix grep command OR
Definition : 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
File Name : ex27.sh
clear
echo "Enter file name : "
read fileName
if [ -f $fileName ]
then
echo "____________________________________"
echo " Trim File .... "
echo "____________________________________"
# Delete leading & trailing whitespace from each line and store it tempFile.txt
sed 's/^[ \t]*//;s/[ \t]*$//;' $fileName >tempFile.txt
# Remove all blank lines from tempFile.txt
sed '/^$/d' tempFile.txt
# Replace content of this two file
mv tempFile.txt $fileName
else
echo "File Not Found"
fi
echo "____________________________________"
echo " Located Lines .... "
echo "____________________________________"
# Locate lines containing only printf but not fprintf
grep -e "printf" $fileName | grep -v "fprint"
-------------------OUTPUT---------------------------------
"dca1211.sh" 28L, 769C written
[dca1621@agni ~]$ sh dca1211.sh
dca1211.sh: line 1: r: command not found
Enter file name :
vishal.c
File Not Found
____________________________________
Located Lines ....
____________________________________
grep: vishal.c: No such file or directory
[dca1621@agni ~]$ studentinfo.txt
bash: studentinfo.txt: command not found...
[dca1621@agni ~]$ sh dca1211.sh
dca1211.sh: line 1: r: command not found
Enter file name :
studentinfo.txt
____________________________________
Trim File ....
____________________________________
ritesh,100,100,100
____________________________________
Located Lines ....
____________________________________
[dca1621@agni ~]$
------------------------------------------------------------
***************************************************************************************
MORE UNIX COMMANDS
***************************************************************************************
******************************************************************************************
Basic UNIX Command Line (shell) navigation
******************************************************************************************
Directories:
File and directory paths in UNIX use the forward slash "/"
to separate directory names in a path.
examples:
/ "root" directory
/usr directory usr (sub-directory of / "root" directory)
/usr/STRIM100 STRIM100 is a subdirectory of /usr
Moving around the file system:
pwd Show the "present working directory", or current directory.
cd Change current directory to your HOME directory.
cd /usr/STRIM100 Change current directory to /usr/STRIM100.
cd INIT Change current directory to INIT which is a sub-directory of the current
directory.
cd .. Change current directory to the parent directory of the current directory.
cd $STRMWORK Change current directory to the directory defined by the environment
variable 'STRMWORK'.
cd ~bob Change the current directory to the user bob's home directory (if you have permission).
Listing directory contents:
ls list a directory
ls -l list a directory in long ( detailed ) format
for example:
$ ls -l
drwxr-xr-x 4 cliff user 1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r--r-- 1 cliff user 767392 Jun 6 14:28 scanlib.tar.gz
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | | |
| | | | | owner group size date time name
| | | | number of links to file or directory contents
| | | permissions for world
| | permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others...
ls -a List the current directory including hidden files. Hidden files start
with "."
ls -ld * List all the file and directory names in the current directory using
long format. Without the "d" option, ls would list the contents
of any sub-directory of the current. With the "d" option, ls
just lists them like regular files.
Changing file permissions and attributes
chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for
the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)
chgrp user file Makes file belong to the group user.
chown cliff file Makes cliff the owner of file.
chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree.
You must be the owner of the file/directory or be root before you can do any of these things.
Moving, renaming, and copying files:
cp file1 file2 copy a file
mv file1 newname move or rename a file
mv file1 ~/AAA/ move file1 into sub-directory AAA in your home directory.
rm file1 [file2 ...] remove or delete a file
rm -r dir1 [dir2...] recursivly remove a directory and its contents BE CAREFUL!
mkdir dir1 [dir2...] create directories
mkdir -p dirpath create the directory dirpath, including all implied directories in the path.
rmdir dir1 [dir2...] remove an empty directory
Viewing and editing files:
cat filename Dump a file to the screen in ascii.
more filename Progressively dump a file to the screen: ENTER = one line down
SPACEBAR = page down q=quit
less filename Like more, but you can use Page-Up too. Not on all systems.
vi filename Edit a file using the vi editor. All UNIX systems will have vi in some form.
emacs filename Edit a file using the emacs editor. Not all systems will have emacs.
head filename Show the first few lines of a file.
head -n filename Show the first n lines of a file.
tail filename Show the last few lines of a file.
tail -n filename Show the last n lines of a file.
Shells
The behavior of the command line interface will differ slightly depending
on the shell program that is being used.
Depending on the shell used, some extra behaviors can be quite nifty.
You can find out what shell you are using by the command:
echo $SHELL
Of course you can create a file with a list of shell commands and execute it like
a program to perform a task. This is called a shell script. This is in fact the
primary purpose of most shells, not the interactive command line behavior.
Environment variables
You can teach your shell to remember things for later using environment variables.
For example under the bash shell:
export CASROOT=/usr/local/CAS3.0 Defines the variable CASROOT with the value
/usr/local/CAS3.0.
export LD_LIBRARY_PATH=$CASROOT/Linux/lib Defines the variable LD_LIBRARY_PATH with
the value of CASROOT with /Linux/lib appended,
or /usr/local/CAS3.0/Linux/lib
By prefixing $ to the variable name, you can evaluate it in any command:
cd $CASROOT Changes your present working directory to the value of CASROOT
echo $CASROOT Prints out the value of CASROOT, or /usr/local/CAS3.0
printenv CASROOT Does the same thing in bash and some other shells.
Interactive History
A feature of bash and tcsh (and sometimes others) you can use
the up-arrow keys to access your previous commands, edit
them, and re-execute them.
Filename Completion
A feature of bash and tcsh (and possibly others) you can use the
TAB key to complete a partially typed filename. For example if you
have a file called constantine-monks-and-willy-wonka.txt in your
directory and want to edit it you can type 'vi const', hit the TAB key,
and the shell will fill in the rest of the name for you (provided the
completion is unique).
Bash is the way cool shell.
Bash will even complete the name of commands and environment variables.
And if there are multiple completions, if you hit TAB twice bash will show
you all the completions. Bash is the default user shell for most Linux systems.
Redirection:
grep string filename > newfile Redirects the output of the above grep
command to a file 'newfile'.
grep string filename >> existfile Appends the output of the grep command
to the end of 'existfile'.
The redirection directives, > and >> can be used on the output of most commands
to direct their output to a file.
Pipes:
The pipe symbol "|" is used to direct the output of one command to the input
of another.
For example:
ls -l | more This commands takes the output of the long format directory list command
"ls -l" and pipes it through the more command (also known as a filter).
In this case a very long list of files can be viewed a page at a time.
du -sc * | sort -n | tail
The command "du -sc" lists the sizes of all files and directories in the
current working directory. That is piped through "sort -n" which orders the
output from smallest to largest size. Finally, that output is piped through "tail"
which displays only the last few (which just happen to be the largest) results.
Command Substitution
You can use the output of one command as an input to another command in another way
called command substitution. Command substitution is invoked when by enclosing the
substituted command in backwards single quotes. For example:
cat `find . -name aaa.txt`
which will cat ( dump to the screen ) all the files named aaa.txt that exist in the current
directory or in any subdirectory tree.
Searching for strings in files: The grep command
grep string filename prints all the lines in a file that contain the string
Searching for files : The find command
find search_path -name filename
find . -name aaa.txt Finds all the files named aaa.txt in the current directory or
any subdirectory tree.
find / -name vimrc Find all the files named 'vimrc' anywhere on the system.
find /usr/local/games -name "*xpilot*"
Find all files whose names contain the string 'xpilot' which
exist within the '/usr/local/games' directory tree.
Reading and writing tapes, backups, and archives: The tar command
The tar command stands for "tape archive". It is the "standard" way to read
and write archives (collections of files and whole directory trees).
Often you will find archives of stuff with names like stuff.tar, or stuff.tar.gz. This
is stuff in a tar archive, and stuff in a tar archive which has been compressed using the
gzip compression program respectivly.
Chances are that if someone gives you a tape written on a UNIX system, it will be in tar format,
and you will use tar (and your tape drive) to read it.
Likewise, if you want to write a tape to give to someone else, you should probably use
tar as well.
Tar examples:
tar xv Extracts (x) files from the default tape drive while listing (v = verbose)
the file names to the screen.
tar tv Lists the files from the default tape device without extracting them.
tar cv file1 file2
Write files 'file1' and 'file2' to the default tape device.
tar cvf archive.tar file1 [file2...]
Create a tar archive as a file "archive.tar" containing file1,
file2...etc.
tar xvf archive.tar extract from the archive file
tar cvfz archive.tar.gz dname
Create a gzip compressed tar archive containing everything in the directory
'dname'. This does not work with all versions of tar.
tar xvfz archive.tar.gz
Extract a gzip compressed tar archive. Does not work with all versions of tar.
tar cvfI archive.tar.bz2 dname
Create a bz2 compressed tar archive. Does not work with all versions of tar
File compression: compress, gzip, and bzip2
The standard UNIX compression commands are compress and uncompress. Compressed files have
a suffix .Z added to their name. For example:
compress part.igs Creates a compressed file part.igs.Z
uncompress part.igs Uncompresseis part.igs from the compressed file part.igs.Z.
Note the .Z is not required.
Another common compression utility is gzip (and gunzip). These are the GNU compress and
uncompress utilities. gzip usually gives better compression than standard compress,
but may not be installed on all systems. The suffix for gzipped files is .gz
gzip part.igs Creates a compressed file part.igs.gz
gunzip part.igs Extracts the original file from part.igs.gz
The bzip2 utility has (in general) even better compression than gzip, but at the cost of longer
times to compress and uncompress the files. It is not as common a utility as gzip, but is
becoming more generally available.
bzip2 part.igs Create a compressed Iges file part.igs.bz2
bunzip2 part.igs.bz2 Uncompress the compressed iges file.
Looking for help: The man and apropos commands
Most of the commands have a manual page which give sometimes useful, often more or less
detailed, sometimes cryptic and unfathomable discriptions of their usage. Some say they
are called man pages because they are only for real men.
Example:
man ls Shows the manual page for the ls command
You can search through the man pages using apropos
Example:
apropos build Shows a list of all the man pages whose discriptions contain the word "build"
Do a man apropos for detailed help on apropos.
Basics of the vi editor
Opening a file
vi filename
Creating text
Edit modes: These keys enter editing modes and type in the text
of your document.
i Insert before current cursor position
I Insert at beginning of current line
a Insert (append) after current cursor position
A Append to end of line
r Replace 1 character
R Replace mode
<ESC> Terminate insertion or overwrite mode
Deletion of text
x Delete single character
dd Delete current line and put in buffer
ndd Delete n lines (n is a number) and put them in buffer
J Attaches the next line to the end of the current line (deletes carriage return).
Oops
u Undo last command
cut and paste
yy Yank current line into buffer
nyy Yank n lines into buffer
p Put the contents of the buffer after the current line
P Put the contents of the buffer before the current line
cursor positioning
^d Page down
^u Page up
:n Position cursor at line n
:$ Position cursor at end of file
^g Display current line number
h,j,k,l Left,Down,Up, and Right respectivly. Your arrow keys should also work if
if your keyboard mappings are anywhere near sane.
string substitution
:n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines
n1 to n2. If g is included (meaning global),
all instances of string1 on each line
are substituted. If g is not included,
only the first instance per matching line is
substituted.
^ matches start of line
. matches any single character
$ matches end of line
These and other "special characters" (like the forward slash) can be "escaped" with \
i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT"
Examples:
:1,$:s/dog/cat/g Substitute 'cat' for 'dog', every instance
for the entire file - lines 1 to $ (end of file)
:23,25:/frog/bird/ Substitute 'bird' for 'frog' on lines
23 through 25. Only the first instance
on each line is substituted.
Saving and quitting and other "ex" commands
These commands are all prefixed by pressing colon (:) and then entered in the lower
left corner of the window. They are called "ex" commands because they are commands
of the ex text editor - the precursor line editor to the screen editor
vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen)
Press <ESC> to exit from an editing mode.
:w Write the current file.
:w new.file Write the file to the name 'new.file'.
:w! existing.file Overwrite an existing file with the file currently being edited.
:wq Write the file and quit.
:q Quit.
:q! Quit with no changes.
:e filename Open the file 'filename' for editing.
:set number Turns on line numbering
:set nonumber Turns off line numbering
**************************************************************************************
more extra commands
**************************************************************************************
cat - display or concatenate files
cat takes a copy of a file and sends it to the standard output
(i.e. to be displayed on your terminal, unless redirected elsewhere),
so it is generally used either to read files, or to string together
copies of several files, writing the output to a new file.
cat ex
displays the contents of the file ex.
cat ex1 ex2 > newex
creates a new file newex containing copies of ex1 and ex2, with the
contents of ex2 following the contents of ex1.
cd - change directory
cd is used to change from one directory to another.
cd dir1
changes directory so that dir1 is your new current directory.
dir1 may be either the full pathname of the directory, or its pathname relative to the current directory.
cd
changes directory to your home directory.
cd ..
moves to the parent directory of your current directory.
chmod - change the permissions on a file or directory
chmod alters the permissions on files and directories using either
symbolic or octal numeric codes. The symbolic codes are given here:-
u user + to add a permission r read
g group - to remove a permission w write
o other = to assign a permission explicitly x execute (for files),
access (for directories)
The following examples illustrate how these codes are used.
chmod u=rw file1
sets the permissions on the file file1 to give the user read and write
permission on file1. No other permissions are altered.
chmod u+x,g+w,o-r file1
alters the permissions on the file file1 to give the user execute permission on file1,
to give members of the user's group write permission on the file, and prevent any users not in this group from reading it.
chmod u+w,go-x dir1
gives the user write permission in the directory dir1, and prevents all other users
having access to that directory (by using cd. They can still list its contents using ls.)
cp - copy a file
The command cp is used to make copies of files and directories.
cp file1 file2
copies the contents of the file file1 into a new file called file2. cp cannot copy
a file onto itself.
cp file3 file4 dir1
creates copies of file3 and file4 (with the same names), within the directory dir1.
dir1 must already exist for the copying to succeed.
cp -r dir2 dir3
recursively copies the directory dir2, together with its contents and subdirectories,
to the directory dir3. If dir3 does not already exist, it is created by cp, and the
contents and subdirectories of dir2 are recreated within it. If dir3 does exist, a
subdirectory called dir2 is created within it, containing a copy of all the contents of the original dir2.
date - display the current date and time
date returns information on the current date and time in the format shown below:-
Wed Jan 30 11:27:50 GMT 2008
It is possible to alter the format of the output from date. For example, using the command line
date '+The date is %d/%m/%y, and the time is %H:%M:%S.'
at exactly 11.30am on 30th January 2008, would produce the output
The date is 30/01/08, and the time is 11:30:00.
diff - display differences between text files
diff file1 file2 reports line-by-line differences between the text files file1 and file2.
The default output will contain lines such as n1 a n2,n3 and n4,n5 c n6,n7 ,
(where n1 a n2,n3 means that file2 has the extra lines n2 to n3 following the
line that has the number n1 in file1, and n4,n5 c n6,n7 means that lines n4
to n5 in file1 differ from lines n6 to n7 in file2). After each such line,
diff prints the relevant lines from the text files, with < in front of each
line from file1 and > in front of each line from file2.
There are several options to diff, including diff -i, which ignores the case of letters when comparing lines,
and diff -b, which ignores all trailing blanks.
diff -cn
produces a listing of differences with n lines of context,
where the default is three lines. The form of the output is different
from that given by diff, with + indicating lines which have been added,
- indicating lines which have been removed, and ! indicating lines which have been changed.
diff dir1 dir2
will sort the contents of directories dir1 and dir2 by name,
and then run diff on the text files which differ.
file - determine the type of a file
file tests named files to determine the categories their contents belong to.
file file1
can tell if file1 is, for example, a source program,
an executable program or shell script, an empty file,
a directory, or a library, but (a warning!) it does sometimes make mistakes.
find - find files of a specified name or type
find searches for files in a named directory and all its subdirectories.
find . -name '*.f' -print
searches the current directory and all its subdirectories for files ending in .
f, and writes their names to the standard output. In some versions of Unix the
names of the files will only be written out if the -print option is used.
find /local -name core -user user1 -print
searches the directory /local and its subdirectories for files called core belonging
to the user user1 and writes their full file names to the standard output.
ftp - file transfer program
ftp is an interactive file transfer program. While logged on to one system
(described as the local system), ftp is used to logon to another system
(described as the remote system) that files are to be transferred to or
from. As well as file transfers, it allows the inspection of directory
contents on the remote system. There are numerous options and commands
associated with ftp, and man ftp will give details of those.
WARNING! When you use ftp the communications between the systems are not
encrypted. This means that your password could be snooped if you use it make
an ftp connection. If you wish to transfer files between two systems where you
have accounts it is better to use the commands sftp (secure file transfer program)
or scp (secure remote file copy program) if available, as they provide encrypted
file transfer. See the section on ssh for examples.
Some systems offer a service called "anonymous ftp", usually to allow general
access to certain archives. To use such a service, enter anonymous instead of
your username when you ftp to the system. It is fairly standard practice for the
remote system to ask you to give your email address in place of a password.
Once you have logged on you will have read access in a limited set of directories,
usually within the /pub directory tree. It is good etiquette to follow the guidelines
laid down by the administrators of the remote system.
A simple example anonymous ftp session is shown below:-
ftp isccp.giss.nasa.gov
If the connection to the remote system isccp.giss.nasa.gov is established,
it will respond with the prompt:-
Name (isccp.giss.nasa.gov:user1)
(supposing user1 is your username on your local system). Enter anonymous and press Return.
You will then be asked to enter your email address instead of a password.
After logging in some Unix commands, such as cd and ls, will be available. Other useful commands are:
help
lists the commands available to you while using ftp
get remote1 local1
creates a copy on your local system of the file remote1 from the remote system.
On your local system this new file will be called local1. If no name is specified
for the file on the local system, it will be given the same name as the file on the remote system.
quit
finishes the ftp session. bye and close can also be used to do this.
See File transfer for more detailed examples of using ftp and other methods of file transfer.
grep - searches files for a specified string or expression
grep searches for lines containing a specified pattern and, by default, writes them to the standard output.
grep motif1 file1
searches the file file1 for lines containing the pattern motif1.
If no file name is given, grep acts on the standard input. grep
can also be used to search a string of files, so
grep motif1 file1 file2 ... filen
will search the files file1, file2, ... , filen, for the pattern motif1.
grep motif1 a*
will search all the files in the current directory with names beginning with 'a' for the pattern motif1.
grep -c motif1 file1
will give the number of lines containing motif1 instead of the lines themselves.
grep -v motif1 file1
will write out the lines of file1 that do NOT contain motif1.
gzip - compress a file
gzip reduces the size of named files,
replacing them with files of the same name extended by .gz
. The amount of space saved by compression varies.
gzip file1
results in a compressed file called file1.gz, and deletes file1.
gzip -v file2
compresses file2 and gives information, in the format shown below,
on the percentage of the file's size that has been saved by compression:-
file2 : Compression 50.26 -- replaced with file2.gz
To restore files to their original state use the command gunzip.
If you have a compressed file file2.gz, then
gunzip file2
will replace file2.gz with the uncompressed file file2.
help - display information about bash builtin commands
help gives access to information about builtin commands in the bash shell.
Using help on its own will give a list of the commands it has information about.
help followed by the name of one of these commands will give information about that commands.
help history, for example, will give details about the bash shell history listings.
info - read online documentation
info is a hypertext information system.
Using the command info on its own will enter the info system, and give a list of the major subjects it
has information about. Use the command q to exit info. For example, info bash will give details about the bash shell.
kill - kill a process
To kill a process using kill requires the process id (PID). This can be found by using ps.
Suppose the PID is 3429, then
kill 3429
should kill the process.
lpr - print out a file
lpr is used to send the contents of a file to a printer. If the printer is a laserwriter,
and the file contains PostScript, then the PostScript will be interpreted and the results of
that printed out.
lpr -Pprinter1 file1
will send the file file1 to be printed out on the printer printer1. To see the status of the job
on the printer queue use
lpq -Pprinter1
for a list of the jobs queued for printing on printer1. (This may not work for remote printers.)
ls - list names of files in a directory
ls lists the contents of a directory, and can be used to obtain information on the files and directories
within it.
ls dir1
lists the names of the files and directories in the directory dir1, (excluding files whose names begin with . ).
If no directory is named, ls lists the contents of the current directory.
ls -R dir1
also lists the contents of any subdirectories dir1 contains.
ls -a dir1
will list the contents of dir1, (including files whose names begin with . ).
ls -l file1
gives details of the access permissions for the file file1, its size in kbytes,
and the time it was last altered.
ls -l dir1
gives such information on the contents of the directory dir1.
To obtain the information on dir1 itself, rather than its contents, use
ls -ld dir1
man - display an on-line manual page
man displays on-line reference manual pages.
man command1
will display the manual page for command1, e.g man cp, man man.
man -k keyword
lists the manual page subjects that have keyword in their headings.
This is useful if you do not yet know the name of a command you are
seeking information about.
man -Mpath command1
is used to change the set of directories that man searches for manual
pages on command1
mkdir - make a directory
mkdir is used to create new directories. In order to do this you must have write permission
in the parent directory of the new directory.
mkdir newdir
will make a new directory called newdir.
mkdir -p can be used to create a new directory, together with any parent directories required.
mkdir -p dir1/dir2/newdir
will create newdir and its parent directories dir1 and dir2, if these do not already exist.
more - scan through a text file page by page
more displays the contents of a file on a terminal one screenful at a time.
more file1
starts by displaying the beginning of file1. It will
scroll up one line every time the return key is pressed,
and one screenful every time the space bar is pressed. Type ?
for details of the commands available within more. Type q if you
wish to quit more before the end of file1 is reached.
more -n file1
will cause n lines of file1 to be displayed in each screenful instead
of the default (which is two lines less than the number of lines that will fit into the terminal's screen).
mv - move or rename files or directories
mv is used to change the name of files or directories, or to move them
into other directories.
mv file1 file2
changes the name of a file from file1 to file2 unless dir2 already exists,
in which case dir1 will be moved into dir2.
mv dir1 dir2
changes the name of a directory from dir1 to dir2.
mv file1 file2 dir3
moves the files file1 and file2 into the directory dir3.
nice - change the priority at which a job is being run
nice causes a command to be run at a lower than usual priority.
nice can be particularly useful when running a long program that
could cause annoyance if it slowed down the execution of other users'
commands. An example of the use of nice is
nice gzip file1
which will execute the compression of file1 at a lower priority
If the job you are running is likely to take a significant time,
you may wish to run it in the background, i.e. in a subshell. To do this,
put an ampersand & after the name of your command or script. For instance,
rm -r mydir &
is a background job that will remove the directory mydir and all its contents.
The command jobs gives details of the status of background processes, and the
command fg can be used to bring such a process into the foreground.
passwd - change your password
Use passwd when you wish to change your password. You will be prompted once for your
current password, and twice for your new password. Neither password will be displayed on the screen.
ps - list processes
ps displays information on processes currently running on your machine.
This information includes the process id, the controlling terminal (if there is one),
the cpu time used so far, and the name of the command being run.
ps
gives brief details of your own processes in your current session.
To obtain full details of all your processes, including those from previous sessions use:-
ps -fu user1
using your own user name in place of user1.
ps is a command whose options vary considerably
in different versions of Unix (such as BSD and SystemV).
Use man ps for details of all the options available on the machine you are using.
pwd - display the name of your current directory
The command pwd gives the full pathname of your current directory.
quota - disk quota and usage
quota gives information on a user's disk space quota and usage.
quota
will only give details of where you have exceeded your disc quota on local disks, whereas
quota -v
will display your quota and usage, whether the quota has been exceeded or not,
and includes information on disks mounted from other machines, as well as the local disks.
rm - remove files or directories
rm is used to remove files. In order to remove a
file you must have write permission in its directory, but it is not necessary to have read or
write permission on the file itself.
rm file1
will delete the file file1. If you use
rm -i file1
instead, you will be asked if you wish to delete file1,
and the file will not be deleted unless you answer y. This is a useful safety check when deleting lots of files.
rm -r dir1
recursively deletes the contents of dir1, its subdirectories,
and dir1 itself, and should be used with suitable caution.
rmdir - remove a directory
rmdir removes named empty directories.
If you need to delete a non-empty directory rm -r can be used instead.
rmdir exdir
will remove the empty directory exdir.
sort - sort and collate lines
The command sort sorts and collates lines in files,
sending the results to the standard output. If no file names are given, sort acts on the standard input.
By default, sort sorts lines using a character by character comparison, working from left to right,
and using the order of the ASCII character set.
sort -d
uses "dictionary order", in which only letters, digits, and white-space characters are considered in the comparisons.
sort -r
reverses the order of the collating sequence.
sort -n
sorts lines according to the arithmetic value of leading numeric strings.
Leading blanks are ignored when this option is used, (except in some System V versions of sort,
which treat leading blanks as significant. To be certain of ignoring leading blanks use sort -bn instead.).
ssh - secure remote access
ssh (also known as slogin) is used for logging onto a remote system,
and provides secure encrypted communications between the local and remote systems using the SSH protocol.
The remote system must be running an SSH server for such connections to be possible. For example,
ssh linux.pwf.cam.ac.uk
initiates a login connection to a MCS Linux server.
You can authenticate access by using your password for the remote system,
or you can set up a passphrase to avoid typing the login password directly
(see the man page for ssh-keygen for information on how to create these).
If you wish to transfer files over an encrypted connection you can use sftp
(secure remote file transfer program) or scp (secure remote file copy program),
with authentication being handled as for ssh. For example, you could use sftp to
connect to the remote system sftp.pwf.cam.ac.uk:
sftp sftp.pwf.cam.ac.uk
Once you have authenticated access to sftp.pwf.cam.ac.uk,
you will be in your home directory on the MCS. You can use the command
{\bf cd} to change directories on sftp.pwf.cam.ac.uk and lcd to change directories on your
local system; get can be used to transfer files from the remote system, and put to transfer
files to the remote system. The command quit will terminate the sftp session.
Alternatively, you could use scp to transfer files. In this example scp is used to transfer
a copy of the file file1 in your home directory on the remote system linux.pwf.cam.ac.
uk to the current directory on the local system, naming the file newfile1.
scp linux.pwf.cam.ac.uk:file1 newfile1
Similarly, if you wish to copy the local file file2 to the remote system,
calling the copy newfile2, you can use the command
scp file2 linux.pwf.cam.ac.uk:newfile2
tar - create and use archives of files
tar can be used to create and manage an archive of a set of files.
tar cf archive1.tar
creates an archive file called archive1.tar containing the contents of the current directory
(and any subdirectories it contains). The c option stands for "create" and the f for "filename".
tar cf archive2.tar mydir
creates an archive file called archive2.tar containing the contents of the directory mydir.
tar tvf archive1.tar
lists the contents of the archive file archive1.tar. The t stands for "list" and the v for "verbose listing".
tar xf archive1.tar
extracts the contents of archive1.tar and copy them into the current directory. The x stands for "extract".
tar xf archive1.tar file2
extracts file2 from archive1.tar (if file2 is in the archive).
tar uf archive1.tar file2
If file2 is not already in the archive it will be added. The u stands for "update".
If there is already a file called file2 in the archive, file2 will be appended to the archive if it has
a more recent timestamp than the file2 already in the archive. This means the most recent version of
file2 will be obtained when file2 is extracted from the archive.
0 comments:
Post a Comment