Archive for the ‘Shell scripts’ Category

Useful bash tips

August 11, 2010

Addition of two int number  with awk –

echo $a $b | awk ‘{sum= $1 + $2 ; print sum }’

Multiplication and division of two floating number  with awk –

echo $a $b | awk ‘{ printf( “%.3f\n”, ( $1 * 100 ) / $2 );}’

res=`echo $a $b | awk ‘{ sum = $1 * $2 ; per = sum / 100; print per}’`

Mathematical operation with bc

res=`echo “$sum * $avg / 100” |bc`

free_t=`echo “$free / 1024  / 1024” |bc`

condition with bc –

if [ $(echo “$a < $b”| bc) -eq 1 ]

It will return either 0 or 1 depending on true or false.

Mathematical operation with bash

avg=`echo $((( $sum * 60 ) / 100 ))`

Comparing floating number

August 5, 2010

Suppose you wanna put a check whether your system load goes high or not??

load=`uptime |sed ‘s/.*average://’|sed ‘s/,.*//’`

val=0.01

res=`expr $load \> $val`

This expr command will return either true or false i.e. ( 1 or 0 )

expressions in shell scripting

July 4, 2010

Testing exit status

The ? variable holds the exit status of the previously executed command (the most recently completed foreground process).

The following example shows a simple test:

myhome ~> if [ $? -eq 0 ]
More input> then echo ‘That was a good job!’
More input> fi
That was a good job!

myhome ~>

The following example demonstrates that TEST-COMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:

myhome ~> if ! grep $USER /etc/passwd
More input> then echo “your user account is not managed locally”; fi
your user account is not managed locally

myhome > echo $?
0

myhome >

The same result can be obtained as follows:

myhome > grep $USER /etc/passwd

myhome > if [ $? -ne 0 ] ; then echo “not a local account” ; fi
not a local account

myhome >

Numeric comparisons

The examples below use numerical comparisons:

myhome > num=`wc -l work.txt`

myhome > echo $num
201

myhome > if [ “$num” -gt “150” ]
More input> then echo ; echo “you’ve worked hard enough for today.”
More input> echo ; fi

you’ve worked hard enough for today.

myhome >

This script is executed by cron every Sunday. If the week number is even, it reminds you to put out the garbage cans:

#!/bin/bash

# Calculate the week number using the date command:

WEEKOFFSET=$[ $(date +”%V”) % 2 ]

# Test if we have a remainder. If not, this is an even week so send a message.
# Else, do nothing.

if [ $WEEKOFFSET -eq “0” ]; then
echo “Sunday evening, put out the garbage cans.” | mail -s “Garbage cans out” your@your_domain.org
fi

String comparisons

An example of comparing strings for testing the user ID:

if [ “$(whoami)” != ‘root’ ]; then
echo “You have no permission to run $0 as non-root user.”
exit 1;
fi

With Bash, you can shorten this type of construct. The compact equivalent of the above test is as follows:

[ “$(whoami)” != ‘root’ ] && ( echo you are using a non-privileged account; exit 1 )

Similar to the “&&” expression which indicates what to do if the test proves true, “||” specifies what to do if the test is false.

Regular expressions may also be used in comparisons:

myhome > gender=”female”

myhome > if [[ “$gender” == f* ]]
More input> then echo “Pleasure to meet you, Madame.”; fi
Pleasure to meet you, Madame.

myhome >

Expression used with if condtition

July 4, 2010

Bash expression is the combination of operators, features, or values used to form a bash conditional statement. Conditional expression could be binary or unary expression which involves numeric, string or any commands whose return status is zero when success. Following are few conditional expressions that are helpful.

[-a FILE ] True if FILE exists.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option “OPTIONNAME” is enabled.
[ -z STRING ] True if the length of “STRING” is zero.
[ -n STRING ] or [ STRING ] True if the length of “STRING” is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. “=” may be used instead of “==” for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 STRING2 ] True if “STRING1” sorts after “STRING2” lexicographically in the current locale.
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.

[ expr1 -a expr2 ] Returns true if both the expression is true.
[ expr1 -o expr2 ] Returns true if either of the expression1 or 2 is true.

if [ $first -eq 0 ] && [ $second -eq 0 ]
then
echo “Num1 and Num2 are zero”

shell scripting syntax

March 30, 2010

– Printing/Redirecting PID of current program

echo $$ >/tmp/out.pid

– Comparing floating value –

a=1.34
b=1.21

res=`expr $a > $b` [ if true then res =1 else res =0 ]

– Running a script with a particular user

if [ “$LOGNAME” != “tarique” ]; then
echo
echo “Only user bea can run this script”
echo
exit 1
fi

– Logical AND with if condition

if [[ ! -f /tmp/out.pid && $res -eq 1 ]] ; then

– Nested for loop

for (( j = 0 ; j <=1 ; j++ ))
do
for (( i = 0 ; i <=9 ; i++ ))
do
….
done
done

– Date formatting

D=`date '+%y-%d-%m %H:%M:%S'`

– Adding value of two variables

result=$(($Value1 + $Value2))

– Remote command execution –

ssh tarique@remotesrv 'touch /tmp/test;rm -f /tmp/test'