Wednesday, 14 December 2016

FOSS Lab from Exp:3

PROGRAM-3
SESSION-1
a.       Login to the system.
b.      Use the appropriate command to determine your login shell.
c.       Use the /etc/passwd file to verify the result of step b.
d.      Use the who command and redirect the result to a file called myfile1.use the more command to see the contents of myfile1.
e.       Use the date and who commands in sequence (in one line) such that the output of date will display on the screen and the output of who will be redirected to a file called myfile2. Use the more command to check the contents of myfile2.
Source code:
I.                   Enter userid and password as student.
II.                To determine the shell, use
echo $0àbash   (Bourne again shell)
echo $SHELLà/bin/bash      (path)
III.             Type to verify the result of step II type:- cat /etc/passwd
Student: X: 500: 500: student: /home/student: /bin/bash
Description of the above statement:-
Studentàusername
Xàpassword which is encrypted form.
500àuserid (range: 0-65535)
500àgroupid (range: 0-65535)
0àsuper user. We cannot create super user. It is created by administrator during installation of OS.
Studentàowner of the system.
/home/student:àhome directory
/bin/bashàtype of shell.
IV.             Type “who”
Student pts/0   2015-02-05      16:01(:0.0)
Description of the above statement:-
Studentàsystem name
Ptsàpseudo terminal slave in which terminal (or) to display how many terminals are working on.
2015-02-05àdate
16:01àtime
  • To redirect the result to a file called myfile1, type: - who >myfile1.
  • To display the content, we can use “cat myfile1”. But “cat” displays only last 26 lines and the remaining result should be scrolled.
  • Therefore, by using more command “more myfile1”. It displays page wise with percentage.
V.                To display the date, type date and press enter.
Thu Feb 5                    16:09:23          IST      2015
To display the output of date and who command in sequence,
            date;who
Thu Feb 5                    16:09:23          IST      2015
Student pts/0   2015-02-05      16:01(:0.0)
To display the output of date and the output of who command is redirected to myfile2 type:-
date;who >myfile2
Thu Feb                       16:09:23          IST      2015
To display “who” in myfile2.type “cat myfile2”
Student pts/0   2015-02-05      16:01(:0.0)

SESSION-2
a.       Write a sed command that deletes the first character in each line in a file.
Source code:
Create myfile1 using cat as “cat  >myfile1”
Type some text like:
      Welcome to unix
      Welcome to gvp
To delete the first character in each line type,
      sed ‘s/^.//’ myfile1
      elcome to unix
      elcome to gvp
No change in file but only change in output
                  cat myfile1
Welcome to unix
Welcome to gvp

b.      Write a sed command that deletes the character before the last character in each line in a file.
Source code:
   Create a file using cat as “cat  >file1”
Type some text like:
      Welcome to unix
      Welcome to gvp
To delete the last before character in each line in a file type,
sed ‘s/.\ (.$\)/\1/’ file1
Output:
Welcome to unx
Welcome to gp


c.       Write a sed command that swaps the first and second words in each line in a file.
Source code:
Create a file using cat as “cat >file”
Type some text like:
welcome to unix
welcome to gvp
To swap the first and second words in each line in a file type,
sed ‘s/\([a-z]*\) \([a-z]*\)/\2 \1/’
Output:
to welcome unix
to welcome gvp
PROGRAM-4
b. Develop an interactive grep script that asks for a word, file name and then tells how many lines contain that word.
Source code:
            cat >abc.txt
            harika pravallika
            nikhitha pravallika
            pravallika

            echo “enter a word:”
            read word
            echo “enter a file name:”
            read file
            echo “No. of. Times $word is present in $file”
            grep –c $word $file

Output:
            enter a word:
            pravallika
            enter a file name:
            abc.txt
            No. of. times pravallika is present in abc.txt
            3   

PROGRAM-5
a.       Write a shell script that takes a command-line argument and reports on whether it is directory, a file, or something else.
Source code:
echo “Enter file:”
read str
if test –f $str
then echo “file exists and it is an ordinary file”
elif test –d $str
then echo “it is a directory file”
else
echo “no such directory (or) file exists”
fi
if test –c $str
then echo “character device file”
fi

Output:
Enter file:
f1
file exists and it is an ordinary file

b.      Write a shell script that accepts one or more file name as arguments and converts all of them to upper case, provided they exist in the current directory.
Source code:
echo “Enter file:”
read str
if test –f $str
then echo “file exists and it is an ordinary file”
tr ‘[a-z]’ ‘[A-Z]’ <$str
fi

Output:
cat >pravs.txt
welcome to unix
welcome to gvp
Enter file:
Pravs.txt
File exists and it is an ordinary file
WELCOME TO UNIX
WELCOME TO GVP

PROGRAM-7
a.       Write a shell script that computes the gross salary of a employee according to the following rules:
àIf basic salary is < 1500 then HRA=10% of the basic and DA=90% of the basic.
àIf basic salary is >= 1500 then HRA=Rs. 500 and DA=98% of the basic.
Source code:
echo “enter the basic salary:”
read basicsal
if [ $basicsal –lt 1500 ]
then
hra=`expr $basicsal \* 10 \/ 100`
da=`expr $basicsal \*90 \/ 100`
else
hra=`expr $basicsal + 500`
da=`expr $basicsal \* 98 \/ 100`
fi
gross=`expr $basicsal + $hra + $da`
echo $gross

Output:
enter the basic salary:
1300
2600
enter the basic salary:
1700
5566

b.      x=$1
y=$2
z=1
i=1
if [ $# -eq 0 ]
then
echo “no arguments”
else
      while [ $i –le $y ]
      do
                  z=`expr $z \* $x`
                  i=`expr $i + 1`
      done
fi
echo $z

Output:
                                          sh f112.sh 2 3
                                          8
                                          sh f112.sh
                                          no arguments

                                          1

No comments:

Post a Comment