Passing parameters to a bash function in bashrc - bash

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?

Related

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

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 >>)

Bash: Get message from error that was just trapped

Bash allows you to trap signals. Is there a way to actually get the message that printed immediately before (the cause of) a particular signal? It's ERR in particular I'm interested in. I'm aware that not all signals are associated with a message. Just wondering if bash itself sets a variable or something when it raises an error.
Sample code:
#!/bin/bash
# Running bash 5.0
handler () {
echo "Handling the error"
exit 0
}
trap handler ERR
notacommand
The code above will print:
./example.sh: line 11: notacommand: command not found
Is there a way to see this message inside the handler?
Edit: I know I could save all output to a file and then read the tail of that file when an error occurs. That seems problematic to me, as it's possible that the last message written to the file is something other than the error (especially if any subprocesses are started with & in the script). I was hoping that maybe bash sets a var or something, in the same way it sets $1, $?, $RANDOM, and others.
Redirect bash's stderr to a file:
#!/bin/bash
# Running bash 5.0
log="error.txt"
exec 2>"$log"
handler () {
echo "Handling this error: $(< "$log")"
exit 0
}
trap "handler" ERR
trap "rm $log" EXIT
notacommand
Output:
Handling this error: ./example.sh: line 15: notacommand: command not found

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

Shellscript throws errors but somehow is still working

I have this shellscrip to deploy code from travis on another machine.
#!/bin/sh
codecov
function sshDeploy {
printf -v __ %q "$1"
ssh -oStrictHostKeyChecking=no deployuser#178.62.252.23 "cd api; git pull origin master; git checkout $__;./sbt clean; ./sbt stage; ./neeedo restart; exit $?"
}
sshDeploy $TRAVIS_COMMIT
exit $?
This script runs without any errors on my macbook. However when I run it on travis(unfortunately I can't tell you which unix they run on their buildagents because I dont have direct access to them) I observe strange behaviour.
Errors are thrown but the script is somehow still being executed.
./after-success.sh: 3: ./after-success.sh: function: not found
./after-success.sh: 4: printf: Illegal option -v
Warning: Permanently added '178.62.252.23' (ECDSA) to the list of known hosts.
From https://github.com/HTW-Projekt-2014-Commercetools/api
* branch master -> FETCH_HEAD
... Git Log ...
Stopping Neeedo-API: ..done.
Starting Neeedo-API: ....done.
./after-success.sh: 6: ./after-success.sh: Syntax error: "}" unexpected
Now I have 2 problems/question:
Why are these errors thrown and how can I make this shellscript as plattform independent as possible? I dont know which buildagent is running my script so it should run on the common unix systems.
How can I make sure that the script returns an errorcode different than 0 if something fails to make sure that the travis build fails. At the moment its still returning a green build and also the app is started somehow as you can see in the logs...
(3.) Is there a way to format the commands that are sent through ssh in a better way than in a long string?
Change it to a bash script.
First line: #!/bin/bash.
sh
$ printf -v ___ %q "$1"
sh: 1: printf: Illegal option -v
bash
$ printf -v ___ %q "$1"
<no error>
The errors are thrown because the function keyword and the -v argument to printf aren't being understood by /bin/sh on the remote host.
Changing the shebang line to #!/bin/bash will fix those errors.
The reason the script still "works" even with the above errors (quotes because it isn't really working I don't think) is because you got lucky. /bin/sh didn't understand the function keyword so it ignored that line entirely which then meant that it likely ran the ssh command directly (not through the function call) and didn't use $__ correctly (because of the printf -v error) so didn't run the correct git checkout command.
I would have expected to see an error about not knowing what the sshDeploy command was but maybe it didn't do that or maybe it got hidden by something else.
That all being said there's no real reason for that function at all.
The exit status from a script is the exit status of the last command that ran so there's no need to end with exit $?.
With that and without the function your script just becomes.
codecov
printf -v __ %q "$TRAVIS_COMMIT"
ssh -oStrictHostKeyChecking=no deployuser#178.62.252.23 "cd api; git pull origin master; git checkout $__;./sbt clean; ./sbt stage; ./neeedo restart"
And you can even avoid the printf -v most likely since git refs are unlikely (if not incapable) of containing any shell metacharacters that would need escaping when expanded in a shell string like that (I believe at least). Though this is certainly safer if you don't mind the bash requirement.

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

Resources