Bash unexpected token when using "&;" - bash

I'm launching a script via SSH with this command:
ssh myuser#myhost "bash -x -c 'source config.sh; nohup Start_JBoss.sh > /dev/null &; sleep 5'"
however I'm hitting an error:
Connecting to: myhost with myuser
Password:
bash: -c: line 0: syntax error near unexpected token `;'
bash: -c: line 0: `source config.sh; nohup Start_JBoss.sh > /dev/null &; sleep 5'
What's I'm doing wrong here? I know that SSH is not the cause as the same command fails into a local shell.
I've read some similar thread where there's some mention on Bash version as culprit for some other kind of unexpected token issue so adding here just for reference in case is needed:
bash -version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Guess I did not searched hard enough as I found out solution here https://superuser.com/questions/269429/why-did-after-return-an-unexpected-token-error-in-bash
but leaving here as reference.
Basically if you send to background a process you do not need to use ; as the shell is already ready to accept new command.

Related

Unable to execute ssh command containing parentheses with perl: "syntax error near unexpected token `('"

If I run this command from the command line, it works as expected on the remote server:
ssh admin#example.com "docker exec -it -d tasks_live_work_ec2_test_server /bin/sh -c \"/usr/bin/nvim -c 'silent! call SetupInstantServer()'\""
However, if I try to execute it from a perl script, I get errors:
my $cmd = qq|docker exec -it -d ${image_name}_server /bin/sh -c \"/usr/bin/nvim -c 'silent! call SetupInstantServer()'\"|;
`ssh admin\#example.com "$cmd"`;
bash: -c: line 1: syntax error near unexpected token '('`
Escaping the parens with backslashes suppresses the error, but the SetupInstantServer function in vim never gets called.
What I would do, using 2 here-doc:
#!/usr/bin/perl
system<<PerlEOF;
ssh admin\#example.com<<ShellEOF
docker exec -it -d ${image_name}_server /bin/sh -c "
/usr/bin/nvim -c 'silent! call SetupInstantServer()'
"
ShellEOF
PerlEOF
You can decide to add quotes on a 'HereDoc' to prevent shell expansion or the need to escape #. Up to your needs.
Check perldoc perlop#Quote-and-Quote-like-Operators

bash: syntax error near unexpected token `newline' for "&! "

I have the following script.sh running in zsh. But since I am running the script as sudo user used shell differs between each other.
#!/bin/bash
echo $SHELL
nohup geth --syncmode fast --cache=1024 > geth_server.out 2>&1 &!
Output in macOS:
$ chmod +x script.sh
$ sudo ./script.sh
/bin/sh
syntax error near unexpected token `newline'
which generated following error on macOS but works fine in linux: syntax error near unexpected token newline'`
Output in linux:
$ chmod +x script.sh
$ sudo ./script.sh
/bin/bash
# works fine
The error seems like generated due to ! at the end of the line. Is there anyway to fix this, what may be the reason for this? Exact same script works fine in linux environment

How do I execute a remote script as a build phase in xcode?

