Professional Documents
Culture Documents
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 :
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
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
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