Multiple conditions in ShellScripting
This is my Shellscript
echo "IsInteractive"
read IsInteractive
if [ "$IsInteractive" == "true" ]; then
echo "Name"
read name
echo "Password"
read password
if [ "$name" == "abcd" & "$password" == "pwd" ]; then
echo "correct username and password"
else
echo "wrong username or password"
fi
elif [ "$IsInteractive" == "false" ]; then
echo "Everything working fine.But no log开发者_JS百科ic given yet"
else
echo "Give proper input"
fi
Is there something wrong in the 2nd if-condition?I tried putting && in the condition,but didn't work
You don't use multiple condition inside square brackets as &
, you use -a
. Or you use multiple brackets if [ $a = "a" ] && [ $b = "c" ]
You can also use the case/esac
construct , eg
case "$name$password" in
"abcdpwd" ) echo "correct" ;;
*) echo "not correct";;
esac
This is much cleaner than if/else
IMO.
Inside square brackets -a
is the equivalent to &&
Also, you should probably be using =
not ==
. Read the manpage for your shell.
if [ "$name" == "abcd" -a "$password" == "pwd" ]; then echo true; else echo false; fi
This and MANY more "basic" questions can be answered quickly and without the need for a forum-post with the command man bash
on most systems; and if it's not there just google "man bash" and pick the one which seems closest to your system... they're all "pretty much the same", especially at the basic level.
Cheers. Keith.
EDIT: FWW: the [ ] construct is a short-cut for "test", a function which is built-into all the the "standard" shells (sh, csh, ksh, and bash)... so the following code is EXACTLY equivalent:
$ a=a
$ b=c
$ if test "$a" = "a" && test "$b" = "c"; then echo true; else echo false; fi
true
The if then
construct just evaluates the return value from the test
function. You can display the return value of your last shell command with $?
... but beware, echo also sets $?
:
$ true
$ echo $?
0
$ false
$ echo $?
1
$ echo $?
0
The really interesting ramification of that is that the if then
construct can evaluate anything which returns success=0=true or failure=anything BUT 0 (typically 1=false)... be that a shell built-in function, a user-defined function, a unix utility or a program you wrote yourself. Hence the following code is roughly equivalent:
$ if echo "$a:$b" | fgrep -s "a:c"; then echo true; else echo false; fi
a:c
true
NOTE: looks like my system's fgrep doesn't accept the -s for silent switch. Sigh.
Note that in above example, where the output from echo
is being piped to the standard fgrep
utility, it is the return value of fgrep (the LAST command to be invoked) which is evaluated by if then
.
Good luck, and may root be with you. Cheers again. Keith.
A minor change will resolve this scripting error [: missing `]'. At line 8 replace '&' with '-a' -a, the logical AND. If both the operands are true then condition would be true otherwise it would be false
Working code below
echo "IsInteractive"
read IsInteractive
if [ "$IsInteractive" == "true" ]; then
echo "Name"
read name
echo "Password"
read password
if [ "$name" == "abcd" -a "$password" == "pwd" ]; then
echo "correct username and password"
else
echo "wrong username or password"
fi
elif [ "$IsInteractive" == "false" ]; then
echo "Everything working fine.But no logic given yet"
else
echo "Give proper input"
fi
精彩评论