How to Read Run Time Variables in UNIX?
We may need to read run time variables, during the execution of a unix script.
It is done with the help of read command.
A few examples as below:
server501:# echo “Enter Name: ”
Enter Name:
server501:# read name
Angelina
server501:# echo $name
Angelina
Some other examples
1.
server501:# #!/bin/sh
server501:# msg1=Hello
server501:# msg2=There!
server501:# echo $msg1 $msg2
Hello There!
2.
server501:# #!/bin/sh
server501:# echo ‘msg=”Hello World!”‘ > hello
server501:# echo ‘echo $msg’ >> hello
server501:# chmod 700 hello
server501:# ./hello
Hello World!
3.
server501:# #!/bin/sh
server501:# fruitlist=”Apple Pear Tomato Peach Grape”
server501:# for fruit in $fruitlist
> do
> if [ “$fruit” = “Tomato” ] || [ “$fruit” = “Peach” ]
> then
> echo “I like ${fruit}es”
> else
> echo “I like ${fruit}s”
> fi
> done
I like Apples
I like Pears
I like Tomatoes
I like Peaches
I like Grapes