Ashok forced the electrons to say:
> Could anyone tell me how to write an 'if' statement in bash shell
> programming

Four ways.

1. If you want to check a command is successful or not:

if ! grep -q '^root' /etc/passwd; then
        echo "Rootless system..."
fi

2. If you want to compare strings:

if [ "$TERM" = "dumb" ]; then
        echo "Set TERM to something nice..."
fi

3. Numerical checks:

if let "$a < 5"; then
        echo "a should be more than 5"
fi

This can also be written as:

if [ $a -lt 5 ]; then
        echo "a should be more than 5"
fi

4. File checks:

if [ ! -x /usr/bin/echo ]; then
        echo "Cannot execute /usr/bin/echo"
fi

This can also be written as:

if test ! -x /usr/bin/echo; then
        echo "Cannot execute /usr/bin/echo"
fi

Other associated keywords are 'else' and 'elif'.

I am posting this also to LIP. Followups there.

Binand

-- 
The prompt for all occasions:
export PS1="F:\$(pwd | tr '/[a-z]' '\134\134[A-Z]')> "
--------------- Binand Raj S. ([EMAIL PROTECTED])


-----------------------------------------------------------------------
LIH is all for free speech.  But it was created for a purpose - to help
people discuss issues about installing and running Linux.  If your
messages are counterproductive to this purpose, your privileges to
submit messages can and will be revoked.

Reply via email to