[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: [LIH] 'if statement'
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. (binand@xxxxxxxxxxxxxxxxxxxxx)