You are on page 1of 9

1. Take number and print number of significant digits in number.

For
example if user enters 00234 print 3.

PROGRAM :
clear
echo "Enter a number:"
read n
dec=0
n=`echo $n | sed 's/^0*//'`
case $n in
*[.]* ) n=`echo $n | sed 's/0*$//'`
dec=1
;;
esac
count=`expr length $n`
if [ $dec -eq 1 ]
then
count=`expr $count - 1`
f
echo "The num is $count long"

OUTPUT :

2. Write script to print table of n (n is input by the user) in the following format:
2*1=2
2*2=4
...
2 * 10 = 20
PROGRAM :
clear
echo "Enter a number:"
read n
x=`expr $n \* 10`
c=`expr length $x`
count=1
while [ $count -le 10 ]
do
r=`expr $n \* $count`
printf "%d * %2d = %`echo $c`d\n" "$n" "$count" "$r"
count=`expr $count + 1`
done
OUTPUT :

3. Write a shell script to implement a command Del, which


deletes files just like rm. It should take arguments, i.e.
filenames just like rm with wild card options. When you type
Del on your terminal, it prompts for your confirmation for each
file to be deleted, including directory files (this is why you should
not use -i option of rm).
Set up a test directory, with several files in it, at least one of
which is a subdirectory, with at least one file there. Then type
Del *.

PROGRAM
delete()
{
pd=$1
del $1
rm filelist
cd ..
rm -r $pd
}
del()
{
if [ -f $1 ]
then
echo "Do you want to delete the file $1 (y/n)?"
read choice
if [ "$choice" = "y" -o "$choice" = "Y" ]
then
rm $1
rm filelist
fname=`ls`
echo $fname | cat > filelist
sed -i 's/filelist//' filelist
echo "File $1 deleted"
else
echo "File $1 not deleted"
fi
fi
if [ -d $1 ]
then
cd $1
flist=filelist
echo "PRESENT WORKING DIR IS $1"
fname=`ls`
echo $fname | cat > filelist
sed -i 's/filelist//' filelist
nooffile=`wc -w filelist`
nooffile=`echo $nooffile | sed 's/filelist//'`
temp=`cut -d" " -f1 filelist`
while [ $nooffile -gt 0 ]
do
temp=`cut -d" " -f1 filelist`
nooffile=`wc -w filelist`
nooffile=`echo $nooffile | sed 's/filelist//'`
if [ $nooffile -eq 0 ]
then
rm filelist
cd ..
rm -r $1
break
fi
echo "NO OF FILES IN $1 DIRECTORY IS $nooffile"
cat filelist
del $temp
done
fi
}

OUTPUT

4. Write shell script to display a menu to show various system


configuration like
1) Currently logged user and the login-name
2) Your current shell
3) Your home directory
4) Your operating system type
5) Your current path setting
6) Your current working directory
7) Show Currently logged number of users
8) About your os and version, release number, kernel version
9) Show computer cpu information like processor type, speed etc
10) Show memory information
11) Show hard disk information like size of hard-disk, cache
memory, model etc
12) File system (Mounted)
When user select an option from the menu, the related information
will be displayed on the screen.
PROGRAM :
while [ true ]
do
echo " 1) Currently logged user and the login-name"
echo " 2) Your current shell"
echo " 3) Your home directory"
echo " 4) Your operating system type"
echo " 5) Your current path setting"
echo " 6) Your current working directory"
echo " 7) Show Currently logged number of users"
echo " 8) About your os and version, release number, kernel version"
echo " 9) Show computer cpu information like processor type, speed etc"
echo "10) Show memory information"
echo "11) Show hard disk information like size of hard-disk, cache memory, model etc"
echo "12) File system (Mounted)"
echo "13) To exit"
echo "Enter your choice "
read choice
case $choice in
1) echo "Currently logged user and the login-name:"
who;;
2) echo -n "Current shell is "
echo $SHELL|cut -d/ -f3-;;
3) echo "Home directory is "
echo "$HOME";;
4) echo "Operationg system type "
echo "$OSTYPE";;
5) echo "Current path is "
echo "$PATH";;
6) echo "Current working directory "
pwd;;
7) echo "Currently logged no. of users are "
who | wc -l;;
8) echo "Operationg System info: "
uname -ovrs;;
9) echo "Computer CPU Information:"
lscpu;;
10) echo "Computer Memory Information:"
cat /proc/meminfo;;
11)echo "Hard disk information:"
cat /proc/scsi/scsi;;
12) echo -e "File System (Mount):"
cat /proc/mounts;;
13) exit;;

*) echo "Invalid opetion entered."


esac
read -p "Press enter to continue..." ch
done

