0% found this document useful (0 votes)
19 views8 pages

Shell Scripts for Basic Programming Tasks

The document contains 15 problems involving shell scripting. The problems cover a range of basic to intermediate concepts in shell scripting including: accepting user input, conditional statements, arithmetic operations, loops, command line arguments, generating series, file operations and more. The shell scripts are designed to test the reader's understanding of shell scripting fundamentals.

Uploaded by

Gurmeet Kadian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Shell Scripts for Basic Programming Tasks

The document contains 15 problems involving shell scripting. The problems cover a range of basic to intermediate concepts in shell scripting including: accepting user input, conditional statements, arithmetic operations, loops, command line arguments, generating series, file operations and more. The shell scripts are designed to test the reader's understanding of shell scripting fundamentals.

Uploaded by

Gurmeet Kadian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a shell script that asks user for a number and then prints the English equivalent of the same. (i.e 1 should be print as one 2 be two) echo " please enter a number...\c" read n case "$n" in 1) echo "One";; 2) echo "Two";; 3) echo "Three";; 4) echo "Four";; 5) echo "Five";; 6) echo "Six";; 7) echo "Seven";; 8) echo "Eight";; 9) echo "Nine";; 0) echo "Zero";; *) echo "please enter number between 0 to 9 ";; esac [Link] a shell script for a multiple choice question for the [Link] of the following is the capital of india? a)Delhi b)Amritsar c)Bangalora d)Calcutta e)None of the above echo "Multiple choice question" echo "Which of the following is the capital of India?" echo "a) Delhi" echo "b) Amritsar" echo "c) Bangalore" echo "d) Calcutta" echo "e) None of the above" echo "enter your choice......\c" read p case "$p" in a) echo " Congrates ";; *) echo "Sorry your choice is wrong" esac

[Link] positional parameters and shell script,sum the two numbers passed as command line arguments.(i.e sum 2 5) a=$1 b=$2 c=`expr $a + $b` echo "Sum of two no is $c" Run--------$ ksh sum 2 5 Sum of two no is 7

4. Using positional parameters and shell script, swap the value of two numbers without using third variable. a=$1 b=$2 echo "Before swapping a=$a b=$b" a=`expr $a + $b` b=`expr $a - $b` a=`expr $a - $b` echo "After swapping a=$a b=$b" Run-----$ ksh swap 10 20 Before swapping a=10 b=20 After swapping a=20 b=10

[Link] a shell script that asks user for basic salary of an employee and then prints the net salary (take HRA=30%and DA=80% of basic salary) echo "enter basic salary of an employee.. \c" read a hra=`expr $a \* 30 / 100` da=`expr $a \* 80 / 100` ns=`expr $a + $hra + $da` echo "basic salary=$a and net salary=$ns"

6. # WASS that aska user for the units of electricity used and then prints the net# bill amount for the current month ( take the rate as Rs3 if units are less # than 100 units and Rs5 if the units are more than 100) echo " enter the units of electricity used in current month= \c" read unit if [ $unit -le 100 ] then rs=`expr $unit \* 3` else rs=`expr 100 \* 3` rst=`expr $unit - 100` rs=`expr $rs + $rst \* 5` fi echo "net bill amount Rs=$rs"
7. # WASS to find the type of file # Whether the file is ordinary,directory etc

echo read if [ then echo else echo fi

"enter file name- \c" a -f $a ] "it is ordinary file" "it is directory file"

8. # WASS to find out division of a student if he/she has appeared for five papers echo "enter the name of student= \c" read n echo "enter the roll no of student= \c" read r echo "enter the marks of math hindi english physics chemistry" read m h e p c total=`expr $m + $h + $e + $p + $c` per=`expr $total / 5` clear echo -------------------------------------- echo " Roll= $r" echo " Name= $n" echo "--------------------------------------" echo "subject full marks obtained marks" echo "--------------------------------------" echo "Math 100 $m" echo "Hindi 100 $h" echo "English 100 $e" echo "Physics 100 $p" echo "Chemistry 100 $c" echo "--------------------------------------" echo "Total 500 $total" echo "--------------------------------------" echo "Percentage $per" echo "-------------------------------------- " echo "Final Result \c" if [ then echo fi if [ then echo fi if [ then echo fi if [ then echo fi $per -ge 60 ] "First Division" $per -ge 45 -a $per -lt 60 ] "Second Division" $per -ge 30 -a $per -lt 45 ] "Third Division" $per -lt 30 ] "Fail"

