Bash command substution error - bash

The code:
`cat <(fgrep -i -v "$DAEMON_TEST" <(sudo -u asm crontab -l)) <(echo "$CRON") | sudo -u asm crontab -`
The error:
command substitution: line 46: syntax error near unexpected token `('
/etc/init.d/asm: command substitution: line 46: `cat <(fgrep -i -v "$DAEMON_TEST" <(sudo -u asm crontab -l)) <(echo "$CRON") | sudo -u asm crontab -'
The command runs fine when run directly into the shell by replacing the variables with the relevant strings
Here are the variables:
DAEMON_TEST=asm_test.php
CRON="*/15 * * * * /opt/asm/daemons/test.php"

The issue ended up being completely unrelated.
Changed:
#/bin/sh
to:
#/bin/bash
I never realized there would be a differnce
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh

Related

awk quoting program text

if I run:
awk '/-jar org.eclipse.osgi.jar --launcher.suppressE/ {print "-Dcom.abc.service.gw.enableValidation=false \\"}1' rest_gw.sh >> new_gw.sh
the command inserts a new line above line -jar org.eclipse.osgi.jar --launcher.suppressE
-Djava.security.properties=/var/vcap/packages/helpers/data.properties \
-Dcom.abc.service.gw.enableValidation=false \ <<<<<<<<<<<<<<<<<<<<<<<<<
-jar org.eclipse.osgi.jar --launcher.suppressErrors -consoleLog &
If I run the following command, it gives: awk: line 1: runaway string constant "-Dcom.abc. ...
bosh -d test-105 ssh service/0 -c 'sudo /usr/bin/awk "/-jar org.eclipse.osgi.jar --launcher.suppressErrors/ {print \"-Dcom.abc.service.gw.enableValidation=false \\\"}1" /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
Tried:
bosh -d test-105 ssh service/0 -c 'sudo /usr/bin/awk '\'/-jar org.eclipse.osgi.jar --launcher.suppressErrors/ {print \"-Dcom.abc.service.gw.enableValidation=false \\\"}1\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
no luck so far. can someone suggest the correct way to do it? TYA!
EDIT:
sed command as recommended:
bosh -d test-105 ssh service/0 -c 'sudo sed '/-jar org.eclipse.osgi.jar --launcher.suppressE/i -Dcom.abc.service.gw.enableValidation=false \\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
unknown flag launcher.suppressE/i'
This worked finally:
bosh -d test-105 ssh service/0 -c "sudo sed '/-jar org.eclipse.osgi.jar --launcher.suppressErrors -consoleLog/i -Dcom.abc.service.gw.enableValidation=false \\\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh"
I strongly suggest use sed command insert your line as:
sed '/-jar org.eclipse.osgi.jar --launcher.suppressE/i -Dcom.abc.service.gw.enableValidation=false \\' rest_gw.sh >> new_gw.sh
It is cleaner and simple.

Bash, Systemd Service, command substitution - Not playing well together

I have the following in my redis#.service file:
...
ExecStop=/bin/bash -c \"/usr/local/bin/redis-cli -p $(echo %i | awk -F \'.\' \'{ print $2 }\') shutdown\"
...
Then when I run:
sudo systemctl stop redis#redis.6379.test-site.com.service
Followed by:
sudo systemctl status redis#redis.6379.test-site.com.service
I get:
...
Process: 10042 ExecStop=/bin/bash -c "/usr/local/bin/redis-cli -p $(echo redis.6379.test-site.com | awk -F '.' '{ print $2 }') shutdown" (code=exited, status=1/FAILURE)
...
Sep 10 17:36:53 hostname bash[10042]: -p: -c: line 0: unexpected EOF while looking for matching `"'
Sep 10 17:36:53 hostname bash[10042]: -p: -c: line 1: syntax error: unexpected end of file
Then if I run:
sudo systemctl start redis#redis.6379.test-site.com.service
Followed by (taken from the systemctl status output):
/bin/bash -c "/usr/local/bin/redis-cli -p $(echo redis.6379.test-site.com | awk -F '.' '{ print $2 }') shutdown"
Then the command works as expected...
Can anyone shed some light as to what's happening here?
If Systemd is just running the command shown for ExecStop, and that command works just fine when run manually, shouldn't it also work when systemd runs it?
I have a feeling it's something to do with the quoting, but I'm too much of a newb at bash to wrap my head around it.

Bracket issue in snakemake cluster command

I am using snakemake 4.0.0 on aws cfncluster with the following commands.
rule fastq_to_counts:
input: fastql="/shared/dbGAP/sras2/fastq.gz/{sample}_1.fastq.gz", fastqr="/shared/dbGAP/sras2/fastq.gz/{sample}_2.fastq.gz"
output: "/shared/counts/{sample}"
shell: '/shared/packages/sailfish-master/bin/sailfish quant -i /shared/packages/gencode26/gencode26 -l IU -p 1 -1 <(zcat {input.fastql}) -2 <(zcat {input.fastqr}) --output {output}'
It runs fine on the head node but when I submit to head node i get the following error.
/shared/packages/sailfish-master/bin/sailfish quant -i /shared/packages/gencode26/gencode26 -l IU -p 1 -1 <(zcat /shared/dbGAP/sras2/fastq.gz/xxx.fastq.gz) -2 <(zcat /shared/dbGAP/sras2/fastq.gz/xxx.fastq.gz) --output /shared/counts/SRR1075530
/bin/sh: 1: Syntax error: "(" unexpected
Error in job fastq_to_counts while creating output file /shared/counts/xxx.
RuleException:
The issue is that the "sh" command does not support "bashism" of the "("
is there any way to force snakemake to use /bin/bash ?
Thanks
found that the solution is adding the following to snakefile
shell.executable('bash')

Run .sh file from cygwin on windows 7

I am trying to run .sh file from cygwin on windows 7
My dumdb.sh file content
#!/bin/bash
for database in $(mysql -e "show databases"|awk -F " " '{print $1}') do
mysqldump -u root -h localhost -p $database > $database.sql
done
On running this command
$ sh dumpdb.sh
m getting following error
bash: line 3: syntax error near unexpected token `mysqldump'
bash: line 3: `mysqldump $database > $database.sql'
Where I am doing wrong?
You are missing a ; before do:
#!/bin/bash
for database in $(mysql -e "show databases"|awk -F " " '{print $1}') ; do
mysqldump $database > $database.sql
done

bash script works on bootup, doesn't in terminal

I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. It includes a command to add a line to crontab for a user using the answer given here.
directory="/home/intahwebz/current/tools/amazon/"
command="cd $directory && sh backupSQLToS3.sh"
job="15 1 */2 * * $command"
cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -
This script appears to work fine during bootup as it displays no error messages and the cronjob is installed in the crotab.
However I'd also like the script to run during server upgrades. Attempting to run the script from the command line gives the error:
installCrontab.sh: line 14: syntax error near unexpected token `('
installCrontab.sh: line 14: `cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -'
What do I need to fix this error?
your approach is working perfectly for me:
$ whoami
test
$ echo $SHELL
/bin/bash
$ command="cd $directory && sh backupSQLToS3.sh"
$ job="15 1 */2 * * $command"
$ crontab -l
$ cat <(fgrep -i -v "$command" <(crontab -u test -l)) <(echo "$job") | crontab -u test -
$ crontab -l
15 1 */2 * * cd && sh backupSQLToS3.sh
I missed to set the "directory" variable but your code works fine for me.
It looks like you are using the bourne shell (/bin/sh) to execute a bash script. Try using bash instead of sh.

Resources