Shell Scripting Important Syntax Example

1)Example 1
1)Example For If  else if Conditional Syntax Example
2)Less Than Greater Than (Comparison Operator) Example.
if [ $1 -lt 10 ] ; then
echo “Lesser Than Ten”;
elif [ $1 -gt 10 ] ; then
echo “Greater Than Ten”;
fi
OUTPUT
# ./if-test.sh 5
“Lesser Than Ten”

2)Example 2
Passing Argument Value Examples
argument.sh
cho '$@ Display Total Passed Arguments='$@;      
echo '$* Display Total Passed Arguments='$*;
echo '$0 Shell Script Name='$0;
echo '$1 Passed Argument ONE ='$1;
echo '$2 Passed Argument Two='$2;
echo '$? Previous Executed Command Status[if 0 Perviouse command EXECUTED Successfully]='$?;
echo '$$ Curret Running script Process ID='$$;
echo '$! Currently running Background Process ID='$!;
OUTPUT
sh-3.2# ./argument.sh 45 34 23
$# count No of Passed Arguments= 3
$@ Display Total Passed Arguments=45 34 23
$* Display Total Passed Arguments=45 34 23
$0 Shell Script Name=./argument.sh
$1 Passed Argument ONE =45
$2 Passed Argument Two=34
$? Previous Executed Command Status[if 0 Perviouse command EXECUTED Successfully]=0
$$ Curret Running script Process ID=5033
$! Currently running Background Process ID=

3)Example 3
1)Check File Proprieties whether is it Directory,File,Readable,Executable, Example
2)If conditional Example.
file-proprities.sh
if [ -f $1  ] ; then
        echo "It is File";
fi

if [ -d $1 ] ; then
        echo "It is Directory";
fi

if [ -r $1 ] ; then
        echo "It is readable ";
fi

if [ -w $1 ]; then
        echo "it is writable";
fi
if [ -x $1 ]; then
        echo "it is Executable";
fi


if [ -s $1 ]; then
        echo "it is empty file";
fi

if [ -L $1 ]; then
        echo "it is link";
fi
OUTPUT
# ./file-proprities.sh /root
It is Directory
It is readable
it is writable
  it is Executable
it is empty file 


4) Using For Loop To Simplify The Operation.
4.1)Linux Shell Script Delete particular Package.

#for i in `rpm -qa | grep intel`
do
    rpm -e $i;
done;

4.2)To do same function (or)Operation for the list of packages.
for i in vsftpd iptables ip6tables
do
        chkconfig $i off;
        service $i stop;
done;

Related post