8:18 PM Addition of two numbers using shell scripting in UNIX | |||||
Hello all We will see a very simple example to add two numbers using shell scripting. Here we will accept two numbers from the user and display the result on the console We will create a simple script with name test.sh and type below lines in it. #!/usr/bin/ksh echo "Enter first number:" read a echo "Enter second number:" read b sum=$(($a+$b)) echo "Result is:" echo $sum Now we will try to understand the above script. First line is the Shebang line which is telling what shell we r using. Currently it is Korn shell, that is why it is #!/usr/bin/ksh . Now we are reading two numbers a and b from user prompt using read command. The vlaue read from user are stored into a and b respectively. The vlaue of any variable can be accessed using $<varname>. So the summation is done using $(($a+$b)) and the value is stored into sum. Finally the result vlaue is displayed as $sum To execute this script , you first need to the set the execute permission for this script because by default the user does not have execute permission on the file. Promt:>ll test.sh
The above command adds the execute permission to the user. Prompt:>ll test.sh Next we will see another method where we will be passing the two numbers as arguments to the script. Create a file with test2.sh and put below lines: #!/usr/bin/ksh To execute this script we will send the two numbers as argument. So, we will execute like this:
The result will be : 90 Lets see another example which will help us understand more on shell scripting. 2. Accept one more positional parameters and and verify the parameter supplied and depending on several conditions proceed further or exit. For example, we will accept one file name as first argument to the script and then will do following things:
No arguments were given. At least one argument needed. Usage: script-name filename
See below program:
Here, we have to remember below things:
Prompt:> ./checkFile1.sh 2. Second run: Prompt:> ./checkFile1.sh someAsciiFile.txt 3. Third Run: Prompt:> ./checkFile.sh someAsciiFile.txt The program will exit
**************************Case syntax ************************* We will see a simple example on this and try to understand the "case syntax".
So the syntax is: case expression in case1) command1;; case2) command2;; *) print "Give poper choice!!";; esac *********************For loop syntax*************************** Lets see some examples of basic for loops in shell scripting: The syntax is: for variable in range; do #some coding.... done The range could be a set of strings, a set of numbers, commands etc. The for loop will traverse the range and for that range it will perform steps written between do and done Example 1: #!/usr/bin/ksh for name in shanky priyanka thiru;
server1:/home/shanky/test:>./forloop.sh
server1:/home/user1/test:>cat forloop2.sh for i in {1..10}; The above program will print all numbers given in the range as {n1..n2}. Example 3: #!/usr/bin/ksh for i in `ls -aF`; do
The above program wil run the command in loop for all listed files in the current directory. Here we are specifying the commands ls -aF as the range for for loop.
***********************************While LOOP Syntax****************************************8
The output will be: shanky@linux-host:/home/shanky:> ./whileLoop.ksh
Here is another example of while loop to list all the factors of a number. Logic: Use a while loop to find all numbers till that number that can divide the number. So we shall start from 1 and navigate till the entered number and check which number can divide the number leaving a remainder as Zero. Remainder can be evaluated as below: rem=`expr $num % $i`; #rem stores the remainder of division of number by i
Output:
| |||||
|
Related blogs
You may also like to see:
[2014-04-22] | [Open System-Linux] |
xmllint : a command line xml tool to view and find errors in XML file |
[2014-06-05] | [Open System-Linux] |
SCRIPT command: Save your Linux sessions automatically |
[2014-03-25] | [Open System-Linux] |
Create a new user in Linux system: useradd |
[2017-08-22] | [Open System-Linux] |
Difference between tailf and tail -f in Linux |
[2015-03-23] | [Open System-Linux] |
FSCK : Checking and reparing filesystems in Linux |
Total comments: 2 | |
| |