+ All Categories
Home > Documents > Unix Manual Knsit

Unix Manual Knsit

Date post: 02-Apr-2018
Category:
Upload: john-ramirez
View: 225 times
Download: 0 times
Share this document with a friend

of 20

Transcript
  • 7/27/2019 Unix Manual Knsit

    1/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 1

    UNIX LAB MANUAL

    Instructions for using UNIX

    Step 1: start the linux operating system

    Step 2: click on applications go to system tools click on terminal

    Step 3: type vi program_name.sh

    Step 4: type the program to save it and execute type esc : wq

    Step 5: on the terminal type sh program_name.sh

    Program1a

    Write a non-recursive shell script which accepts any number of arguments and prints them in

    the reverse order (For example, if the script is named args, then executing args A B C should

    produce C B A on the standard output).

    if [ $# -eq 0 ]

    thenecho "No argument"exitfifor i in $*doy=$i" "$ydoneecho $y

    Output

    $ sh 1a.sh 1 2 3

    3 2 1

  • 7/27/2019 Unix Manual Knsit

    2/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 2

    Program1b

    Write a shell script that accepts two file names as arguments, checks if the permissions for

    these files are identical and if the permissions are identical, output common permissions andotherwise output each file name followed by its permissions.

    if [ $# -ne 2 ]thenecho "No arguments"elif [ ! -e $1 -o ! -e $2 ]thenecho "FIle does not exist"elsep1=`ls -l $1|cut -c2-10`p2=`ls -l $2|cut -c2-10`if [ $p1 == $p2 ]thenecho "File permissions are equal & is $p1"elseecho "File permissions are not equal"echo "1st file $p1"echo "2nd file $p2"fifi

    Output

    $ sh 1b.sh a b

    File permissions are equal & is rwxrwxrwx

    Program2a

    Write a shell script that takes a valid directory name as an argument and recursively descend

    all the subdirectories, finds the maximum length of any file in that hierarchy and writes this

    maximum value to the standard output.

    if [ $# -ne 1 ]thenecho "No argument"exitfiif [ ! -e $1 ]thenecho "The given directory does not exist"exit

  • 7/27/2019 Unix Manual Knsit

    3/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 3

    fiecho "The file with maximum length $1 dir is"ls -lR $1|tr -s " "|cut -d " " -f 5,10|sort -n|tail -n 1

    Output

    $ sh 2a.sh sharan

    The file with maximum length sharan dir is

    252

    Program2b

    Write a shell script that accepts a path name and creates all the components in that path

    name as directories. For example, if the script is named mpc, then the command mpc a/b/c/d

    should create directories a, a/b, a/b/c, a/b/c/d.

    if [ $# -ne 1 ]thenecho "Invalid number of arguments"elsemkdir -p $1

    fi

    Output

    $ sh 2b.sh a/b/c/d

    $ls R a

    a

    b

    a/b

    c

    a/b/c

  • 7/27/2019 Unix Manual Knsit

    4/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 4

    d

    a/b/c/d

    Program3a

    Write a shell script which accepts valid log-in names as arguments and prints their

    corresponding home directories, if no arguments are specified, print a suitable error message.

    Clearif [ $# -eq 0 ]then

    echo "No command line argument passed"

    exitfiwhile [ $1 ]do

    cat /etc/passwd | cut -d ":" -f1 | grep "^$1" > tempck=`cat temp`

    if [ "$ck" != "$1" ]then

    echo "ERROR:$1 is an invalid login name"else

    echo "Home Directory for $1 is"echo `cat /etc/passwd | grep "^$1" | cut -d ":" -f6`

    fishiftdone

    Output

    $ 3a.sh student

    Home Directory for student is

    /home/student

  • 7/27/2019 Unix Manual Knsit

    5/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 5

    Program3b

    Write shell script to implement terminal locking (similar to the lock command). It should

    prompt the user for a password. After accepting the password entered by the user, it must

    prompt again for the matching password as confirmation and if match occurs, it must lock the

    keyword until a matching password is entered again by the user, Note that the script must be

    written to disregard BREAK, control-D. No time limit need be implemented for the lock

    duration.

    stty echowhile truedoclearecho "Enter the paasword"

    read pass1echo "Re enter the password"read pass2if [ $pass1 = $pass2 ]thenecho "Terminal locked"echo "To unlock enter the password"pass1=""until [ "$pass1" = "$pass2" ]doread pass1done

    echo "Terminal unlocked"stty echoexitelseecho "Password mismatch retype it"fidone

    Output

    $ sh 3b.shEnter the passwordRe enter the passwordTerminal lockedTo unlock enter the passwordTerminal unlocked

  • 7/27/2019 Unix Manual Knsit

    6/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 6

    Program4a

    Create a script file called file-properties that reads a file name entered and outputs it

    properties.echo "Enter a file name"read fileif [ -f $file ]thenset - -`ls -l $file`echo "file permission : $1"echo "number of links : $2"echo "User name : $3"echo "Owner name : $4"echo "Block size : $5"

    echo "Date of modification : $6 : $7"echo "Time of modification : $8"echo "Name of file : $9"elseecho "File does not exist"fi

    Output

    $ sh 4a.shEnter a file name

    SharanFile permissions : -rw-rw-rNumber of links :1User name :santoshOwner name: santoshBlock size 24Date of modification Nov 28Time of modification 12:49Name of file : sharan

    Program4b

    Write a shell script that accept one or more filenames as argument and convert all of them to

    uppercase, provided they exist in current directory.

    for f in $*do

  • 7/27/2019 Unix Manual Knsit

    7/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 7

    if [ -e $f ]thencat $f|tr "[a-z]" "[A-Z]">tmpmv tmp $f

    elseecho "File $f does not exist"fidone

    Output

    $ sh 4b.sh

    Cat >sharanHi how are you$ sh 4b.sh sharanCat sharanHI HOW ARE YOU

    Program5a

    Write a shell script that displays all the links to a file specified as the first argument to the

    script. The second argument, which is optional, can be used to specify in which the search is

    to begin. If this second argument is not present, the search is to begin in current workingdirectory. In either case, the starting directory as well as all its subdirectories at all levels

    must be searched. The script need not include any error checking.

    if [ "$2" != " " ]then

    cwd=`pwd`cd $2link=`ls -l $1 | tr -s " " | cut -d " " -f2`cd $cwd

    else

    link=`ls -l $1 | tr -s " " | cut -d " " -f2`fiecho "Number of linkes of file $1: $link"

  • 7/27/2019 Unix Manual Knsit

    8/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 8

    Output

    $ sh 5a.sh kns

    Number of links of file kns : 41

    Program5b

    Write a shell script that accepts as filename as argument and display its creation time if file

    exist and if it does not send output error message.

    if [ $# -eq 0 ]then

    echo "No argument"exitfiif [ -f $1 ]thentime=`ls -l $1|cut -c 33-59`echo "File $1 has created on $time"elseecho "File $1 does not exist"fi

    Output

    $ sh 5b.sh sharanFile sharan has created on 2010-12-13 14:49 sharan

  • 7/27/2019 Unix Manual Knsit

    9/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 9

    Program6a

    Write a shell script to display the calendar for current month with current date replaced by *

    or ** depending on whether the date has one digit or two digits.

    set `date`y=$3if [ $y -le 9 ]thencal |sed "s/$3/*/"elsecal |sed "s/$3/**/"fi

    Output

    $ sh 6a.shDecember 2010su mo tu we th fr sat

    1 2 3 45 6 * 8 9 10 1112 13 14 15 16 1* 1819 20 21 22 23 24 2526 2* 28 29 30 31

    Program6b

    Write a shell script to find smallest of three numbers that are read from keyboard.

    echo "Enter the Three Numbers"read a b cif [ $a -le $b -a $a -le $c ]then

    echo "$a is Smallest"elif [ $b -le $c -a $b -le $a ]then

    echo "$b is Smallest"else

    echo "$c is Smallest"fi

  • 7/27/2019 Unix Manual Knsit

    10/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 10

    Output

    $ sh 6b.sh

    Enter the three numbers2 31 is smallest

    Program7a

    Write a shell script using expr command to read in a string and display a suitable message if it

    does not have at least 10 characters.

    echo "Enter a String"read strc=`expr "$str" : '.*'`

    if [ $c -lt 10 ]thenecho "The String has Less than 10 charecter"elseecho "The String has $c charecters"fi

    Output

    $ sh 7a.sh

    Enter a stringHi how are youThe string has 14 charecters

    Program7b

    Write a shell script to compute the sum of number passed to it as argument on command line

    and display the result.

    sum=0for i in $*do

    sum=`expr $sum + $i`done

    echo "Sum of Number is : $sum"

  • 7/27/2019 Unix Manual Knsit

    11/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 11

    Output

    $ sh 7b.sh 1 2 3

    Sum of Number is : 6

    Program8a

    Write a shell script that compute gross salary of an employee, accordingly to rule given

    below.

    If basic salary is < 15000 then HRA=10% of basic 7 DA=90% of basic.

    If basic salary is >=15000 then HRA=500 of basic & DA=98% of basic.

    Echoenter Basic salary

    read basicif [ $basic -lt 15000 ]then

    hra=`expr 10 \* $basic / 100`da=`expr 90 \* $basic / 100`

    elsehra=`expr 50 \* $basic / 100`da=`expr 98 \* $basic / 100`

    figs=`expr $basic + $hra + $da`echo "Gross salary : Rs. $gs"

    Output

    $ sh 8a.shEnter Basic salary10000Gross salary : Rs . 20000

  • 7/27/2019 Unix Manual Knsit

    12/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 12

    Program8b

    Write a shell script that delete all lines containing a specific word in one or more file

    supplied as argument to it.if [ $# -eq 0 ]thenecho "Please enter one or more filenames as argument"exitfiecho "Enter the word to be searched in files"read wordfor file in $*dosed "/$word/d" $file | tee tmpmv tmp $filedone

    Output

    $ sh 8b.sh knsEnter the word to be searched in a fileareWhat about you

    Program9a

    Write a shell script that gets executed displays the message either Good Morning or Good

    Afternoon or Good Evening depending upon time at which the user logs in.

    echo "Greetings of the day"echo `date`h=`date |cut -c 12-13`if [ $h -ge 0 -a $h -lt 12 ]thenecho "Good Morning"

    elif [ $h -ge 12 -a $h -lt 18 ]thenecho "Good Afternoon"elif [ $h -ge 18 -a $h -lt 20 ]thenecho "Good Evening"else

  • 7/27/2019 Unix Manual Knsit

    13/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 13

    echo "Good Night"fi

    Output

    $ sh 9a.shGreeting of the dayThe Dec 7 03:01:28 EST 2010Good Morning

    Program9b

    Write a shell script that accept a list of filenames as its argument, count and report

    occurrence of each word that is present in the first argument file on other argument files.

    if [ $# -ne 2 ]then

    echo "Error :invalid no of arguments "exit

    fistr=`cat $1 | tr '\n' ' '`for a in $strdo

    echo "word=$a , count=`grep -c "$a" $2` "done

    Output

    $ sh 9b.sh a bword=Sharanakumar, count=0word=Shridharmurthy,count=1word=Santoshkumar count=1word=Shivaprasad count=0

    Program10aWrite a shell script that determine the period for which a specified user is working on system.

    echo -e "enter the user name :\c"read usrtuser=`who | tr -s " " | head -1 | cut -d " " -f1`if [ "$tuser" = "$usr" ]

  • 7/27/2019 Unix Manual Knsit

    14/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 14

    thentm=`who | tr -s " " | head -1 | cut -d " " -f4`uhr=`echo $tm | cut -d ":" -f1`umin=`echo $tm | cut -d ":" -f2`

    shr=`date "+%H"`smin=`date "+%M"`if [ $smin -lt $umin ]thenshr=`expr $shr - 1`smin=`expr $smin + 60`

    fih=`expr $shr - $uhr`m=`expr $smin - $umin`

    echo "user name : $usr"echo "login period : $h : $m"else

    echo "Invalid User"fi

    Output

    $ sh 10.shEnter the user name :santoshUser name :santoshLogin period :-19:41

    Program10b

    Write a shell script that reports the logging in of a specified user within one minute after

    he/she log in. The script automatically terminate if specified user does not log in during a

    specified period of time.

    a=`date +%M`b=`expr $a + 1`while [ $b -gt `date +%M` ]dowho | grep $1if [ $? -eq 0 ]

    thenecho "$1 has logged in 1 minute"exitfidoneecho "$1 has not looged in within 1 minute"

  • 7/27/2019 Unix Manual Knsit

    15/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 15

    Output

    $ sh 10b.sh santoshSantosh tty1 2010-12-06 21:30 (:0)

    Program11a

    Write a shell script that accepts two integers as its argument and compute the value of first

    number raised to the power of second number.

    if [ $# -ne 2 ]thenecho "Invalid number of arguments"

    exitfipwr=`echo $1^$2 |bc`echo "$1 raised to $2 is : $pwr"

    Output

    $ sh 11a.sh 3 3raised to 3 is :27

    Program11b

    Write a shell script that accept the file name, starting and ending line number as an

    argument and display all the lines between the given line number.

    if [ $# -ne 3 ]thenecho "Pass minimum three arguments"

    exitfic=`cat $1 | wc -l`if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ]thenecho "Invaid Input"exit

  • 7/27/2019 Unix Manual Knsit

    16/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 16

    fised -n "$2,$3p" $1

    Output

    $ sh 11b.sh a 2 5SharanShriSantoshShiva

    Program12a

    Write a shell script that folds long lines into 40 columns. Thus any line that exceeds

    40 characters must be broken after 40th, a \ is to be appended as the indication of folding

    and the processing is to be continued with the residue. The input is to be supplied through a

    text file created by the user.

    {if(length($0)40){printf"%s\n",substr(x,1,40)x=substr(x,41,length(x)-40)}printf"%s\n",x}}

    Output

    $ awkf 12a.sh fileC is the structure oriented language C++

    Is the object oriented language

  • 7/27/2019 Unix Manual Knsit

    17/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 17

    Program12b

    Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in theform if day, month, and year. The script should check the validity of the argument and in the

    case of error, display a suitable message.

    BEGIN {FS="-"f=1;printf("enter date with (mm-dd-yy) format:");getline < "/dev/tty"if(((($3%4!=0) && ($1==2) && ($2>28)) || (($3%4==0) && ($1==2) && ($2>29))) ||

    ((($1==1) || ($1==3) || ($1==5) || ($1==7) || ($1==8) || ($1==10) || ($1==12)) && ($2>31)) ||

    ((($1==4) || ($1==6) || ($1==9) || ($1==11)) && ($2>30)) || ($1

  • 7/27/2019 Unix Manual Knsit

    18/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 18

    flag=0;}if (flag){

    x[j]=$0;printf("\n%s",x[i]);i++;

    }}

    Output

    $ awkf 13a.sh a

    SharanakumarShridharSantoshShiva

    Program13b

    Write an awk script to find out total number of books sold in each discipline as well as total

    book sold using associate array down table as given below.

    Electrical 34 Mechanical-1 67

    Electrical-1 80 Computer Science 43

    Mechanical 65 Civil-1 198

    Civil 198 Computer Science-1 64

    BEGIN{printf "enter the mechanical books:"getline mc

  • 7/27/2019 Unix Manual Knsit

    19/20

    KNS INSTITUTE OF TECHNOLOGY DEPT OF MCA

    Lecturer: Syed Khutubuddin Ahmed Page 19

    if(NR==1)tot1=$2-mcif(NR==2)tot2=$2-ec

    if(NR==3)tot3=$2-csif(NR==4)tot4=$2-ci}{if(NR==4){printf "total mechanical books present:%d\n",tot1printf "total electrical books present:%d\n",tot2printf "total computer books present:%d\n",tot3printf "total civil books present:%d\n",tot4

    }}

    Program14

    Write an awk script to compute gross salary of an employee accordingly to rule given below.

    If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic.

    If basic salary is >=10000 then HRA=20% of basic & DA=50% of basic.

    BEGIN{printf"enter the basic salary:Rs"getline bp

  • 7/27/2019 Unix Manual Knsit

    20/20


Recommended