Linux shell scripting basics

1. Strings  ( == equal; != Not equal, -z Null/empty/zero length; -n NOT empty/null)
2. Number (-eq Equal; -ne Not equal; -gt Greater than; -ge Greater than or Equal to;
                       -lt Less than; -le Less than or Equal to)
3. Files (-e file exist; -f is a file; -d is a directory; -s file exists and non-zero length;
-g have guid, -u have suid, -r readable; -w writable; -x executable)

a)
read -p “Enter a string [e.g abc.123]: ” str
if  [  !  -z  $str  ]  &&  [  -n  $str  ]; then
echo “String is NOT empty and its length is: ${#str}”
echo “First three chars are: ${str:0:3}”
echo “All chars after . are ${str#*.}”
else
echo “String is empty”
fi

P.S. There must be a space between if  [  condition  ].

b)
read -p “Enter 1st string: ” str1
read -p “Enter 2nd string: ” str2
if  [  $str1 == $str2  ]; then
echo “Both strings are same”
elif  [  $str1 != $str2  ]; then
echo “Both strings are different”
fi

c)
read -p “Enter a file or a directory name: ” file
if  [ -f $file  ]; then
echo “$file exists and is a file”
elif  [ -d $file  ]; then
echo “$file exists and a directory”
fi

2. if, elif and else
# There must be a single space after ‘if’ | ‘elif’ and ‘[‘, and inside the expression

if  [  $# -eq 1  ]; then   #Can use double ‘[[  ]]’ parenthesis as well
echo “Nos of arguments are one: $1
elif  [  $# -eq 2  ]; then
echo “Nos of arguments are two: $1, $2
else
echo “Nos of arguments are $#: ”          (Total number of arguments)
echo “All argument values are: $*”       (Show all arguments)
echo “Script name: $0”                            (Show script name)
echo “Script exit code is: ” $?                 (Show command or script exit status code)
fi

3. for
a)
for i in 1 2 3; do
echo $i
done

b)
for i in {1..3}; do
echo $i
done

c)
read -p “Enter the number to calculate factorial: ” num
fact=1
for  ((i=2; i<=$num; i++)); do
fact=$((fact * i))
done
echo “$num factorial is: ” $fact

d)
for file in $HOME/*; do
echo $file
done

4. while
read -p “Enter a number: ” num
fact=1
while [ $num -gt 1 ]; do
fact=$((fact * num)); num=$((num – 1))
done
echo “Factorial: $fact”

5. untill
read -p “Enter a number: ” num
fact=1
#CONTINUE until $num is less or equal to 1 or STOP when expression is true
until [ $num -le 1 ]; do
fact=$((fact * num)); num=$((num – 1))
done
echo “Factorial: $fact”

5. case and functions
#Functions must be defined at the top or before they are called
func1 () {
echo “$1! from func1()”
}
func2 (){
echo “$1! from func2()”
}
read -p “Enter function number [1-2] ” n
case $n in
1) func$n Hi;;               #’Hi’ is a parameter/argument to function
2) func$n Hello;;          #’Hello’ is a parameter/argument to function
*) echo “Invalid function number”;;
esac

References:

a. http://www.ibm.com/developerworks/library/l-bash2.html

b. http://www.linuxconfig.org/Bash_scripting_Tutorial

c. http://linuxhelp.blogspot.com/2005/10/10-seconds-guide-to-bash-shell.html

Advertisements

About Ishtiaque

I am IBM Certified Infrastructure Systems Architect, Linux Foundation Certified System Administrator, Oracle Certified Programmer in Java and Web Component Developer, and TOGAF 9 certified with over 10 years of support and development experience in IBM middleware software and Java. Additionally, have a sound grip in databases and OpenStack administration. I hold the following certifications: IBM Certified Infrastructure Systems Architect Linux Foundation Certified System Administrator (LFCS) TOGAF 9 Certified Oracle Certified Expert, Java EE6 Web Component Developer Oracle Certified Professional – Java 6 Programmer ITIL v3 Foundation Certified IBM Certified Solution Architect – Cloud Computing Infrastructure V1 IBM Certified System Administrator – WebSphere Portal V8, V7, V6.1, V6 IBM Certified System Administrator – WebSphere Application Server V7, V6.1 IBM Certified System Administrator – AIX V7 IBM Certified System Administrator – WebSphere MQ V7 IBM Certified Deployment Professional – Business Process Manager Advanced V7.5 IBM Certified Solution Advisor – Cloud Computing Architecture V3 IBM Certified Solution Developer – WebSphere Portal V5.1
This entry was posted in LFCE, LFCS, Linux, Scripts. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s