Shell Programming with Linux

Shailendra Tiwari

. Options -n Do not output the trailing new line. Syntax: echo [options] [string. -e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell) \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab \\ backslash .] Displays text or variables value on screen."echo" Command Use echo command to display text or value of variable.. variables.

fi" Statement ‡ If given condition is true then command1 is executed otherwise command2 is executed..."if. else.0) execute all commands up to else statement else if condition is not true then execute all commands up to fi fi .. Syntax: if condition then condition is zero (true ..

..) ‡ Example: ‡ $ vi isnump_n #!/bin/sh # Script to see whether argument is positive or negative if [ $# -eq 0 ] thenecho "$0 : You must give/supply one integers" exit 1 fi if test $1 -gt 0 then echo "$1 number is positive" else echo "$1 number is negative" fi ."if.. else... fi" Statement (cont.

/ispos_n : You must give/supply one integers $ isnump_n 0 0 number is negative ...."if. else...) ‡ Try it as follows: $ chmod 755 isnump_n $ isnump_n 5 5 number is positive $ isnump_n -45 -45 number is negative $ isnump_n . fi" Statement (cont.

‡ Exercises: 1. 4. Write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.. If cost price and selling price of an item is input through keyboard. Overtime is paid at the rate of Rs.00 per hour for every hour worked above 30 hours. Also check if marks of any of the subject is less than 40 then FAIL should be displayed as result. 40. 2. Also check for NO Overtime or Undertime working. Find the total and percentage. 3. Input the marks obtained by a student in five different subjects through keyboard. . Else statements determine the Grade of the student. Determine that for how many hours employee has worked under-time or over-time. Write a program to calculate overtime pay of an employee. Using If . Write a program to find out whether the given number is an odd or even number.

... ..... ... do this else . do this fi . ‡ Syntax: if condition then if condition then ... . This is called the nesting of ifs... do this fi else ..."Nested if-else-fi" Statement ‡ You can write the entire if-else construct within either the body of the if statement of the body of an else statement.

if within if ###### if [ $osch -eq 2 ] .Example: $ vi nestedif.sh osch=0 echo "1.e. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] . then echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS. then echo "You Pick up Unix (Sun Os)" else #### nested if i." fi fi .

/nestedif. Linux (Red Hat) Select you os choice [1 or 2]? 2 You Pick up Linux (Red Hat) . Linux (Red Hat) Select you os choice [1 or 2]? 1 You Pick up Unix (Sun Os) ‡ $ .Run the above shell script as follows: ‡ $ chmod +x nestedif. Unix (Sun Os) 2.sh 1. Unix (Sun Os) 2.sh $ ./nestedif.sh 1.

Linux (Red Hat) Select you os choice [1 or 2]? 3 What you don't like Unix/Linux OS. .‡ $ . Unix (Sun Os) 2./nestedif.sh 1.

If Salary is between 30000 and 50000. a. PF = 7. . e. HRA = 15% of basic. PF = 3. DA = 8% of basic. b. HRA = 5% of basic. PF = 2. PF = 5% of basic.5% of basic. Tax = 10%. HRA = 14% of basic. Tax = 14%. HRA = 10% of basic.5% of basic. If Salary is between 15000 and 30000. PF = 6. DA = 10% of basic. d. Deduction & Net_Salary with the following conditions.5% of basic.25% of basic. DA = 2. Deduct = PF + Tax & NS = GS Deduct). DA = 6. Tax = 750. DA = 4% of basic. Input basic salary of an employee through keyboard.5%. (GS = BASIC + DA + HRA. HRA = 20% of basic.Exercises: 1. If Salary is between 5000 and 15000. If Salary is greater than 50000. Tax = 16. If Salary is less than 5000. Tax = 12%. c.75% of basic.5% of basic. Calculate Gross_Salary.

If Chem >= 85. If Maths >= 85. If Maths >= 75. c. d. 3. then the Student is Eligible for Chemical Engg. Find the total and percentage.2. course. In the program (2). Phy >= 70 and Percent >= 80. Phy >= 70 and Percent >= 80. If Chem >= 75. Else statements determine the Grade of the student. then the Student is Eligible for Electrical Engg. then the Student is Eligible for Electronics Engg. course. course. make the following modifications to check the eligibility of the course for the student: a. then the Student is Eligible for Medical course. . e. Using If . Phy >= 90 and Percent >= 70.. Input the marks obtained by a student in five different subjects through keyboard. Otherwise Eligible for any other Course . Also check if marks of any of the subject is less than 40 then FAIL should be displayed as result. b. Phy >= 70 and Percent >= 90.

variableN ‡ Example: $ vi sayH # # Script to read your name from key-board # echo "Your first name please:" read fname echo "Hello $fname.... Syntax: read variable1."read" Statement ‡ Use to get input (data from user) from keyboard and store (data) to variable. Lets be friend!" . variable2.

. Exercise: ‡ Write a shell script to sort an array in ascending order.Arrays ‡ Arrays can be declared in UNIX with the declare keyword. To get the elements of the array we have to enclose the array element within { } prefixed with $.

#Script to find the maximum number from an array # Declare the array of 5 subscripts to hold 5 numbers # declare nos[5]=(4 -1 2 66 10) # # Prints the array # echo "Numbers in array:" for (( i = 0. i <= 4. i <= 4 . then max=${nos[$i]} fi done echo The maximum value in an array is $max . i++ )) do echo ${nos[$i]} done # # Finding maximum value # max=${nos[0]} for (( i = 0. i++ )) do if [ $max -lt ${nos[$i]} ].

. Syntax: ‡ case $variable-name in pattern1) command . .... patternN) command .... command. It enables you to match several values against one variable. esac . ..The case Statement The case statement is good alternative to Multilevel if-then-else-fi statement. command.. command. command.. ‡ ... Its easier to read and write... *) command .. . pattern2) command ..

.. B.. or C" . c|C)echo "You entered C" . or C: " read letter case "$letter" in a|A)echo "You entered A" .echo -n "Enter A. esac ‡ Note that esac is always required to indicate end of case statement.. b|B)echo "You entered B" .. *)echo "You did not enter A. B.

then answer=$(echo "scale=2. -. then answer=$(echo "scale=2. done ) .value1-value2" |bc)." ] .value1%value2" |bc). elif [ " $ operator " = " % " ] . *.Simple Calculator ( while [ $ != 'x' ] do echo "Welcome to calculator (x to quit)" echo "Enter the first operand: " read value1 echo "Enter an operator (+. fi echo "Answer: $value1 $operator $value2 = $answer". then answer=$(echo "scale=2. elif [ " $ operator " = " .value1/value2" |bc). /): " read operator echo "Enter the second operand: " read value2 if [ " $ operator " = " + " ] .value1+value2" |bc). elif [ " $ operator " = " / " ] . then answer=$(echo "scale=2. elif [ " $ operator " = " * " ] .value1*value2" |bc). then answer=$(echo " scale=2.

Sign up to vote on this title
UsefulNot useful