i Shell script tutorial – part1 – All things in moderation

Shell script tutorial – part1

In the this part I will write some base of shell script like about variables, functions , if statement, etc.

echo

print something in quote

echo "You're the unique persion"

Variables, Constant, Operators

Define a Constant

declare -r NUM=5

Define and call a variable

num=2  
echo "$num"
result is : 2

Operators

Examples:

#exponentiation
echo "$((5**2))  
#modulus
echo "$((5%2))"  

results are: 
25  
1  

Icrementing values

rand=5  
let rand+=4  
result is:  
9 

echo "rand++ = $(( rand++ ))"  
echo "$rand"  
echo "++rand = $(( ++rand ))"   
echo "rand-- = $(( rand-- ))"  
echo "--rand = $(( --rand ))"  

results are:  
rand++ = 5
6
++rand = 7
rand-- = 7
--rand = 5     

Running python inside

num1=2.1  
num2=1.2  
num3=$(python -c "print $num1 + $num2")  
echo $num3  

result is: 
3.3  

Printing a text

cat << END  
I'm a geek guy  
I'm not afraid any chanlange  
END  

Functions

Functions with local Variables

name="WinStark" 
demoLocal(){ 
    local name="cloudi" # Only avaiable inside this fucntion  
    return
}

demoLocal  

echo "$name"  

result is:  
WinStark  
Pass attributes into functions
getSum(){
        local num3=$1
        local num4=$2

        local sum=$(( num3+num4))
        echo $sum
}

num1=2
num2=5

sum=$(getSum num1 num2)
echo "The sum is $sum"  

result is: 
The sum is 7  

Conditionals

Read and receive form user’s input

read -p "Enter your name: " name  
echo "Hello $name"  

IF statement

# p is Promnt
read -p "How old are u ?" age
if [ $age -ge 18 ] # the condition
then
        echo "You can drive"  
elif [ $age -eq 17 ]
then
        echo "You can drive the next year"  
else
        echo "You can't drive"  
fi # close if statement

Example result is:  
How old are u ? 18  
You can drive  

Note
eq : equal
ge : greater euqal
lt : less than

if ((num == 12)); then  
    echo "Your number equals 12"  
fi  

if ((num > 12)); then  
    echo "It greater then 12"  
else  
    echo "It is less then 12  
fi  

if (( ((num % 2)) ==0 )); then  
    echo "It is even"  
fi  

To be continuted…

Leave a Reply