9. # WASS to find the factorial of a number echo "enter a number= \c" read a p=$a f=1 while [ $a -gt 0 ] do f=`expr $f \* $a` a=`expr $a - 1` done echo "factorial of input no $p is $f" [Link] a shell script to find the factorial of a number using command line argument(i.e fact(5)=120) a=$1 f=1 while [ $a gt 0 ] do f=`expr $f \* $a` a=`expr $a 1` done echo factorial of no $1 is $f Run-----$ ksh fact 5 Factorial of no 5 is 120 11. # Using positional parameters and shell script, swap the value of two numbers. a=$1 b=$2 c=$a a=$b b=$c echo

$a $b

Run---------$ ksh swap1 10 20 20 10


5

[Link] the series 1 3 5 7 9 till n using echo "enter the nth term= \c" read n i=1 while [ $i -le $n ] do echo $i i=`expr $i + 2` done

shell scripts

13. # Using system time, cut the column which gives us the time and then wish the # user "good morning',good afternoon and good evening depending upon the time # using shell scripts. echo `date` t=`date +%H` if [ $t -lt 12 -a $t -ge 1 ] then echo "Good morning" fi if [ $t -lt 16 -a $t -ge 12 ] then echo "Good Afternoon" fi if [ $t -le 24 ] then echo "Good evening" fi echo "time= \c" echo `date +%H`

[Link] the series 2 4 6 8 10 till n terms using shell scripts. echo "enter the nth term= \c" read n i=2 while [ $i -le $n ] do echo $i i=`expr $i + 2` done [Link] the Fibonacci series till n terms using first two values 0 and 1. # Fabonacci series till n terms using first teo value as 0 and 1 echo "enter the n terms= \c" read n n=`expr $n - 2` a=0 b=1 f=`expr $a + $b` echo $a echo $b while [ $n -gt 0 ] do echo $f a=$b b=$f f=`expr $a + $b` n=`expr $n - 1` done

output
$ ksh s3 press enter Enter the n terms 7 0 1 1 2 3 5
7

Common questions

Powered by AI

The shell script first prompts the user to enter an employee's basic salary. It then calculates the House Rent Allowance (HRA) as 30% and the Dearness Allowance (DA) as 80% of the basic salary. These values are added to the basic salary to determine the net salary. The script computes these allowances using the expr command for arithmetic operations .

The script prompts for a file name and uses the test command with -f flag to check if the file is ordinary. If not, it assumes the file is a directory. The result is output as a descriptive message to the user .

The shell script takes a number as a command line argument and calculates its factorial using a loop. It multiplies the current number by decremented values until the number is reduced to 1. This product yields the factorial, which is then printed .

The script prompts for an nth term and initializes a counter at 1. It prints the current counter value, then increments it by 2 in each iteration of a loop until reaching the nth term. This generates a sequence of odd numbers .

The script prompts the user to input the number of electricity units used. It checks whether the units are less than or equal to 100, applying a rate of Rs 3 per unit. For units exceeding 100, it calculates the first 100 units at Rs 3, and any additional units at Rs 5. These rates are used in arithmetic expressions to determine the total bill .

The script obtains the current hour using the date command. Depending on the hour, it issues greetings: 'Good morning' for hours before 12, 'Good afternoon' from 12 to less than 16, and 'Good evening' for later hours. The greeting logic is structured through if conditions .

The shell script asks for the student's name, roll number, and scores in five subjects. It calculates the total marks and percentage. Based on the percentage, it assigns division standings: 'First Division' for 60% or higher, 'Second Division' for 45% to less than 60%, 'Third Division' for 30% to less than 45%, and 'Fail' for less than 30% .

The user inputs the nth term, and the script begins at 2. It prints each current iteration value, incrementing by 2 each loop cycle, continuing as long as the iteration value does not exceed the nth term .

The script prompts for n terms, subtracts 2, and initializes the first two Fibonacci numbers, 0 and 1. It iteratively computes the next number by summing the last two numbers in the series, updating these base values each loop cycle, until n terms are printed .

The shell script uses arithmetic operations to swap the values of two numbers without a temporary variable. Initially, it computes the sum of the two numbers, assigns the difference between the sum and one of the numbers to the other number, and vice versa. This effectively swaps their values .

You might also like