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

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

Related

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

Executing 'bash -c' in 'docker exec' command

Context: I'm trying to write a shortcut for my daily use of the docker exec command. For some reasons, I'm experimenting the problem that my output is sometimes broken when I'm using a bash console inside a container (history messed up, lines overwrite each other as I'm writing, ...)
I read here that you could overcome this problem by adding some command before starting the bash console.
Here is a relevant excerpt of what my script does
#!/bin/bash
containerHash=$1
commandToRun='bash -c "stty cols $COLUMNS rows $LINES && bash -l"'
finalCommand="winpty docker exec -it $containerHash $commandToRun"
echo $finalCommand
$finalCommand
Here is the output I get:
winpty docker exec -it 0b63a bash -c "stty cols $COLUMNS rows $LINES && bash -l"
cols: -c: line 0: unexpected EOF while looking for matching `"'
cols: -c: line 1: syntax error: unexpected end of file
I read here that this had to do with parsing and expansion. However, I can't use a function or an eval command (or at least I didn't succeed in making it work).
If I execute the first output line directly in my terminal, it works without trouble.
How can I overcome this problem?
It's not Docker related, but Bash (In other words, the docker's part of the command works well, it's just bash grumbling on the container like it would grumble on your host):
Minimal reproducible error
cmd='bash -c "echo hello"'
$cmd
hello": -c: line 0: unexpected EOF while looking for matching `"'
hello": -c: line 1: syntax error: unexpected end of file
Fix
cmd='bash -c "echo hello"'
eval $cmd
hello
Answer
foo='docker exec -it XXX bash -c "echo hello"'
eval $foo
This will let you execute your command echo hello on your container, now if you want to add dynamic variables to this command (like echo $string) you just have to get rid of single quotes for double ones, to make this works you will have to escape inner double quotes:
foo="docker exec -it $container bash -c \"echo $variable\""
A complete example
FOO="Hello"
container=$1
bar=$2
cmd="bash -c \"echo $FOO, $bar\""
final_cmd="docker exec -it $container $cmd"
echo "running command: \"$final_cmd\""
eval $final_cmd
Let's take time to dig in,
$FOO is a static variable, in our case it works exactly like a regular variable, just to show you.
$bar is a dynamic variable which takes second command line argument as value
Because $cmd and $final_cmd uses only double quotes, variables are interpreted
Because we use eval $final_cmd command is well interpreted, bash is happy.
Finally, a usage example:
bash /tmp/dockerize.sh 5b02ab015730 world
Gives
running command: "docker exec -it 5b02ab015730 bash -c "echo Hello, world""
Hello, world

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 for loop work in command line, but failed in script

When a run a for statement in debian bash command line, it works fine.
But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'"
Where is the difference?
[leon#www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon#www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
BTW, all works fine in centos enviorment.
Use the -c option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.
bash -c 'for i in {1..10}; do echo $i; done'
your bash command line ends with the first ;
so it gets executed separately as:
bash for i in {1..10};
do echo $i;
done
and man bash says command argument should be a file to load: bash [options] [file]
You can wrap all your script inside inverted commas or in a file. Because here, you're doing bash for i in {1..10} then do echo $i and so on. You should use -c option if you don't put it in a file.

Resources