If I want to delete everything in the current directory except for "myfile", I can use
rm -r !("myfile")
But if I put this in a script (called cleanup
):
#!/bin/bash
rm -r !("myfile")
I get:
pi@raspberrypi:~/tmp $ ./cleanup
./cleanup: line 2: syntax error near unexpected token `('
./cleanup: line 2: `rm -r !("file2")'
If I run
ps -p $$
I can see that my terminal is using bash,
PID TTY TIME CMD
1345 pts/3 00:00:02 bash
so I'm unclear on what the problem is.
Notes:
rm -r !("cleanup"|"myfile")
, but the error message is the same either way.The !(pattern-list)
pattern is an extended glob. Many distros have it enabled for interactive shells, but not for non-interactive ones. You can check that with
$ shopt extglob
extglob on
$ bash -c 'shopt extglob'
extglob off
To fix your script, you have to turn it on: add
shopt -s extglob
at the beginning of it.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加