Bash: "syntax error near unexpected token" that's a valid token - bash

I have the following bash code in a script file:
if [ 'false' == 'true' ]
then
write_map() >> 'groups.txt'
Groups='groups.txt'
fi
When I try to run the script, I get this message from bash, and nothing is run:
syntax error near unexpected token `>>'
Why is >> an "unexpected token?
Why is bash failing on code that is inside an "if" that won't be run?
This bash code was created by Cromwell wdl. write_map is a wdl function, not a bash function. Could that be what's breaking bash?

So, there were two issues
write_map was being called wrong in the wdl code that was the source for this bash code
When called correctly, write_map is turned into a file, and you can't >> a file (you have to cat it then >>)

Related

Syntax error near unexpected token `;' when running a command in Bash

In Bash in the terminal, it is impossible to run a command that ends with and & (being sent to the background) followed by another command (obviously with a ; between them). Why is that? Why can't you run anything with &; or $ ; in it?
I'm trying to recreate a 502 error and was trying to DoS a specific page in a testing server. I was trying to run this:
while true; do curl -s https://some.site.com/someImage.jpg > /dev/null &; echo blah ; done
as a "one-liner" in the terminal. However, I got this error:
-bash: syntax error near unexpected token `;'
However the commands work individually, and when I run the curl command not in the background it works as a loop as well. It also works when it write a one line script, "/tmp/curlBack.sh" that includes only
curl -s https://some.site.com/someImage.jpg > /dev/null &
And then run
while true; do bash /tmp/curlBack.sh ; echo blah ; done
Why am I getting this error and how do I fix it?
The problem is with the semi-colon after the ampersand (&):
An ampersand does the same thing as a semicolon or newline in that it indicates the end of a command, but it causes Bash to execute the command asynchronously.
BashSheet
Basically, it's as if you were to put double semi-colons back to back, that would cause a syntax error too.
To fix this problem, you simply need to remove the semi-colon, I tested it that way and it seems to be working:
while true; do curl -s https://some.site.com/someImage.jpg > /dev/null & echo blah ; done

Passing parameters to a bash function in bashrc

I have the code below in my .bashrc file,
function gitex() {
ssh-agent bash -c "ssh-add ~/.ssh/xxx; git $1";
}
If I try the command gitex status,
I keep getting "bash: syntax error near unexpected token `status'
Is there a way to fix this?

Jenkins file execution error while running shell commands

The below code is being used to trigger a shell command using Jenkins file.
sh """
val=\$(echo ${val} | sed 's/[^a-zA-Z0-9]//g')
echo ${val}
"""
The below error reflects as part of the code :
/home/me/#tmp/abcd/script.sh: command substitution: line 4: syntax error near unexpected token `('
Any solution to the same ? Thanks in advance!

shell script error status and syntax checking

I have written a shell script and I am using few commands like rm, ls, etc. In case where those commands fails , I am checking the return status '$?' . But If the script has some syntax error, how can I get the error status of it ? Basically I am going to source this script from another script using the 'source' command. So if the script which is sourced has any syntax error I want to display that in console. Is there any way to get that status ? In shell I executed the script with syntax error and I got the error like 'missing [' , but when I executed echo $? its returning 0 as the status, is this the behavior ? How can I get the status if the script has some syntax error or not ?
You can check the syntax of a shell script using the -n option prior to sourcing:
bash -n somescript # Works also for sh, ksh, zsh et al.
will tell you if somescript is syntactically okay without actually running it. In a program:
if bash -n somescript; then
. somescript
else
printf '%s\n' "Uh-oh, somescript is not syntactically correct." >&2
fi

Why do I get "unexpected token `;'?

I would like to run a few instances of my bash script foo.bash in background.
When I write for i in {1..10}; do ~/bin/foo.bash & ; done in the command line I get an error: bash: syntax error near unexpected token ;
Could you explain why this error occurs and how to fix the command?
& and ; are both command separators; you don't need (and can't have) both.
for i in {1..10}; do ~/bin/foo.bash & done

Resources