Monday, April 8, 2013

LINUX Shell Script manual



Relational Operators
-eq - Equal to
-lt - Less than
-gt - Greater than
-ge - Greater than or Equal to
-le - Less than or Equal to


File related tests
-f file - True if file exists and is a regular file.
-r file - True if file exists and is readable.
-w file - True if file exists and is writable.
-x file - True if file exists and is executable.
-d file - True if file exists and is a directory.
-s file - True if file exists and has a size greater than zero.

String tests
-n str - True if string str is not a null string.
-z str - True if string str is a null string.
-s file - True if file exists and has a size greater than zero.
Test also permits the checking of more than one expression in the same line.
-a  - Performs the AND function
-o  - Performs the OR function

$ test -e .
$ echo $?
0 $ test -e xyz
$ echo $?
1
Operator "#" means "delete from the left, to the first case of what follows."

$ x="This is my test string."
$ echo ${x#* }

is my test string.

                   
                   
Operator "##" means "delete from the left, to the last case of what follows."

$ x="This is my test string."
$ echo ${x##* }

string.

                     
                   
Operator "%" means "delete from the right, to the first case of what follows."

$ x="This is my test string."
$ echo ${x% *}

This is my test

                       
                   
Operator "%%" means "delete from the right, to the last case of what follows."

$ x="This is my test string."
$ echo ${x%% *}

This 



IF

#!/bin/bash
if test -e .
then
        echo "Yes."
else
        echo "No."
fi


#!/bin/bash
if [ -e . ]
then
        echo "Yes."
else
        echo "No."
fi



FOR LOOP

#!/bin/bash
for fn in tom dick harry; do
        echo "$fn"
done


#!/bin/bash
for n in {0..5}
do
        echo $n
done


WHILE


#!/bin/bash
n=1
while [ $n -le 6 ]; do
        echo $n
        let n++
done



#!/bin/bash
y=1
while [ $y -le 12 ]; do
        x=1
        while [ $x -le 12 ]; do
               printf "% 4d" $(( $x * $y ))
               let x++
        done
        echo ""
        let y++
done


CASE


#!/bin/bash
echo "What is your preferred programming / scripting language"
echo "1) bash"
echo "2) perl"
echo "3) phyton"
echo "4) c++"
echo "5) I do not know !"
read case;
#simple case bash structure
# note in this case $case is variable and does not have to
# be named case this is just an example
case $case in
    1) echo "You selected bash";;
    2) echo "You selected perl";;
    3) echo "You selected phyton";;
    4) echo "You selected c++";;
    5) exit
esac





















No comments:

Post a Comment