Bash Catch Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow bash syntax error near unexpected token done' the company Business Learn more about hiring developers or posting ads with us Stack
Bash Syntax Error Near Unexpected Token Else'
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of bash syntax error near unexpected token fi' 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Trap syntax issue in bash up vote 1 down vote favorite I intend to use trap to execute
Bash Syntax Error Invalid Arithmetic Operator
some clean up code in case of a failure. I have the following code, but it seems to be have some syntactical issues. #!/bin/bash set -e function handle_error { umount /mnt/chroot losetup -d $LOOP_DEV1 $LOOP_DEV2 } trap "{ echo \"$BASH_COMMAND failed with status code $?\"; handle_error; }" ERR Does any one see an issue with the way the trap has been written. In case of an error the trap does get executed bash syntax error near unexpected token then' fine but it also throws another unwanted error message below. /root/myscript.sh: line 60: } ERR with status code 0: command not found ##line 60 is that line of code that exited with a non zero status How do I write it correctly to avoid the error message? Also what if I had to send arguments $LOOP_DEV1 and $LOOP_DEV2 from the main script to the trap and then to the handle_error function? Right now they are exported as environment variables in the main script. I did some search for trap examples but I couldn't get something similar. EDIT I changed the shebang from /bin/sh to /bin/bash. As /bin/sh was already symlinked to bash I did not expect unicorns nor did I see any. linux bash shell syntax trap share|improve this question edited Jul 13 '13 at 16:51 asked Jul 13 '13 at 15:16 The Governor 608614 If you script is actually executed with sh and not bash, you need to define the function with handle_error () {. The function keyword is a bash extension. –chepner Jul 13 '13 at 15:49 @chepner Isn't /bin/sh a sym link to bash in most cases? At least that seems to be the case in my system. –The Governor Jul 13 '13 at 16:33 2 I
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
Bash Syntax Error Near Unexpected Token Do'
the workings and policies of this site About Us Learn more about
Bash Syntax Error Near Unexpected Token Newline'
Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux bash syntax error unexpected end of file Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join http://stackoverflow.com/questions/17631485/trap-syntax-issue-in-bash them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to make bash abort the execution of a script on syntax error? up vote 14 down vote favorite 5 To be on safe side, I'd like bash abort the http://unix.stackexchange.com/questions/82224/how-to-make-bash-abort-the-execution-of-a-script-on-syntax-error execution of a script if it encounters a syntax error. To my surprise, I can't achieve this. (set -e is not enough.) Example: #!/bin/bash # Do exit on any error: set -e readonly a=(1 2) # A syntax error is here: if (( "${a[#]}" == 2 )); then echo ok else echo not ok fi echo status $? echo 'Bad: has not aborted execution on syntax error!' Result (bash-3.2.39 or bash-3.2.51): $ ./sh-on-syntax-err ./sh-on-syntax-err: line 10: #: syntax error: operand expected (error token is "#") status 1 Bad: has not aborted execution on syntax error! $ Well, we can't check $? after every statement to catch syntax errors. (I expected such safe behavior from a sensible programming language... perhaps this must be reported as a bug/wish to bash developers) More experiments if makes no difference. Removing if: #!/bin/bash set -e # exit on any error readonly a=(1 2) # A syntax error is here: (( "${a[#]}" == 2 )) echo status $? echo 'Bad: has not aborted execution on syntax error!' Result: $ ./sh-on-syntax-err ./sh-on-syntax-err: line 6: #: synta
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting http://unix.stackexchange.com/questions/39623/trap-err-and-echoing-the-error-line ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Trap, ERR, and echoing the error line up vote 11 down vote favorite 2 I'm trying syntax error to create some error reporting using a Trap to call a function on all errors: Trap "_func" ERR Is it possible to get what line the ERR signal was sent from? The shell is bash. If I do that, I can read and report what command was used and log/perform some actions. Or maybe I'm going at this all wrong? I tested with the following: #!/bin/bash trap "ECHO $LINENO" ERR echo hello | grep "asdf" And $LINENO is returning 2. Not working. bash shell-script bash syntax error error-handling trap share|improve this question edited May 29 '12 at 23:55 Gilles 369k666681119 asked May 29 '12 at 18:23 Mechaflash 4122814 You can look at the bash debugger script bashdb. It seems that the first argument to trap can contain variables that are evaluated in the desired context. So trap 'echo $LINENO' ERR' should work. –donothingsuccessfully May 29 '12 at 18:53 hmm just tried this with a bad echo | grep command and it returns the line of the Trap statement. But I'll take a look at bashdb –Mechaflash May 29 '12 at 18:56 I'm so sorry... I didn't specify in my original question that I need a native solution. I edited the question. –Mechaflash May 29 '12 at 19:05 Sorry, I borked the example line: trap 'echo $LINENO' ERR. The first argument to trap is the entire echo $LINENO hardquoted. This is in bash. –donothingsuccessfully May 29 '12 at 19:43 4 @Mechaflash It would have to be trap 'echo $LINENO' ERR, with single quotes, not double quotes. With the command you wrote, $LINENO is expanded when line 2 is parsed, so the trap is echo 2 (or rather ECHO 2, which would output bash: ECHO: command not found). –Gilles May 29 '12 at 23:56 | show 1 more comment 1 Answer 1 active oldest votes up vote 22 down vote accepted As pointed out in comments, your quoting is wrong. You need single quotes to prevent $LINENO from