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).
Related
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
This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 4 years ago.
I have the following bash script test.sh (with execution permissions):
#!/bin/sh
CAT_BIN="cat"
"$CAT_BIN" <(tail -n +2 test.sh)
It gives me that error when I run it:
$ ./test.sh
./test.sh: line 4: syntax error near unexpected token `('
./test.sh: line 4: `"$CAT_BIN" <(tail -n +2 test.sh)'
However, when I source the following commands it executes alright.
$ CAT_BIN="cat"
$ "$CAT_BIN" <(tail -n +2 test.sh)
How can this work in a script? (Use <(tail -n +2 test.sh) inline as a filename argument)
The <(tail -n +2 test.sh) construct is a bash feature, so you need to run your script in the bash shell,
Replace your top line
#!/bin/sh
with
#!/bin/bash
(Or the proper path to the bash executable if it is not /bin/bash on your system)
Note, even if /bin/sh is e.g. a symlink to bash, it will start bash in posix compatibility mode when you run it as /bin/sh , and many bash specific features will not be available)
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
$ sh
sh-3.2$ if
> ps -ef | grep apple ;
> then
> echo APPLE
> fi ;
lazer 7584 7571 0 04:36 pts/4 00:00:00 grep apple
APPLE
sh-3.2$ exit
exit
$ which sh
/bin/sh
$ /bin/sh -c if ps -ef | grep apple ; then echo APPLE fi ;
bash: syntax error near unexpected token `then'
$
As above, my simple if statement works as expected when executed line by line but gives me the following error when executed using sh -c:
bash: syntax error near unexpected token `then'
What am I missing here?
Your interactive shell will be escaping the invocation via sh -c. In particular it's taking everyting after the semi-colon as a new statement.
Quote everything that you're feeding to /bin/sh e.g.
$ /bin/sh -c "if ps -ef | grep apple ; then echo APPLE fi ;"
I think you may also need to delimit further using semi-colons given that you're condensing everything onto one line, and would perhaps suggest you could use a heredoc.
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.