Wednesday 6 September 2017

Unix Shell Scripting Tutorial - page 8

Unix Shell Scripting Tutorial - page 8

Question: What are two different way to executing a file?
Script file with name script8.sh
myname='Arun kumar';
echo $myname 

Way #1
sh script8.sh

After executing this script, if we run echo $myname it will not print anything because $myname is not in scope.

Way #2
source script8.sh

After executing this script, if we run echo $myname it will print because $myname is in scope.


Question: How to get input from user?
echo "Enter your name?"
read name
echo "Your name is '$name'"



Question: How to include configuration file in main file?
use source to include file.
source config.sh
myname='Arun kumar';
echo $myname 



Question: Give example of for?
for  table in {2..20..2}
do 
echo "$table,"
done

Output 2,4,6,8,10,12,14,16,18,20


Question: What is difference between while and until?
 Until loop always executes at least once. while executes till it returns a zero value and until loop executes till it returns non-zero value.

Question: Give example of function with parameters?
function fname(){
    echo $1; #first argument
    echo $2; #second argument
    echo $3; #third argument
}

fname a b c


Question: Give example of Switch case?
FRUIT="kiwi"

case "$FRUIT" in
   "apple") echo "Apple is good for health." 
   ;;
   
   "banana") echo "I like banana." 
   ;;
   
   "kiwi") echo "New Zealand is famous for kiwi fruit." 
   ;;
esac


Question: What does do eval? Give example eval
execute the statement.
name='name_variable'
name_variable='This is value'
echo $name; #name_variable
echo \${$name}; #${name_variable}
eval echo \${$name}; #This is value