Saturday 14 November 2015

Basic shell scripting.......

==============================================================================
Variables:
==============================================================================
variable="value"
variable=`unix command`

index=1

==============================================================================
Incrementing a numeric variable:
==============================================================================
index++

==============================================================================
Arrays:
==============================================================================
variable[1]="value"
variable[2]=`unix command`

==============================================================================
Print to console:
==============================================================================
echo "Prining my first line.."
echo $variableName

echo ${variable[${index}]}


==============================================================================
Generating a unique Nubmer/Name:
==============================================================================
date +%Y%m%d%H%M



==============================================================================
IF Block
==============================================================================
if [ condition ] ; then
    ## Case 1
else
    ## Case 2
fi


==============================================================================
Check for successful execution of a command:
==============================================================================

NOW=`date `
if [ $? -ne 0 ] ; then
echo "Errors"
else
echo "Success"
fi
echo $NOW

==============================================================================
FOR Block:
==============================================================================
for <variable> in <array/variable> ; do
    ## commands
done

Example:

file_list=`ls -N *.log`
for file_name in $file_list ; do
    rm -f $file_name
done

==============================================================================
While Block:
==============================================================================
while [ condition ]
do
   
done

==============================================================================
Command to find and replace a string
==============================================================================
DT=`date | awk '{print $6}'`
NEWDT=`expr $DT + 1`
NEWDT2=`expr $DT + 2`

sed -i "s/$DT/$NEWDT/g" */*.xml
sed -i "s/validUntil=\"$DT/validUntil=\"$NEWDT/g" */*.xml

==============================================================================
Shell script method & arguments
==============================================================================
printData(){
    param=1
    echo $param
}
printData("Unix session is gona end shortly...");


==============================================================================
Passing on Arguments to a shell script:
==============================================================================
echo $1 ## put this any where in the script
echo $2 ## keep increasing the number to get more arguments

No comments:

Post a Comment