Monday, January 31, 2011

BASH : Control Structures

touch file.sh
CONTENT OF FILE
#!/bin/bash
if [ 1 -eq 1]
then
echo Equal
else
echo Not Equal
fi
#END
CONTENT OF FILE
#!/bin/bash
if [ "text1" = "text1"]
then
echo Equal
else
echo Not Equal
fi
#END
CONTENT OF FILE
#!/bin/bash
for countries in USA Germany France
do
if [ "$countries" = "USA"]
then
echo Welcome to the $countries
elif [ "$countries" = "Germany"]
then
echo One country
else
echo $countries
fi
done
#END


. file.sh | sort
CONTENT OF FILE
#!/bin/bash
$FILE=helloworld.sh
if [ -e $FILE] #check if a file exists on computer
then
echo the file exists
else
echo the file does not exists
#END


#!/bin/bash
$FILE="test"
if [ -d $FILE] #check if a directory exists on computer
then
echo the directory exists
else
echo the directory does not exists
#END


touch file{1,2} -> create empty files
CONTENT OF FILE
#!/bin/bash
$FILE1="test1"
$FILE2="test2"
if [ $FILE1 -nt $FILE2] # FILE1 is newer than FILE2
then
echo FILE1 is newer than FILE2
else
echo FILE2 is newer than FILE1
#END
CONTENT OF FILE
#!/bin/bash
netstat -ant | greo :80
APACHESTATUS="$?"
if [ "$APACHESTATUS" -eq 0]
then
echo Apache is Up and Running
fi
#END


service httpd stop -> stops httpd service
netstat -ant | grep :80
service httpd start -> start httpd service
CONTENT OF FILE
#!/bin/bash
netstat -ant | greo :80 > /dev/null #this is a black holl
APACHESTATUS="$?"
if [ "$APACHESTATUS" -eq 0]
then
echo Apache is Up and Running
netstat -ant | grep 3306 #test mysql > /dev/null
MYSQLSTATUS="$?"
if [ "$MYSQLSTATUS" != 0]
then
echo MySQL is NOT running
else
echo MYSql is running
fi
else
echo Apache is not running
fi
#END


service mysqld stop


CASE sentence

#!/bin/bash
for countries in USA Germany France Armenia
do
case $countries in USA )
echo "Welcome to America" ;;
Germany )
echo "You are in GErmany" ;;
France )
echo "You are in France" ;;
* ) #default case
echo "Welocome to another country" ;;
esac #close case statemant
done
#END
. forloopcase.sh
wc -l . forloopcase.sh - showas the number of lines

No comments:

Post a Comment