bob21
April 28, 2012, 11:23pm
1
Hi there
Everytime it calls the second if statement it keeps calling the first echo message from the first if statement.
how do I get it to display the second echo message in the second if statement?
Thanks.
function (verbose) {
if [ "args_V" = v" ] ; then
echo "rm: cannot remove $1 : is a directory"
elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then
echo "removed \`$1'"
if [[ "$args_V" = "v" && "$args_I" = "i" && "$args_R" = "r" ] then
interactive $1
fi
fi
}
There are syntactical problems with your snippet. The function name cannot have parentheses, on the second line there is a double quote missing. I suggest you have another look at the syntax...
bob21
April 29, 2012, 8:59am
3
sorry i worte the code wrong here on the forum
still have the same problem as suggested above
function verbose () {
if [ "args_V" = v" ] ; then
echo "rm: cannot remove $1 : is a directory"
elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then
echo "removed \`$1'"
if [[ "$args_V" = "v" && "$args_I" = "i" && "$args_R" = "r" ] then
interactive $1
fi
fi
}
And still there is a double quote missing on line 2 (and a $-sign?)
bob21
April 29, 2012, 12:10pm
5
Apologies for the mistakes on the forum,
still having the same problem? :(
function verbose () {
if [ "$args_V" = "v" ] ; then
echo "rm: cannot remove $1 : is a directory"
elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then
echo "removed \`$1'"
if [[ "$args_V" = "v" && "$args_I" = "i" && "$args_R" = "r" ] then
interactive $1
fi
fi
}
Since elif is being used, the second condition does not get evaluated when condition 1 is true, and it always evaluates to false when condition 1 is false, so condition 2 never becomes true.....
bob21
April 29, 2012, 1:23pm
7
Oh i see however the second condition (elif) does evaluate so in this case it does remove the file but it doesn't execute the second echo statement. What can i do to fix this?
I don't know what you need to achieve, perhaps something like this (just a guess)?
verbose()
{
if [ "$args_V" = "v" ]; then
if [ "$args_I" = "i" ] && [ "$args_R" = "r" ]; then
interactive "$1"
elif [ -f "$1" ]; then
echo "removed \`$1'"
else
echo "rm: cannot remove $1 : is a directory"
fi
fi
}
This thread is not Linux specific, moving to "Shell Programming and Scripting"