I am a bit rusty on my bash shell scripting. I have created the following script:
#!/bin/bash
display_usage() {
echo -e "\nUsage: This script must be run with both a valid source and a target client name."
echo -e "Example: ./createClientRolesRespChart <source client name> <target client name>\n"
}
mv $1 $2
# If less than two arguments supplied, display usage
if [ $# -le 1 ]; then
display_usage
exit 1
fi
if [ $? -eq 0 ]; then
echo "Created new response chart for the $2 client."
exit 0
else
echo "[Error] Move (mv) command failed. Please check parameters are correct."
exit 1
fi
The script works, BUT if I enter a wrong filename, the command fails, buy it still prints the success message. Any hints. Should be an easy thing. I just forget :(
bash-3.2$ ./createClientRolesRespChart foo bar
mv: rename foo to bar: No such file or directory
Created new response chart for the bar client.
$?
only says whether the last command succeeded. In your case, you're doing an mv
, which is failing, then looking at the argument count, then looking at $?
, which will now contain the result of the argument count check, and not of the mv
. To fix it, switch the argument count check and the mv
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.