Unix Shell Programming Solved

Embed Size (px)

Citation preview

  • 7/27/2019 Unix Shell Programming Solved

    1/14

    1. Write a shell script to print the users logged in, the current date and the calendar forthe year 2010.

    echo "Program to print user logged in, current date and calender"echo "Following are the current users\n `who`"

    echo "\nCurrent date is : `date`\n"set `date`echo "calendar of current year is \n `cal $6`"

    Program to shell script to check the present working directory, then make a directory onthat location, then create 3 files in it and give executable permissions to them.

    echo "present working directory is `pwd`\n"if [ -d newdir ]

    then echo "newdir already exists"

    elsemkdir newdirecho "newdir created"

    fi

    cd newdirecho "Current working director is `pwd`"

    for file in file1 file2 file3do

    echo $file|tee $file

    done

    3. shell script to take input for name, course and university and print it in the format I[name] study in [course] from [university].

    if [ $# -ne 3 ]then

    echo "Number of arguments required are three\n"echo "Enter your name\n"read nameecho "Enter the course\n"read courseecho "Enter the university\n"read university

    elsename=$1course=$2university=$3

    fi

  • 7/27/2019 Unix Shell Programming Solved

    2/14

    echo "I $name study in $course from $university"

    4. shell script which creates one hard link and two soft links of a file.

    echo "Enter name of the file\n"read fileif [ -e $file ]

    thenecho "Creating hard link\n"ln $file $file"link1"

    echo "Creating two soft links\n"ln -s $file $file"link2"ln -s $file $file"link3"

    ls -li

    elseecho "file not present"fi

    5. #shell script that accepts two filenames and your script should copy file1 to file2 anddisplay both the files. (take input from keyboard and command line both).

    if [ $# -ne 2 ]thenecho "Enter name of first file: "read file1echo "Enter name of second file: "read file2

    elsefile1=$1file2=$2

    ficp $file1 $file2

    8. #shell script that should accept the output of the current date and print the output inthe form 2009 Oct Tue

    echo "Current date is `date`"set `date`echo "$6 $2 $1"

    9. #shell script that will display the files of the current directory and number of filesthrough positional arguments

  • 7/27/2019 Unix Shell Programming Solved

    3/14

    if [ -d $1 ]then echo "$1 is a directory"

    elseecho "$1 is not present"

    fi

    cd $1echo "Following are the files present in $1 :\n"ls -lecho "Number of file are `ls|wc -l`"

    10. #shell script using if, copy source to target, if it copies then echo copy processsuccessful else echo unable to copy

    echo "Enter the source file: "

    read snameecho "Enter the target program: "read tnamecp $sname $tname 2> errorif [ $? -eq 0 ]

    then echo "Copy successful"else

    echo "Unable to copy"fi

    11. #shell script that will accept a number and then check if number is less than 10.Display the results accordinglyecho "Enter the number: "read numif [ num -lt 10 ]

    then echo "$num is less than 10"else

    echo "$num is greater or equal to 10"fi

    12. #If Basic Salary=1500, then hra=500, da=98% of Basic.# If the employees basic salary is entered through the keyboard, then write a shell scriptto find the employees gross salary.echo "Enter your basic salary"read salaryif [ $salary -lt 1500 ]

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

    else

  • 7/27/2019 Unix Shell Programming Solved

    4/14

    hra=500da=`expr $salary \* 98 / 100`

    fi

    gross=`expr $salary + $hra + $da`

    echo "Gross salary is: $gross"

    13. #shell script where distance between two cities is entered from keyboard in kms andthen print that distance in meters and cms.echo "Enter the distance b/n two cities in kms: "read distanceecho "Distance in mtrs is : `expr $distance \* 1000`\n"echo "Distance in cms is : `expr $distance \* 100000`"

    14. #shell script that perform all the mathematical operations on two numbers. (take input

    from keyboard and command line both).if [ $# -ne 2 ]then echo "Enter the two numbers: "read num1 num2

    elsenum1=$1num2=$2

    fi

    echo "Addition of numbers is `expr $num1 + $num2`"echo "Subtraction of numbers is `expr $num1 - $num2`"echo "Multiplication of numbers is `expr $num1 \* $num2`"echo "Division of numbers is `expr $num1 / $num2`"

    15. #shell script which check whether the number is even or odd.echo "Enter the number: "read numif [ `expr $num % 2` -eq 0 ]

    then echo "$num is even number"else

    echo "$num is odd number"fi

    16. #shell script which displays the result of student as follows:# If %age is less then 40 then FAIL.# If %age is 40-50 then Third Division.# If %age is 50-60 then Second Division.# If %age is 60-75 then First Division,

  • 7/27/2019 Unix Shell Programming Solved

    5/14

    # If above 75 then Distinction.

    echo "Enter the marks of the student: "read marksif [ $marks -gt 75 ]

    then echo "Distinction"elif [ $marks -gt 59 ]then echo "First Division"

    elif [ $marks -gt 49 ]then echo "Second division"

    elif [ $marks -gt 39 ]then echo "Third division"

    elseecho "Fail"

    fi

    17.. #shell script which accepts the age of a person and checks:# If age is less than 10 yrs then is not allowed.# If age is 10-18 then allowed to swim in junior pool.# If age >18 then senior pool.#

    echo "Enter the age "read ageif [ $age -gt 18 ]

    then echo "Senior pool allowed"elif [ $age -gt 9 ]

    then echo "Junior pool allowed"else

    echo "Not allowed"fi

    18. #shell script that accepts a year and check whether it is a leap year or not.

    echo "Enter the year "read yearif [ `expr $year % 400 ` -eq 0 ]

    then echo "$year is a leap year"elif [ `expr $year % 4` -eq 0 ] && [ `expr $year % 100` -ne 0 ]

    then echo "$year is a leap year"else

    echo "$year is not a leap year"fi

  • 7/27/2019 Unix Shell Programming Solved

    6/14

    19. #shell script which calculate area and perimeter of a rectangle when length andbreadth are entered into through keyboard and through command line

    if [ $# -eq 2 ]then length=$1breadth=$2

    elseecho "Enter length and breadth"read length breadth

    fi

    area=`expr $length \* $breadth`perimeter=`expr 2 \* $length + 2 \* $breadth`echo "Area is $area\n"

    echo "Perimeter is $perimeter"

    20. #shell script which calculate sum of digits of a five-digit number and also checkswhether the number is a palindrome or not.

    echo "Enter the five digit number"read numnum1=$numsum=0while [ $num -gt 0 ]do

    n=`expr $num % 10`sum=`expr $sum + $n`num=`expr $num / 10`rev=$rev$n

    doneecho "Sum of digits is $sum"echo "Reverse of number is $rev"if [ $rev -eq $num1 ]

    then echo "$num1 is a palindrome"else

    echo "$num1 is not a palindrome"fi

    21. #Make a menu driven shell script with# Option 1: Creates a dir d1/d2/d3 where all dir doesnt exist.# Option 2: Display some file f4 in the current dir.# Option 3: Check all the users who have logged in.#

  • 7/27/2019 Unix Shell Programming Solved

    7/14

    choice="4"echo "Enter choice 1 to create a directory\nEnter choice 2 to display file in current directory\nEnter choice 3 to check all logged in users\nEnter choice 4 to exit"

    read choicewhile [ $choice != "4" ]do

    case $choice in1) echo "Enter the directory to be created d1/d2/d3"

    read dircase $dir ind1|d2|d3) if [ -d $dir ]

    then echo "Directory $dir already present"elsemkdir $dir

    fi ;;esac ;;2) echo "Enter the file to be displayed"

    read filenameif [ -e $filename ]then echo "File contents are \n`cat $filename`"elseecho "File not present in directory"

    fi ;;3) echo "Current users are `who`" ;;# *) $choice="y";;

    esacecho "Enter your choice "read choice

    done

    22. #menu driven shell script with#Option 1: Check read permission. If it is there then display the file, if not then give theread permission.#Option 2: Check write permission. If it is there then append the file, if not then give thewrite permission.#Option 3: Check execute permission. If it is there then display the file, if not then say itis a text file.

    echo "Enter the filename : "read filenameif [ ! -e $filename ]

    then echo "$filename not present"else

    echo "Enter choice 1 to check for read permission\n

  • 7/27/2019 Unix Shell Programming Solved

    8/14

    Enter choice 2 to check write permission\nEnter choice 3 to check execute permission\nEnter choice 4 to exit"read choicewhile [ $choice != "4" ]

    docase $choice in1) if [ -r $filename ]

    then echo "$filename is readable"elseecho "$filename not readable"fi ;;

    2) if [ -w $filename ]then echo "$filename is writable"

    echo "Enter text to be appended. To stop press CTRL + d "cat >> $filename

    else echo "$filename not writable"fi ;;

    3) if [ -x $filename ]then echo "$filename is executable"elseecho "$filename is not executable and a text file"

    fi ;;

    esacecho "Enter your choice "read choice

    donefi

    23. #shell script to check whether a file entered from command line is an ordinary file ordirectory. If it is a dir, then check the required permissions and create a dir in it, and inthat sub-dir, create a file. If it is a file, then append the file after checking thepermissions.

    echo "Enter the filename : "read filenameif [ -f $filename ]

    then echo "File is a regular file ."if [ -w $filename ]

    then echo "Enter text to be appended. Press CTRL + d to exit"cat >> $filename

    elseecho "File not editable"

    fielse

  • 7/27/2019 Unix Shell Programming Solved

    9/14

    if [ -d $filename ]then echo "File is a directory"if [ -w $filename ]

    then echo "Enter new file to be created in $filename"read f

    cd $filenametouch $felse

    echo "Directory does not have write permission"fi

    fifi24. #shell script that uses for loop to display the names of files in the current directorywhich are only directory files

    for file in *doif [ -d $file ]

    then echo $filefi

    done

    25. #shell script to check the count of total number of arguments entered if number ofarguments is less than or equal to 5, then echo your name else echo error message, ie,invalid number of arguments. (Use while loop)echo $#while [ $# -gt 0 ]do

    echo "Name"shift

    done

  • 7/27/2019 Unix Shell Programming Solved

    10/14

    26 #Write a shell script where day(In Capitals) is entered in the command line. Check thestring and display:#a) If it is MON display Unix test,# If it is TUE display Computer Organization test,# If it is WED display System Programming test,

    # If it is THUR display Maths test,# If it is FRI display Aptitude test,# then exit.

    echo "Enter the day in capital letters, only first three letters : "read daycase $day in

    MON) echo "Unix test" ;;TUE) echo "COA test" ;;WED) echo "System Programming test" ;;THUR) echo "Maths test" ;;

    FRI) echo "Aptitude test" ;;*) exitesac

    26.b) #Write a shell script where day(In Capitals) is entered in the command line. Checkthe string and display:#b) If it is MON | mon display Unix test,# If it is TUE | tue display Computer Organization test,# If it is WED | wed display System Programming test,# If it is THUR | thur display Maths test,# If it is FRI | fri display Aptitude test,# then exit.#echo "Enter the day in capital letters, only first three letters : "read daycase $day in

    MON|mon) echo "Unix test" ;;TUE|tue) echo "COA test" ;;WED|wed) echo "System Programming test" ;;THUR|thur) echo "Maths test" ;;FRI|fri) echo "Aptitude test" ;;*) exit

    esac

    27) #Write table of 2, and 5 through two ways:#1) Take the number inside the script.#2) Through positional parameter.

    if [ $# -eq 1 ]

  • 7/27/2019 Unix Shell Programming Solved

    11/14

    then num=$1else

    echo "Enter the number 2 or 5 : "read num

    fi

    i=1while [ $i -lt 11 ]

    doecho "$num * $i = `expr $num \* $i`"i=`expr $i + 1`

    done

    28) #Write a shell script which accepts number of arguments. If number of arguments isequal to 5 then only it should displays your name five times (You can try this, first, bydisplaying your name once)?

    echo "Enter number of arguments : "read argswhile [ $args -gt 0 ]

    doecho "name"args=`expr $args - 1`

    done

    29) #Write a shell script to search whether the username passed on command line haslogged in or not, after each minute? If user has logged in display the message and exitotherwise display a message Not logged in Yet (i.e. The process will go inbackground).

    who > userlistgrep "$1" userlistwhile [ $? -ne 0 ]do

    sleep 60echo "$1 has not logged in"who > userlistgrep "$1" userlist

    doneecho "$1 logged in"

    30) #Write a shell script which scans your current directory and the files which endwith .doc, rename those files to .txt. Suppose: the current directory named UNIXcontains the file unix01doc then it should be renamed to unix01txt, similarly for all thefiles of that directory?

  • 7/27/2019 Unix Shell Programming Solved

    12/14

    for file in *.docdo

    mv $file `basename $file doc`"txt"done

    31) #Write a shell script that scans the current directory (or any path ) and the filesending with doc are to be moved to a new directory name "word assignment" and the fileending with .c (its dot c) are to be moved to a new directory named "c assignments"?

    for file in *.docdoflag=1if [ $flag -eq 1 ];

    then mkdir "word_assignment"flag=0

    fimv $file "word_assignment"done

    for file in *.cdoflag=1if [ $flag -eq 1 ];

    then mkdir "c_assignment"flag=0

    fimv $file "c_assignment"

    done

    32) #Write a shell script that scans the current directory & renames all the regular files &not the directory files to the name of the log name i.e. filename. student ?cd "c_assignment"for file in *

    doif [ -d $file ]

    then echo "$file is a directory"elif [ -f $file ] && [ ! -d $file ]

    then mv $file $file".student"fi

    done

  • 7/27/2019 Unix Shell Programming Solved

    13/14

    33) #Write a shell script that takes pattern and filename as command line arguments anddisplays the result appropriately i.e. pattern found/ pattern not found.pattern=$1fname=$2

    grep "$1" $2

    if [ $? -eq 0 ]then echo "$pattern found in $fname"

    elseecho "$pattern not found in $fname"

    fi

    34) #Write a shell script where you check whether the name#a) begins with A and ends with t.#b) begins with S and ends with T.

    #c) any word with fixed length of four characters.#

    echo "Enter name\n"read namelength=`expr "$name" : '.*'`echo "length is $length"

    case "$name" inA*t) echo "name starts with A and ends with t" ;;S*T) echo "name starts with S and ends with T" ;;*) if [ length -eq 4 ]

    then echo "length of $name is 4"fi

    esac

    35) #Write a shell script where you can accept a word and echo the message whether thename begins with a vowel, consonant or a digit?

    echo "Enter name\n"read name

    case "$name" in[AaEeIiOoUu]*) echo "$name begins with a vowel" ;;[0-9]*) echo "$name begins with a digit" ;;[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*) echo "$name begins witha consonant" ;;

    Esac

  • 7/27/2019 Unix Shell Programming Solved

    14/14

    36) #Write a shell script that computes the factorial of a given number.

    echo "Enter the number "

    read numprod=1

    while [ $num -gt 0 ]do

    prod=` expr $prod \* $num `num=` expr $num - 1 `

    done

    echo "Factorial of $num is $prod"