I've written the following shell script:
#!/bin/bash
...
#delete downloaded file
if [ "$param" == "delete" ]; then
shopt -s extglob
cd $DataPersistent/download/
rm -f !($label.*)
cd $DataPersistent/swm/install_pkg/
rm -rf !($label|$label.json|active.json)
shopt -u extglob
fi
...
When I remove the if statement, it works fine, but when I add the if statement, it results in the following error:
/usr/local/bin/clear.sh: line 17: syntax error near unexpected token `('
/usr/local/bin/clear.sh: line 17: ` rm -f !($label.*)'
What's causing this? How can I fix it?
eval rm -rf '!("$label"|"$label".json|active.json)'will do the trick. Single quote the expression