0% found this document useful (0 votes)
114 views

UP LAB Programs

This document discusses shell programming using if/else statements and functions. It provides 4 examples of simple shell scripts that perform calculations. The first script reads a user's name and prints a welcome message. The second calculates products, sums, differences and quotients of two numbers. The third adds an if/else conditional to select the calculation. The fourth defines functions for each calculation and calls them conditionally. All examples demonstrate basic shell scripting concepts.
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

UP LAB Programs

This document discusses shell programming using if/else statements and functions. It provides 4 examples of simple shell scripts that perform calculations. The first script reads a user's name and prints a welcome message. The second calculates products, sums, differences and quotients of two numbers. The third adds an if/else conditional to select the calculation. The fourth defines functions for each calculation and calls them conditionally. All examples demonstrate basic shell scripting concepts.
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

AIM: Programs to perform a simple shell programming using If, else

statement and use of function.


THEORY: A worthwhile program needs to talk to the user and require a language. Two very basic words in the shell vocabulary are read, to accept input by the user and echo to output to user. Suppose a name entered, is read by the shell and name is a variable where in it is collected. Now if we use, $name in the echo statement then the value of variable name will be extracted and will be echoed on the screen. read name echo $name. we as owners have the permission to read and write to the file ,but not to execute it. The remedy to this is the chmod command. $chmod 744 filename This commond change the mode or permission available with a file.The new permission for the file are therefore 744. Source 1:
#!/bin/bash #Simple script in bash scripting language. clear echo "please enter your name:" read -n 10 name echo "welcome $name.you are in currentaly `pwd` directory.today is `date +%F`" #End

Output:
root@bt:~/ashabash# chmod +x helloworld.sh root@bt:~/ashabash# ./helloworld.sh please enter your name: asha welcome asha ,you are in currently /root/ashabash directory.Today is 2009"07"12 root@bt:~/ashabash#

Source 2:
#!/bin/bash #Simple automated calculator in bash scripting language clear echo "valid for integers only" echo "plz enter no.1:" read num1 echo "plz enter no.2:" read num2 echo "product is:" num3=`expr $num1 \* $num2` echo $num3 echo "addition is:" num4=`expr $num1 + $num2` echo $num4 echo "subtraction is:" num5=`expr $num1 - $num2` echo $num5 echo "division is:" num6=`expr $num1 / $num2` echo $num6

#End

Output:
root@bt:~/ashabash# chmod +x simplecalc.sh root@bt:~/ashabash# ./simplecalc.sh valid for integers only plz enter no.1: 34 plz enter no.2: 23 product is: 782 addition is: 57 subtraction is: 11 division is: 1 root@bt:~/ashabash#

Source 3:
#!/bin/bash #simple calculator using if-else conditions in bash scripting language. clear echo "plz enter no.1:" read num1 echo "plz enter no.2:" read num2 echo "plz select a choice{1,2,3,4}:" echo "1.product" echo "2.addition" echo "3.subtraction" echo "4.division" read choice if [ $choice -eq 1 ] then echo "product is:" num3=$((num1*num2)) echo $num3 elif [ $choice -eq 2 ] then echo "addition is:" num3=$((num1 + num2)) echo $num3 elif [ $choice -eq 3 ] then echo "subtraction is:" num3=$((num1 - num2)) echo $num3 elif [ $choice -eq 4 ] then echo "division is:" num3=$((num1 / num2)) echo $num3 else fi echo "plz enter valid choice"

Output:
root@bt:~/ashabash# chmod +x calcusingif.sh root@bt:~/ashabash# ./calcusingif.sh plz enter no.1: 23 plz enter no.2:

45 plz select a choice{1,2,3,4}: 1.product 2.addition 3.subtraction 4.division 1 product is: 1035 root@bt:~/ashabash#

Source 4:
#!/bin/bash #simple calculator using conditions and functions in bash scripting language. clear echo "plz enter no.1:" read num1 echo "plz enter no.2:" read num2 echo "plz select a choice{1,2,3,4}:" echo "1.product" echo "2.addition" echo "3.subtraction" echo "4.division" read choice #functions declaration function product() # this is the standard way of function declaration in bash { echo "product is:" num3=$((num1 * num2)) echo $num3 } function addition() { echo "addition is:" num3=$((num1 + num2)) echo $num3 } function subtraction() { echo "subtraction is:" num3=$((num1 - num2)) echo $num3 } function division() { echo "division is:" num3=$((num1 / num2)) echo $num3 }

if [ $choice -eq 1 ] then product #function call elif [ $choice -eq 2 ] then addition elif [ $choice -eq 3 ] then subtraction elif [ $choice -eq 4 ] then division else fi echo "plz enter valid choice"

#End

Output:
root@bt:~/ashabash# chmod +x calcusingfunction.sh root@bt:~/ashabash# ./calcusingfunction.sh plz enter no.1: 45 plz enter no.2: 76 plz select a choice{1,2,3,4}: 1.product 2.addition 3.subtraction 4.division 2 addition is: 121 root@bt:~/ashabash#

You might also like