I need find solution for using space in pattern in case
. I have this function with case
setParam() {
case "$1" in
0624)
# do something - download file from url
;;
del-0624)
# do something - delete file from local machine
exit 0
;;
# Help
*|''|h|help)
printHelp
exit 0
;;
esac
}
for PARAM in $*; do
setParam "$PARAM"
done
Paramter "0624"
is for run function for download files from url.
Paramter "del-0624"
is for deleting files in local machine.
Question: It is possible use paramter "del 0624"
(with space)? I have problem with space in parameter in case
.
To cd to a directory with spaces in the name, in Bash, you need to add a backslash ( \ ) before the space. In other words, you need to escape the space.
The lack of spaces is actually how the shell distinguishes an assignment from a regular command. Also, spaces are required around the operators in a [ command: [ "$timer"=0 ] is a valid test command, but it doesn't do what you expect because it doesn't recognize = as an operator.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching. Follow this answer to receive notifications.
You need to use double quotes
in case
. Also script should be run as ./script "del 0624"
.
setParam() {
case "$1" in
"0624")
# do something - download file from url
;;
"del-0624")
# do something - delete file from local machine
exit 0
;;
"del 0624")
# do something - delete file from local machine
exit 0
;;
# Help
*|''|h|help)
printHelp
exit 0
;;
esac
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With