Regular expression on a remote script - bash

I'm trying to run a script on a remote machine (both machines use bash), using ssh, which has the following lines:
cd /home/invitado/
rm -r !(Desktop|Downloads|Videos|Pictures)
So when I run ssh hostname './remove', I get this error:
syntax error near unexpected token `('
I tried appending this line to the script
shopt -s extglob
But I'm still having the same error, so what should I do? Thanks for your help.

shopt -s extglob should be before rm -r !(Desktop|Downloads|Videos|Pictures).
To verify, see what is the original value of extglob by running just shopt.
Note: In my case, that value was set when I run the command rm directly from the shell. But when I put it in script, it failed. Realized that extglob is set by default when running from interactive shell. & It is disabled by default when running inside the script.

Related

How to delete a file with string.numbers.string format by passing a command in Airflow

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

scp command fails with syntax error when used in a bin/sh script

The following command works perfectly when called directly from the terminal
scp -r ./!(node_modules|public) $SERVER:$WEBSITE_SRC
but when the same command is added to a shell script it fails with the following error
line 9: syntax error near unexpected token `('
here is the script for reference:
#!/bin/sh
set -e
SERVER=127.0.0.1
WEBSITE_SRC=~/website/src
echo "Deploying changes to the website src code"
scp ./.env* $SERVER:$WEBSITE_SRC
scp -r ./!(node_modules|public) $SERVER:$WEBSITE_SRC
!(node_modules|public) is extended globbing syntax. You should do two things.
Change the shebang line to #!/bin/bash to make sure your script is run by bash.
Put shopt -s extglob near the top to enable extended globbing syntax.
If it works interactively, it's probably because you have shopt -s extglob in one of your shell initialization files (e.g. ~/.bashrc).

Problem using cp command with negation (!) on crontab

#!/bin/bash
cd /var/www/html/tpd
cp -pr !(edc|dti|swb|audio|blog|buy|dpt|dpt.git|dpt-staging-server|dti_old|images|images2|images_linkshare|smarty-3.1.30|swb.com|swb.com.temp|talent|template_c|video|yout|yout-admin|All_About_Your_Canine_Friend.pdf|Canine_Cuisine.pdf|definitiveguide.pdf|GroomingYourDogAtHome-FreeReport.pdf|HowToStopYourPuppyOrOlderDogFromBiting.pdf|Network Merchants API.pdf|SuperDogsandPuppies.pdf|TopDogs.pdf|UltimateBreedGuide.pdf|10CommonProblemsofAdultDogs.pdf|10CommonProblemsofPuppies.pdf|45commonlyaskedquestionsondoggrooming.pdf|g.tar.gz|h.tar.gz|dti_back.tar.gz|Canine_Cuisine.zip|deluxe_version.zip|h.zip|StyleXPInstallMale.zip|UltimateBreedGuide.zip|videotranscripts.zip|101ways.zip) weeklyBackup
tar -cvf weeklyBackup.tar.gz weeklyBackup
rm -rf weeklyBackup
I am taking a weekly backup for only some selected files and this is the script that I am using. It works perfectly when running manually but when I am entering this is a crontab, it returns an error line 3: syntax error near unexpected token `('
The syntax !(name1|name2|name3) is what's called an "extglob". This is an optional extension to bash, not enabled by default.
To enable this syntax, you need to run (as a prior line in your script):
shopt -s extglob
Presumably your dotfiles are already doing this for interactive shells, which is why the syntax works for you out-of-the-box. (Also ensure that if your script is invoked from cron with sh scriptname, bash scriptname is used instead, or the code is modified to honor the shebang).

Shell globbing negation doesn't work in bash

I'm currently confused why shell globbing in terminal works with negation but shows an error when running in bash.
Take the commands executed in the terminal below, which shows all js files within the ./HTML directory except for js files that ends with .bundle.js.
$ shopt -s globstar
$ ls ./HTML/**/!(*.bundle).js
The command above works perfectly, now let's put it in a bash file
list-js.sh
#!/usr/bin/env bash
shopt -s globstar
ls ./HTML/**/!(*.bundle).js
Executing it in a terminal:
$ bash list-js.sh
list-js.sh: line 4: syntax error near unexpected token `('
list-js.sh: line 4: `ls ./HTML/**/!(*.bundle).js'
As you can see, it shows a syntax error.
globstar only enables the ** pattern. The extglob option allows !(...). Somewhere in your interactive shell, that has already been enabled (perhaps in your .bashrc, perhaps you typed shopt -s extglob earlier). However, it needs to be enabled explicitly in your script, since such settings are not inherited from the shell that starts the script.
#!/usr/bin/env bash
shopt -s globstar extglob
ls ./HTML/**/!(*.bundle).js
(As an aside, ** without globstar does not cause a syntax error because it is treated simply as two adjacent *s, the second one being redundant.)

When creating symbolic links on ubuntu I sometimes get an odd result

I'm trying to create a bunch of symbolic links for all the files in a directory. It seems like, when I type this command in the shell manually, it works just fine, but when I run it in a shell script, or even use the up arrow to re-run it, I get the following problem.
$ sudo ln -s /path/to/my/files/* /the/target/directory/
This should create a bunch of sym links in /path/to/my/files and if I type the command in manuall, it indeed does, however, when I run the command from a shell script, or use the up arrow to re-run it I get a single symbolic link in /the/target/directory/ called * as in the link name is actually '*' and I then have to run
$ sudo rm *
To delete it, which just seems insane to me.
When you run that command in the script, are there any files in /path/to/my/files? If not, then by default the wildcard has nothing to expand to, and it is not replaced. You end up with the literal "*". You might want to check out shopt -s nullglob and run the ln command like this:
shopt -s nullglob
sudo ln -s -t /the/target/directory /path/to/my/files/*
Maybe the script uses sh and your using bash when executing the command.
You may try something like this:
for file in $(ls /path/to/my/files/*) do
ln -s "${file}" "/the/target/directory/"${file}"
done

Resources