Wednesday, March 22, 2017

Shell Scripting Tutorial for Beginners 13 - The case statement Example



#! /bin/bash

echo -e "Enter some character : \c"
read value


case $value in
    [a-z] )
        echo "User entered $value a to z" ;;
    [A-Z] )
        echo "User entered $value A to Z" ;;
    [0-9] )
        echo "User entered $value 0 to 9" ;;
    ? )
        echo "User entered $value special character" ;;
    * )
        echo "Unknown input" ;;
esac
Output:


test@test$ ./hello.sh 
Enter some character : f
User entered f a to z
test@test$ ./hello.sh 
Enter some character : K
User entered K a to z
test@test$ LANG=C
test@test$ ./hello.sh 
Enter some character : K
User entered K A to Z
test@test$ ./hello.sh 
Enter some character : 9
User entered 9 0 to 9
test@test$ ./hello.sh 
Enter some character : 5
User entered 5 0 to 9
test@test$ ./hello.sh 
Enter some character : &
User entered & special character
test@test$ ./hello.sh 
Enter some character : sdsdsdsd
Unknown input
test@test$ 

Tuesday, March 21, 2017

Shell Scripting Tutorial for Beginners 12 - The case statement

#! /bin/bash

vehicle=$1

case $vehicle in
    "car" )
        echo "Rent of $vehicle is 100 dollar" ;;
    "van" )
        echo "Rent of $vehicle is 80 dollar" ;;
    "bicycle" )
        echo "Rent of $vehicle is 5 dollar" ;;
    "truck" )
        echo "Rent of $vehicle is 150 dollar" ;;
    * )
        echo "Unknown vehicle" ;;
esac

Saturday, March 18, 2017

Shell Scripting Tutorial for Beginners 11 - Floating point math operatio...



#! /bin/bash

num1=20.5
num2=5

echo "$num1+$num2" | bc
echo "$num1-$num2" | bc
echo "20.5*5" | bc
echo "scale=20;20.5/5" | bc
echo "20.5%5" | bc

num=4

echo "scale=2;sqrt($num)" | bc -l
echo "scale=2;3^3" | bc -l

Thursday, March 16, 2017

Shell Scripting Tutorial for Beginners 10 - Perform arithmetic operations

#! /bin/bash

num1=20
num2=5

echo $(( num1 + num2 ))
echo $(( num1 - num2 ))
echo $(( num1 * num2 ))
echo $(( num1 / num2 ))
echo $(( num1 % num2 ))


echo $(expr $num1 + $num2 )
echo $(expr  $num1 - $num2 )
echo $(expr  $num1 \* $num2 )
echo $(expr  $num1 / $num2 )
echo $(expr  $num1 % $num2 )

Wednesday, March 15, 2017

Shell Scripting Tutorial for Beginners 8 - Logical 'OR' Operator



#! /bin/bash

age=60

# for using OR operator use ||
if [ "$age" -gt 18] || ["$age" -lt 30 ]
then
  echo "valid age"
  else
  echo "age not valid"
fi

#  The -o option provide
# an alternative compound condition test.
if [ "$age" -gt 18 -o "$age" -lt 30 ]
then
  echo "valid age"
  else
  echo "age not valid"
fi

# if [[ $condition1 || $condition2 ]]    # Also works.
if [[ "$age" -gt 18 || "$age" -lt 30 ]]
then
  echo "valid age"
  else
  echo "age not valid"
fi

Monday, March 13, 2017

Shell Scripting Tutorial for Beginners 8 - Logical 'AND' Operator

#! /bin/bash

age=60

# for using And operator use &&
if [ "$age" -gt 18] && ["$age" -lt 30 ]
then
  echo "valid age"
  else
  echo "age not valid"
fi

#  The -a option provide
# an alternative compound condition test.
if [ "$age" -gt 18 -a "$age" -lt 30 ]
then
  echo "valid age"
  else
  echo "age not valid"
fi

# if [[ $condition1 && $condition2 ]]    # Also works.
if [[ "$age" -gt 18 && "$age" -lt 30 ]]
then
  echo "valid age"
  else
  echo "age not valid"
fi

Saturday, March 11, 2017

Shell Scripting Tutorial for Beginners 7 - How to append output to the e...



#! /bin/bash

echo -e "Enter the name of the file : \c"
read file_name

if [ -f $file_name ]
then
      if [ -w $file_name ]
      then
         echo "Type some text data. To quit press ctrl+d."
         cat >>  $file_name
      else
          echo "The file do not have write permissions"
      fi
 else   
  echo "$file_name not exists"
fi

Shell Scripting Tutorial for Beginners 6 - File test operators



#! /bin/bash

echo -e "Enter the name of the file : \c"
read file_name

if [ -s $file_name ]
then
  echo "$file_name not empty"
 else   
  echo "$file_name empty"
fi