I have a remote script which I can execute from command line without an issue:
bash <(curl -sSL 'goo.gl/p1GPQw') 29165BE4-EA61-8228-9F33-A9B9DAD481A0 ./Info.plist
Problem comes when I try to use it in as a build phase of my iOS app (to update a build number).
The error log:
/Users/pr.......: line 2: syntax error near unexpected token `('
/Users/pr.......: line 2: `bash <(curl -sSL 'goo.gl/p1GPQw') 7CD17FC7-E724-7240-34ED-927122733119 ./Info.plist'
So the question is, are there any limitations in what you can through xcode execute or is it some error in syntax?
Update:
If I change the /bin/sh to /bin/bash, it works ... so the updated question is, what is the /bin/sh equivalent of bash <(curl -sSL 'goo.gl/p1GPQw') 29165BE4-EA61-8228-9F33-A9B9DAD481A0 ./Info.plist as making users change this is another complication that can make the entire thing go sideways.
I tried to simplify the problem, for purpose of reproducibility:
This is 1.sh script:
#!/usr/bin/bash
echo echo Hello World
echo echo Today : $(date)
When run in /bin/bash, it works fine:
bash $ bash <(./1.sh)
Hello World
Today : Thu, Sep 22, 2016 2:32:19 AM
$
When run in /bin/sh it gives an error:
sh $ bash <(./1.sh)
sh: syntax error near unexpected token `('
sh $
But, the following should work fine even in /bin/sh:
sh $ bash -c "bash <(./1.sh)"
Hello World
Today : Thu, Sep 22, 2016 2:35:35 AM
sh $

Automate SSH and running commands

I am trying to automate SSH into a machine and then run some commands. However, I am getting stuck at the SSH portion:
for h in ${hosts[*]}; do
ssh -i foo.pem bc2-user#$h
echo "here"
sudo bash
cd /data/kafka/tmp/kafka-logs/
exit
exit
done
As soon as I run this script, I am able to SSH in but the automation stops at this message:
********************************************************************************
This is a private computer system containing information that is proprietary
and confidential to the owner of the system. Only individuals or entities
authorized by the owner of the system are allowed to access or use the system.
Any unauthorized access or use of the system or information is strictly
prohibited.
All violators will be prosecuted to the fullest extent permitted by law.
********************************************************************************
Last login: Thu Dec 10 10:19:23 2015 from 10.81.120.55
-bash: ulimit: open files: cannot modify limit: Operation not permitted
When I press control + d, my echo "here" commands executes and then my script exits. Without performing the rest of the commands.
I read around and I tried this but I am getting this syntax error:
./kafka_prefill_count.sh: line 38: warning: here-document at line 26 delimited by end-of-file (wanted `EOF')
./kafka_prefill_count.sh: line 39: syntax error: unexpected end of file
script:
for h in ${hosts[*]}; do
ssh -i foo.pem bc2-user#$h << EOF
echo "here"
sudo bash
cd /data/kafka/tmp/kafka-logs/
ls | grep "$dir_name"
exit
exit
bash -l
EOF
done
Your current script isn't really how you would execute commands remotely using ssh.
It should look more like this
shh -i foo.pem user#host 'echo "here" ; hostname'
(hostname command is just example to prove its running on other machine.
Good resource
Just saw your edit:
EOF needs to be all the way to the left.
ssh -i foo.pem bc2-user#$h << EOF
echo "here"
sudo bash
cd /data/kafka/tmp/kafka-logs/
ls | grep "$dir_name"
exit
exit
bash -l
EOF
Try this with two here documents, one for ssh and one for bash:
ssh -i foo.pem bc2-user#$h << EOF1
echo "remote"
sudo bash << EOF2
cd /data/kafka/tmp/kafka-logs/
ls | grep "$dir_name"
EOF2
EOF1

Using bash -c and Globbing

I'm running gnu-parallel on a command that works fine when run from a bash shell but returns an error when parallel executes it with bash using the -c flag. I assume this has to do with the special globbing expression I'm using.
ls !(*site*).mol2
This returns successfully.
With the flag enabled the command fails
/bin/bash -c 'ls !(*site*).mol2'
/bin/bash: -c: line 0: syntax error near unexpected token `('
The manual only specifies that -c calls for bash to read the arguments for a string, am I missing something?
Edit:
I should add I need this to run from a gnu-parallel string, so the end resultant command must be runnable by /bin/bash -c "Some Command"
You should try the following code :
bash <<EOF
shopt -s extglob
ls !(*site*).mol2
EOF
Explanation :
when you run bash -c, you create a subshell, and shopt settings are not inherited.
EDIT
If you really need a one liner :
bash -O extglob -c 'ls !(*site*).mol2'
See this thread

Resources