Contact us

Print

llm1-lab5-exercise4

Exercise 4


In this exercise you will re-write the above script using the expr command. “Expr” is used to perform simple calculations. First you will learn expr’s syntax from the command line.

To use expr from the command line


1. While still in the myscripts directory. Use expr to add three numbers.

root@localhost myscripts# expr 2 + 800 + 1845

What is your output?


You will rewrite the calculator script from exercise 3 using “expr”

2. Make sure you are still in the “myscripts” directory.

3. Launch your text editor again and type in the following commands.


1 #!/bin/bash
2. #this script will perform a simple addition using expr
3. first_num=0
4. second_num=0
5. echo "Please type in the first number "
6. read first_num
7. echo "Please type in the second number "
8. read second_num
9.
10. echo "$first_num plus $second_num equals `expr $first_num + $second_num`"



The only thing we have done differently is to use “expr” to perform the calculation in the last line.

3. Save the code you typed in a file and call it “calc2.sh”

4. Change the permissions on the file you created to make it executable. Type


[root@localhost myscripts]# chmod u+x calc2.sh


5. Run the script by typing





Created by: wale. Last Modification: Monday 24 of November, 2008 18:18:37 EST by wale.

...