I can't find out what's happening. Is this regular expression not working for shell scripts?
sudo rm -R -f '/web/!(release)'
Any ideas?
thanks
Perhaps you forgot to enable extglob. Also you shouldn't quote your extended glob pattern:
shopt -s extglob
sudo rm -R -f '/web/'!(release)
Also if the shell you're calling sudo with is not able to access /web, you can wrap up your command with bash:
sudo bash -c "shopt -s extglob"$'\n'"rm -R -f '/web/'!(release)"
See Pattern Matching and Filename Expansion.
Script file
shopt -s extglob
sudo rm -R -f 'web/'!(release);
Result
script.sh: Syntax error: "(" unexpected
edit :
I was running with "sh" instead of bash
now with bash script.sh it works
thanks
Related
This is the file format I have ABD.123244.trig , when I try to remove the file in the terminal I used rm -f ABD.+([0-9]).trig, but when I use the same thru Airflow scheduler, it throwserror unexpected token '('
please help
This command rm -f ABD.+([0-9]).trig doesn't work while passing it thru Airflow scheduler. it throws error unexpected token '('
Airflow bash operator runs the bash command with:
bash -c "<your bash command>"
To use an extended globbing pattern in bash, you have to enable the extglob shell option. You can try:
BashOperator(task_id="bash_task", bash_command="shopt -s extglob\n rm -f ABD.+([0-9]).trig")
The + is not a special character in bash, therefore, your expression +(...) does not make any sense. Neither does .+.
You are thinking regular expressions instead of gobbing patterns.
Try this:
echo ABD.[0-9]*
and the whole command would be:
rm -f ABD.[0-9]*.trig
In BASH, the following command removes everything in a directory except one file:
rm -rf !(filename.txt)
However, in SSH the same command changes nothing in the directory and it returns the following error: -jailshell: !: event not found
So, I escaped the ! with \, (the parentheses also require escaping) but it still doesn't work:
rm -rf \!\(filename.txt\)
It returns no error and nothing in the directory changed.
Is it even possible to run this command in SSH? I found a workaround but if this is possible it would expedite things considerably.
I connect to the ssh server using the alias below:
alias devssh="ssh -p 2222 -i ~/.ssh/private_key user#host"
!(filename.txt) is an extglob, a bash future that might have to be enabled. Make sure that your ssh server runs bash and that extglob is enabled:
ssh user#host "bash -O extglob -c 'rm -rf !(filename.txt)'"
Or by using your alias:
devssh "bash -O extglob -c 'rm -rf !(filename.txt)'"
If you are sure that the remote system uses bash by default, you can also drop the bash -c part. But your error message indicates that the ssh server runs jailshell.
ssh user#host 'shopt -s extglob; rm -rf !(filename.txt)'
devssh 'shopt -s extglob; rm -rf !(filename.txt)'
I wouldn't do it that way. I wouldn't rely on bash being on the remote, and I wouldn't rely on any bashisms. I would use:
$ ssh user#host 'rm $(ls | grep -v "^filename.txt$")'
If I wanted to protect against the possibility that the directory might be empty, I'd assign the output of $(...) to a variable, and test it for emptiness. If I was concerned the command might get too long, I'd write the names to a file, and send the grep output to rm with xargs.
If it got too elaborate, I'd copy a script to the remote and execute it.
The following command set is working on linux prompt.
%cd ${ADIR}/exe; shopt -s extglob; rm -rf !(BDIR)
But it is not working in Makefile
Linux command - works
%cd ${ADIR}/exe; shopt -s extglob; rm -rf !(BDIR)
Command in Makefile
#cd ${ADIR}/exe; shopt -s extglob; rm -rf !\(BDIR\)
Make file message
rm: cannot remove `!(BDIR)': No such file or directory
The problem with your Makefile is that it escapes ( and ), which makes the shell interpret them literally.
The second issue,
/bin/sh: -c: line 0: syntax error near unexpected token `('
is caused by make using sh to execute commands, not bash.
The !(...) wildcard syntax (and extglob) are only supported by bash, not sh.
You could call bash explicitly:
#bash -c 'cd ${ADIR}/exe; shopt -s extglob; rm -rf !(BDIR)'
But that doesn't work either, because extglob doesn't take effect until the next line of input has been read, so !( ) still throws a syntax error.
We need a way to run a multi-line command using a single invocation of the shell. Unfortunately make makes this unnecessarily complicated.
One possible solution:
SHELL = /bin/bash
...
#bash -c $$'cd ${ADIR}/exe; shopt -s extglob\nrm -rf !(BDIR)'
This tells make to use bash to execute all recipes (not /bin/sh). We then run bash again manually, but using $'...' to quote the command string. This lets us write \n to embed a literal newline, which makes extglob / !( ... ) work.
We need double $$ to escape the $ for make, so $'...' becomes $$'...'.
I'm not very happy with this solution.
Unfortunately there's a weird behavior of bash in that the shopt setting won't take effect until the newline, so any globbing on the same line won't recognize it. Try this at your shell prompt:
$ shopt -s extglob; echo !(BDIR)
bash: !: event not found
Then try it in two lines:
$ shopt -s extglob
$ echo !(BDIR)
...works
Unfortunately this means it's almost impossible to use this with make.
You should use the POSIX-compatible version suggested in triplee's comment and avoid the need for special shells altogether.
Oh, it seems the answer was deleted. Anyway, do something like this instead:
foo:
cd ${ADIR}/exe && for f in *; do \
case $$f in (BDIR) : ok ;; (*) rm -rf "$$f" ;; esac; \
done
When running the following command:
rm -rf !(file1|file2)
all files except file1 and file2 are removed; as intended.
When either placing this command in a bash script:
#!/bin/bash
rm -rf !(file1|file2)
or running it using bash -c:
bash -c "rm -rf !(file1|file2)"
I receive the following error:
syntax error ner unexpected token '('
I have tried setting the shell options using
shopt -s extglob
yeilding in:
bash -c "shopt -s extglob; rm -rf !(file1|file2)"
to enable glob according to:
https://superuser.com/questions/231718/remove-all-files-except-for-a-few-from-a-folder-in-unix and some other questions as well.
Still it doesn't work, and I'm at loss.
First of all, for safety, let's do our testing with echo !(file1|file2) instead of rm -rf !(file1|file2).
Anyway, bash does some parsing of the entire command line before executing the shopt -s extglob command. When bash encounters the (, the extglob option isn't set yet. That's why you get the error.
Try this instead:
bash -O extglob -c 'echo !(file1|file2)'
In your script, you just need to turn on the option as a separate command line before relying on it:
#!/bin/bash
shopt -s extglob
echo !(file1|file2)
You can actually do this with the -c flag also:
bash -c 'shopt -s extglob
echo !(file1|file2)'
Or even like this:
bash -c $'shopt -s extglob\necho !(file1|file2)'
I cannot figure it out, i understand shopt -s cdspell but cannot find out what shopt -s dirspell does.
The Bash Reference Guide says:
dirspell
If set, Bash attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist.
I tried several times on several directories but that is not the behavior.
I'm using bash 4.2.10(2) on i386-apple-darwin10.7.0
From the change-log
x. There is a new shell option: `dirspell'. When enabled, the filename
completion code performs spelling correction on directory names during
completion.
Let's try:
$ ls
spam/
$ cat spam/test
hello world
without dirspell
$ cat span/test [tab]
# nothing happens
with dirspell
$ shopt -s dirspell
$ cat span/test [tab]
#line is replaced by
$ cat /home/user/tmp/shopt/spam/test
If you set the shell options direxpand and dirspell, then the tab-completion does work.