. 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 -
Enter the number from the user and check if the greater number
Enter number from the user and check if the number is a single digit or double digit
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
Check the number is even or odd
if [ `expr $a % 2` == 0 ]
then
echo "$a is even"
else
echo "$a is Odd"
fi
Enter directory name form the user and check if it exist or not if it dosn't exits create an directory
read -p "enter the directory name" name
if [ -d $name ]
then
echo "Directory already exists"
else
mkdir $name
echo "Diectory is created"
fi
Enter age of the user and check if he is eligible to vote or not
#!/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
Enter username and password from the user and check if user name is "admin" password "passwd" it is valid user else incorrect
#!/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
Give grade to the students according to their score - A - greater than 80, B between 80 and 60 C less than 60
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
Print Numbers from 1 to 10
for i in {1..10}
do
echo $i
done
Print number count of 10's 10...20...30 till 100
#!/bin/bash
count=0
for i in {10..100..10}
do
echo "$i"
count=$((count + 1))
done
echo "The count is: $count"
print multiplication table for user input number
read -p "enter a number: " num
for ((i=1;i<10;i++));
do
result=$((num *i))
echo "$num * $i = $result"
done
Print a factorial of a given number n!
fact=1
read num
for((i=2;i<=num;i++))
{
fact=$((fact * i)) #fact = fact * i
}
echo $fact
Print 10 numbers in reverse order
for i in {10..1}
do
echo $i
done
Ask user how many files to make and make that many files using touch command
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."