Unix as Sing
-
Upload
pritam-sondhiya -
Category
Documents
-
view
217 -
download
0
Transcript of Unix as Sing
-
8/16/2019 Unix as Sing
1/32
1.Wap to print fibonacci series.
echo "Enter the number"
read n
a=1
b=-1
while [ $n -ne 0 ]
do
s=`expr $a + $b`
b=$a
a=$s
echo $s
n=`expr $n - 1`
done
2. Wap to take string as an input for finding its length.
-
8/16/2019 Unix as Sing
2/32
echo "Enter any strin to !ind the lenth"
read s
echo "he lenth o! the strin is#"
expr "$s" # %&'
3.Wap by using a switch case to construct a calculator.
echo "Enter two number"read aread b
echo "press 1 !or add"echo "press ( !or subtract"echo "press ) !or multiplication"echo "press * !or diision"read ocase $o in1,
echo `expr $a + $b` ''(,
echo `expr $a - $b`''),
echo `expr $a & $b` ''*,
echo `expr $a $b` ''&, echo "wron choice" ''esac
-
8/16/2019 Unix as Sing
3/32
4.Wap to find whether the number is armstrong or not%
echo 'Enter armstrong number='read na=$nr=0s=0
while [ $a -gt 0 ]do
r=`expr $a % 10`s=`expr $s $r !" $r !" $r`a=`expr $a # 10`done
i [ $s -e $n ]thenecho 'armstrong'elseecho 'not armstrong'&
-
8/16/2019 Unix as Sing
4/32
5. Wap by using grep command with both logical operators && and ||.
grep e !hod "| hi! compsc
grep e !study .# student! compsc
$. Write and e%ecute the following '() *ommands
*reate two files with the name of name.t%t+ which contains only names+ and reg.t%t
with the content of register number respecti,ely.
-
8/16/2019 Unix as Sing
5/32
• .ombine the two !iles in the !orm o! reister number !ollowed by name
• /ort the two !iles in ascendin order
-
8/16/2019 Unix as Sing
6/32
• .ount the number o! lines in the !iles
-ename the two files
-
8/16/2019 Unix as Sing
7/32
. Write and e%ecute the following '() *ommands
*reate two files
/isplay the contents of both the files
*ount the number of characters in both the files
-
8/16/2019 Unix as Sing
8/32
-ename the two files
*ombine the two files without duplicate
-
8/16/2019 Unix as Sing
9/32
0. Write and e%ecute the following '() *ommands
*reate a file
rint the specified number of lines of a file from start to end of the file
/isplay the file content with line number
pdate the file content with ( redirection
-
8/16/2019 Unix as Sing
10/32
-ename the file
. Write and e%ecute the following '() *ommands
*reate a directory
-
8/16/2019 Unix as Sing
11/32
*reate a file
-ename the directory
-ename the file
iew the file
-
8/16/2019 Unix as Sing
12/32
10, rite and execute the !ollowin 234 .ommands
• .reate a !ile
• pdate the !ile
• 5isplay the !ile content
• 5isplay the !ile with line number
-
8/16/2019 Unix as Sing
13/32
• .ount the number o! words in the !ile
11, rite and execute the !ollowin 234 .ommands
• .reate a !ile
• 6ename a !ile
• .hane the mode o! the !ile to read only
-
8/16/2019 Unix as Sing
14/32
• 7iew the content o! a !ile
• .ount the number o! words in !ile
1(, rite and execute the !ollowin 234 .ommands
• .reate a 5irectory called main
-
8/16/2019 Unix as Sing
15/32
• .reate a sub-directory called sub in the main directory
• .reate a !ile in the main directory
• .reate the !ile to the sub directory
• 5elete the !ile in the main directory
-
8/16/2019 Unix as Sing
16/32
-
8/16/2019 Unix as Sing
17/32
• 5isplay the present wor8in directory
• 5isplay your user name
• .reate a !ile usin cat command
-
8/16/2019 Unix as Sing
18/32
1*, rite and execute the !ollowin 234 .ommands
• .reate two directories called ./1101 and ./110(
• .reate a !ile called ./1101%txt in the ./1101 directory
• .reate the !ile ./1101%txt !rom ./1101 to ./110(
• pdate the !ile content o! ./1101%txt in the ./110( directory
-
8/16/2019 Unix as Sing
19/32
• 6ename the !ile in the ./110( with ./110(%txt
19, rite and execute the !ollowin 234 .ommands
• .reate a directory with the name o! Exam
• .hane the Exam directory as the wor8in directory
-
8/16/2019 Unix as Sing
20/32
-
8/16/2019 Unix as Sing
21/32
• .reate a !ile
• 7iew only !iles !rom a directory
• 6ename a !ile
• 7iew all !iles startin with a speci!ic character
-
8/16/2019 Unix as Sing
22/32
• 7iew all the !iles which has a speci!ic extension
1;, rite and execute the !ollowin 234 .ommands
• 5emonstrate pipe command
• 5emonstrate tee command
-
8/16/2019 Unix as Sing
23/32
• se more than one command at a time < who and date ,
• .reate a !ile called create%txt
• oe the !ile create%txt to moe%txt
• 1>% rite script to !ind out biest number !rom ien three numbers% 2umbers are supplies as
command line arument% ?rint error i! su!!icient aruments are not supplied%
• i! [ $@ -ne ) ]
• then
• echo "?lease ie ) command line aruments"
• exit 1'
• !i
• i! [ $1 -t $( ] AA [ $1 -t $) ]
• then
-
8/16/2019 Unix as Sing
24/32
• echo " $1 is the reatest number "
• eli! [ $( -t $) ] AA [ $( -t $1 ]
• then
• echo " $( is the reatest number "
• else
• echo "$) is the reates number"
• !i
• 1B% Cow to write shell script that will add two numbersD which are supplied as command
line arumentD and i! this two numbers are not ien show error and its usae%
i! [ $@ -ne ( ] ' then
echo -e " please proide correct number o! aruments"
else
@ $1 is !irst arument and $( is second %
echo " sum o! $1 + $( is `expr $1 + $(` "
!i
-
8/16/2019 Unix as Sing
25/32
• (0% ind a ien !ile and show its permission and last modi!ied 5ate 0*%0)%(01:
• (1% rite a shell script to chec8 whether a ien !ile is exist or notF
• ((% Enter a number o! names and sort them then display on the screen%
• ()% rite a /hell proram to chec8 the ien number is een or odd
echo -n "Enter numnber # "read nrem=$
-
8/16/2019 Unix as Sing
26/32
(9% rite a /hell proram to !ind the area and circum!erence o! a circle
echo "Enter the radius o! the circle"read rpi=)%1*c=$
-
8/16/2019 Unix as Sing
27/32
s=`expr $s + $r K& $r`doneecho "sum o! sHuare o! the number is # $s"
20. Write a 6hell program to find the largest among three numbers
echo 8nter 3 numbers with spaces in between
read a b c
l9:a
if ; :b gt :l <
then
l9:b
fi
if ; :c gt :l <
then
l9:c
fi
echo =agest of :a :b :c is :l
(B% rite a /hell proram to !ind the !actorial o! a number
echo "Enter the alue !or n"read n
-
8/16/2019 Unix as Sing
28/32
s=1
!or
-
8/16/2019 Unix as Sing
29/32
read becho "be!ore swap"echo "a=$a"echo "b=$b"temp=$a
a=$bb=$tempecho "a!ter swap"echo "a=$a"echo "b=$b"
echo "enter two numbers to swap without usin temp% ariable"
read aread bc=`expr $a K& $b`a=`expr $c $a`b=`expr $c $b`echo "a!ter swap"echo "a=$a"echo "b=$b"
-
8/16/2019 Unix as Sing
30/32
32. Write a 6hell program to find the sum of all numbers between 5> and 1>>+ which aredi,isible by 3 and not di,isible by 5.
echo "the sum o! numbers !rom 90 to 100 which are diisible bye ) but not diisible by 9"s=0!or
-
8/16/2019 Unix as Sing
31/32
))% rite script to see current date D time D shell and directory%
echo "CelloD $MNO2PE"echo ".urrent date is `date`"echo "ser is `who i am`"echo ".urrent direcotry `pwd`"
)*% Cow to calculate 9%1( + 9%( real number calculation at dollar prompt in shell%
)9% Cow to per!orm real number calculation in shell script and store the result in third ariableDletsay a=9%::D b=>%:; D c=a+bFa=5.66
-
8/16/2019 Unix as Sing
32/32
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
(9% rite a /hell proram to chec8 the ien inteer is prime or notecho "Enter a number# "read numi=(!=0while [ $i -le `expr $num (` ]
doi! [ `expr $num G $i` -eH 0 ]then!=1!ii=`expr $i + 1`donei! [ $! -eH 1 ]thenecho "he number is composite"else
echo "he number is ?rime"!i