OUTPUT :
suju@suju-LENOVO-G580:~ > sh q4.sh
1) Currently logged user and the login-name
2) Your current shell
3) Your home directory
4) Your operating system type
5) Your current path setting
6) Your current working directory
7) Show Currently logged number of users
8) About your os and version, release number, kernel version
9) Show computer cpu information like processor type, speed etc
10) Show memory information
11) Show hard disk information like size of hard-disk, cache memory, model etc
12) File system (Mounted)
13) To exit
Enter your choice
1
Currently logged user and the login-name:
suju
tty8
2014-08-12 08:16 (:0)
suju
pts/2
2014-08-12 08:17 (:0)
suju
pts/3
2014-08-12 08:56 (:0)
Press enter to continue...
2
Current shell is bash
Press enter to continue...
3
Home directory is
/home/suju
Press enter to continue...
4
Operationg system type
linux-gnu
Press enter to continue...
5
Current path is
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Press enter to continue...
6
Current working directory
/home/suju
Press enter to continue...
7
Currently logged no. of users are
3
Press enter to continue...
8
Operationg System info:
Linux 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 GNU/Linux
Press enter to continue...
9
Computer CPU Information:
Architecture:
x86_64
CPU op-mode(s):
32-bit, 64-bit
Byte Order:
Little Endian
CPU(s):
4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s):
1
NUMA node(s):
1
Vendor ID:
GenuineIntel
CPU family:
6
Model:
58
Stepping:
9
CPU MHz:
1200.000
BogoMIPS:
4988.67
Virtualization:
VT-x
L1d cache:
32K

L1i cache:
32K
L2 cache:
256K
L3 cache:
3072K
NUMA node0 CPU(s):
0-3
Press enter to continue...
10
Computer Memory Information:
MemTotal:
3891804 kB
MemFree:
1419580 kB
Buffers:
131952 kB
Cached:
1157160 kB
SwapCached:
0 kB
Active:
1295840 kB
Inactive:
797060 kB
Active(anon):
805656 kB
Inactive(anon): 145912 kB
Active(file):
490184 kB
Inactive(file): 651148 kB
Unevictable:
32 kB
Mlocked:
32 kB
SwapTotal:
4035580 kB
SwapFree:
4035580 kB
Dirty:
0 kB
Writeback:
0 kB
AnonPages:
803820 kB
Mapped:
347308 kB
Shmem:
147784 kB
Slab:
279156 kB
SReclaimable:
242816 kB
SUnreclaim:
36340 kB
KernelStack:
4184 kB
PageTables:
44464 kB
NFS_Unstable:
0 kB
Bounce:
0 kB
WritebackTmp:
0 kB
CommitLimit:
5981480 kB
Committed_AS: 3368652 kB
VmallocTotal: 34359738367 kB
VmallocUsed:
402656 kB
VmallocChunk: 34359327708 kB
HardwareCorrupted:
0 kB
AnonHugePages:
81920 kB
HugePages_Total:
0
HugePages_Free:
0
HugePages_Rsvd:
0
HugePages_Surp:
0
Hugepagesize:
2048 kB
DirectMap4k:
94004 kB
DirectMap2M:
3944448 kB
Press enter to continue...
11
Hard disk information:
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00
Vendor: ATA
Model: WDC WD5000BPVT-2 Rev: 03.0
Type: Direct-Access
ANSI SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
Vendor: Slimtype Model: DVD A DS8A9SH Rev: EP5C
Type: CD-ROM
ANSI SCSI revision: 05
Host: scsi6 Channel: 00 Id: 00 Lun: 00
Vendor: Multiple Model: Card Reader
Rev: 1.00
Type: Direct-Access
ANSI SCSI revision: 00
Press enter to continue...
12
-e File System (Mount):
rootfs / rootfs rw 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,relatime,size=1930488k,nr_inodes=482622,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=389184k,mode=755 0 0
/dev/disk/by-uuid/e63233f5-54ea-482c-aa72-0eca66652d38 / ext4 rw,relatime,errors=remount-ro,data=ordered 0
0
none /sys/fs/cgroup tmpfs rw,relatime,size=4k,mode=755 0 0
none /sys/fs/fuse/connections fusectl rw,relatime 0 0
none /sys/kernel/debug debugfs rw,relatime 0 0
none /sys/kernel/security securityfs rw,relatime 0 0

none /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0


none /run/shm tmpfs rw,nosuid,nodev,relatime 0 0
none /run/user tmpfs rw,nosuid,nodev,noexec,relatime,size=102400k,mode=755 0 0
none /sys/fs/pstore pstore rw,relatime 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
systemd /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
Press enter to continue...
13
suju@suju-LENOVO-G580:~ >

5. Write a shell script to do the following:


User inputs a word and a filename as argument and your script
searches the word in the file and displays File <filename>
contains <word> or File <filename> does not contain <word>
based on whether file actually contains the given word or not.
It may be assumed that word does not contain any special
character. It
only consists of alphabets and digits. Print error message when
user gives
less or more number of arguments than what is required for script
to
work.
PROGRAM:
if [ $# -gt 2 ]
then
echo "Too many arguments"
exit
elif [ $# -lt 2 ]
then
echo "Too few arguments"
exit
fi
if grep -i -q $1 $2
then
echo "File $2 contains word $1"
else
echo "File $2 doesn't contain word $1"
fi

OUTPUT:

6. Write a shell script that will take number n as input from user
and will print nth prime number as output. Note that 2 is
the first prime number.
PROGRAM
clear
echo "Enter the value of n: "
read n
a=2
num=3
count=1
n=`expr $n - 1`
while [ $count -le $n ]
do
while [ $a -lt $num ]
do
if [ `expr $num % $a` -eq 0 ]
then
num=`expr $num + 1`
continue
fi
if [ $a -eq `expr $num - 1` ]
then
count=`expr $count + 1`
fi
a=`expr $a + 1`
done
a=2
num=`expr $num + 1`
done
echo "`expr $num - 1` is `expr $n + 1` th prime"

OUTPUT

You might also like