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

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

Related

cat <(echo 'hello') can run, but can't run in shell script mode

If I run cat <(echo 'hello') in the [root#hostname]#, it shows correctly
But if I turn the above into below script test.sh:
#!/bin/sh
cat <(echo 'hello')
and run sh -x test.sh, it get back to me below errors:
./test.sh: line 3: syntax error near unexpected token `('
./test.sh: line 3: `cat <(echo 'hello')'
Reason ask this is because I follow this thread(the green answer one), it prompt the same error.
It looks like you are using Bash-specific syntax in your script, so you should change the shebang line to something like:
#!/bin/bash
and if you want to invoke the program like you were doing you should run:
bash -x test.sh

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 $

Bash functions inside process substitution

Getting an error trying to call a function inside a process substitution.
Is there any way to do this?
#!/bin/bash
function testfunc
{
echo "bork"
}
diff <(testfunc) <(echo "bork")
The error is:
bork.sh: line 7: syntax error near unexpected token `('
bork.sh: line 7: `diff <(testfunc) <(echo "bork")'
--Update--
Problem was calling sh bork.sh, instead of bash ./bork.sh . Moral of the story make sure which shell you are executing with.
There's no problem here:
$ chmod +x test.sh
$ ./test.sh
Clear diff. No problem!
$ bash -x ./test.sh
+ diff /dev/fd/63 /dev/fd/62
++ testfunc
++ echo bork
++ echo bork
Proof that it worked
Troubleshooting:
Maybe you
run in a restricted shell
you don't have /dev/fd available/mounted correctly (due to somekind of secure chroot jail?)
The problem is probably that you're running the command with sh instead of bash.
$ cat > xx.sh
#!/bin/bash
function testfunc
{
echo "bork"
}
diff <(testfunc) <(echo "bork")
$ sh xx.sh
xx.sh: line 7: syntax error near unexpected token `('
xx.sh: line 7: `diff <(testfunc) <(echo "bork")'
$ bash xx.sh
$
The process substitution is not portable to the shell in /bin/sh. See the Bash manual on POSIX mode and bullet 28:
Process substitution is not available.
Tested on Mac OS X 10.10.5 (Yosemite).

Bash unexpected token when using "&;"

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.

Array bash script doesn't work

#!/bin/bash
ARRAYNAME=( 'time1' 'life' 'time2' )
echo ${ARRAYNAME[1]}
In the above code when i run my script as
$ sh ex1.sh
it gives an error message:
ex1.sh: 2: Syntax error: "(" unexpected
Why is this?
sh on your system is not bash.
Your "shebang" lines uses bash shell ("/bin/bash") but you're probably invoking another shell ("sh") invoking another shell to execute your script. Try this :
$ chmod 700 ex1.sh
This will make your script executable. Then run it :
$ ./ex1.sh

Resources