. test [ ] 

test 10 -gt 55 ; echo $?  

displays 1 if the command if false and 0 if the condition is true. 

[ -d foo ] Does the directory foo exist ? 

[ -e bar ] Does the file bar exist ? 

[ '/etc' = $PWD ] Is the string /etc equal to the variable $PWD ? 

[ $1 != 'secret' ] Is the first parameter different from secret ? 

[ 55 -lt $bar ] Is 55 less than the value of $bar ? 

[ $foo -ge 1000 ] Is the value of $foo greater or equal to 1000 ? 

[ "abc" < $bar ] Does abc sort before the value of $bar ? 

[ -f foo ] Is foo a regular file ? 

[ -r bar ] Is bar a readable file ? 

[ foo -nt bar ] Is file foo newer than file bar ? 

[ -o nounset ] Is the shell option nounset set ? 

Tests can be combined with logical AND and OR 

[ 66 -gt 55 -a 66 -lt 500 ] && echo true || echo false 

true 

#!/bin/bash 

if [ -f isit.txt ] then 

echo isit.txt exists! 

else echo isit.txt not found! 

fi 

if [ conditions ]

    then

         commands

fi


if [[ condition ]]

then

statement

elif [[ condition ]]; then

statement 

else

do this by default

fi

Shell Scripting for Beginners – How to Write Bash Scripts in Linux (freecodecamp.org) 

Practice -

Harsh

#!/bin/bash

read -p"Enter the no.  :" n

if [ $n -ge 10 ]

then

echo "The no. is two digits"

else

echo "The no. is single digit"

fi

Sudharshan

read -p "enter a number " num 

 

if [ ${#num} -eq 1 ]

then

  echo "Single digit number"

else

  echo "Double digit number"

fi


if [ `expr $a % 2` == 0 ]

then

echo "$a is even"

else

echo "$a is Odd"

fi


read -p  "enter the directory name" name

 

if [ -d $name ]

then

echo "Directory already exists"

 

else

 

mkdir $name

echo "Diectory is created"

 

fi


#!/bin/bash

read -p "Enter the no.  :" n

if [ $n -ge 18 ]

then

echo "The user is eligible for vote"

else

echo "The user  is  not eligible for vote"

fi


#!/bin/bash

 

read -p "Enter user name  " name

read -p "Enter Password   " pass

 

if [ $name == 'admin' ] && [ $pass == 'passwd' ]

then

echo "User is valid"

 

else

echo "User is invalid"

 

fi


echo "Enter Marks"

read obm

 

if [ $obm -gt $tm ]

then

echo "Invalid Marks"

 

elif [ $obm -ge 80 ] && [ $obm -le 100 ]

then

echo "You Achived Grade A"

 

elif [ $obm -ge 60 ] && [ $obm -lt 80 ]

then

echo "You Achived Grade B"

 

elif [ $obm -gt 1 ] && [ $obm -lt 60 ]

then

echo "You Achived Grade C"

 

fi


#!/bin/bash


for i in {1..5}

do

    echo $i

done


#!/bin/bash

i=1

while [[ $i -le 10 ]] ; do

   echo "$i"

  (( i += 1 ))

done


for i in {1..10}

do

echo $i

done

 #!/bin/bash

count=0

for i in {10..100..10}

do

  echo "$i"

  count=$((count + 1))

done

echo "The count is: $count"

read -p "enter a number: " num

for ((i=1;i<10;i++));

do

result=$((num *i))

echo "$num * $i = $result"

done

fact=1

read num

for((i=2;i<=num;i++))

{

  fact=$((fact * i))  #fact = fact * i

}

echo $fact


for i in {10..1}

do

    echo $i

done

Sudarshan

read -p "Enter the number of files to create: " n

file=1

while [ $file == $n ]

do

  touch file$file.txt

  file=$((file+1))

done

 

echo "Created $n files."