2

so I have the following which runs if I run it on the command line, but gives this error within a bash script:

sudo -u $usr rm -rf "${htmldir}"/!("awstatsicons"|"awstats-icon"|"icon"|"roundcube"|"phpmyadmin"|"stats"|"htusers")

Note, in the bash script, the line above I've specified: "shopt -s extglob". If I add in the usr and htmldir variables on the command line, set the "shopt -s extglob" and copy that line exactly, it then behaves as it should. However running it from a script, it gives the following error:

syntax error near unexpected token `('

I've tried some permutations, but not coming right. Any help would be appreciated

10
  • How exactly are you running the script? does it have a #!/bin/bash shebang? Jul 15, 2020 at 11:57
  • Hi @steeldriver yes, it's a standard bash script with '''#!/bin/bash''' starting it off
    – Neural_oD
    Jul 15, 2020 at 12:22
  • how about find with -maxdepth -type -not -name -delete flags
    – alecxs
    Jul 16, 2020 at 0:19
  • hi @alecxs, I really wanted to use the rm command. As mentioned this works on the command line itself, I'm trying to find out why it's not working within a script
    – Neural_oD
    Jul 16, 2020 at 8:22
  • post the output of hexdump -C script
    – alecxs
    Jul 16, 2020 at 8:27

2 Answers 2

1

You need to make the shell interactive and use history -p to interpret ! characters:

#!/bin/bash -i
eval $(history -p <your statement involving ! expansion>)

And before you call the shell script, you need to save the history:

history -a

Example:

cat jt
#!/bin/bash -i
eval `history -p !df`

df /; history -a
./jt
1
  • Please note that the OPs intention is not to use the ! for history expansion, but as a negation for the following OR-type filename alternative ( ... | ... | ... ), so the OP wants to rm all files that are not called "awstatsicons" or "awstats-icon" or "icon" or "roundcube" or "phpmyadmin" or "stats" or "htusers".
    – AdminBee
    Jul 17, 2020 at 7:58
0

Ok, so basically I had posted a snippet of my problem. However, @alecxs was right on the money. I must have had something with a hidden character, or "white space" that was causing issues. I went in and deleted all the "empty" rows, made sure that everything was deleted after the last visible character on every line. Then ran the script, and it all worked. Thanks once again to @alecxs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.