Bash Script Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta bash end of file Discuss the workings and policies of this site About Us Learn
Shell Script Syntax Error ( Unexpected
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix expr syntax error in shell script & 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
How To Check Syntax Error In Shell Script
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 Shell script fails: Syntax error: “(” unexpected up vote 31 down vote favorite 7 I've been working on a script that automates setting up a syntax error ( unexpected bash array development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but convenience you can find it here also. Now when run this script install and sets up the environment without error but you have to enter your sudo password more than once due to sudo's time-out value by default. So I started experimenting by removing all the sudo lines and running the whole script via sudo at the command line like so: kemra102@ubuntuvm:~$ sudo ./pi_dev_env_install.sh This works fine as expected and gets most of the way through until this point: ./pi_dev_env_install: 68: ./pi_dev_env_install.sh: Syntax error: "(" unexpected Now this line worked fine previously when not running the whole script with sudo. There is nothing about this line running as sudo that should stop it working to my knowledge, does anyone have any ideas? bash shell ubuntu shell-script share|improve this question edited Aug 18 '12 at 12:08 Gilles 369k666681119 asked Aug 18 '12 at 11:37 kemra102 4681613 The shebang is really i
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 ads with us Stack Overflow
Ksh Syntax Error Unexpected
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Syntax Error ( Unexpected Ubuntu
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Bash if statement syntax line 1: syntax error: unexpected word (expecting ")") error [duplicate] up vote 1 down vote favorite This question already has an answer here: How do I compare two string variables in an 'if' statement in Bash? 11 answers I'm at a loss as to why this is giving a syntax error. Any http://unix.stackexchange.com/questions/45781/shell-script-fails-syntax-error-unexpected thoughts? #!/bin/bash if [ `date +%H` -lt 11 ] ; then exit 0; fi if [ `date +%H` -gt 14 ] ; then if[ `date +%H` -lt 20 ] ; then # <--- this line is the culprit, it seems exit 0; fi fi When run, I get: ./get.sh: line 7: syntax error near unexpected token `then' ./get.sh: line 7: ` if[ `date +%H` -lt 20 ] ; then ' bash share|improve this question asked Aug 19 '11 at 16:57 CoolUserName 1,28041224 marked as duplicate by tripleeebash http://stackoverflow.com/questions/7124903/bash-if-statement-syntax-error Users with the bash badge can single-handedly close bash questions as duplicates and reopen them as needed. Jan 7 at 10:25 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 1 editor with syntax highlight can help you in similar situations. My gedit don't highlight second if in your code –RiaD Aug 19 '11 at 17:01 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted The reason that this is a syntax error is that [ isn't part of the shell syntax; it's actually a command. Originally it was just a symlink to the test command. It still is, but it's also a built-in command in bash and other Bourne-derived shells. if is a shell keyword, but the shell sees if[, not if. Because it didn't see an if, it doesn't know what to do when it sees then. (Actually, it knows exactly what to do: print a syntax error message.) ... A bit of experimentation shows that it's not quite as simple as I thought it was. I tried creating a command called if[ and putting it in a directory in my $PATH. When I type just if[ at the prompt, the shell asks for more input. I actually don't know what it's looking for, but apparently the [ character is specially treated by the shell. The shell just doesn't split if[ into the if keyword and the [ command (as you might reasonably expe
Out Of Trouble by William Shotts, Jr. Now that our scripts are getting a little more complicated, I want to point out some common mistakes that you might run into. To do this, create the following script called trouble.bash. Be sure to enter it exactly as written. #!/bin/bash number=1 if http://linuxcommand.org/wss0100.php [ $number = "1" ]; then echo "Number equals 1" else echo "Number does not equal 1" fi When you run this script, it should output the line "Number equals 1" because, well, number equals 1. If you don't get the http://wiki.bash-hackers.org/scripting/debuggingtips expected output, check your typing; you made a mistake. Empty variables Edit the script to change line 3 from: number=1 to: number= and run the script again. This time you should get the following: [me@linuxbox me]$ ./trouble.bash /trouble.bash: [: =: unary syntax error operator expected. Number does not equal 1 As you can see, bash displayed an error message when we ran the script. You probably think that by removing the "1" on line 3 it created a syntax error on line 3, but it didn't. Let's look at the error message again: ./trouble.bash: [: =: unary operator expected We can see that ./trouble.bash is reporting the error and the error has to do with "[". Remember that "[" is an abbreviation for the test shell syntax error ( builtin. From this we can determine that the error is occurring on line 5 not line 3. First, let me say there is nothing wrong with line 3. number= is perfectly good syntax. You will sometimes want to set a variable's value to nothing. You can confirm the validity of this by trying it on the command line: [me@linuxbox me]$ number= [me@linuxbox me]$ See, no error message. So what's wrong with line 5? It worked before. To understand this error, we have to see what the shell sees. Remember that the shell spends a lot of its life substituting text. In line 5, the shell substitutes the value of number where it sees $number. In our first try (when number=1), the shell substituted 1 for $number like so: if [ 1 = "1" ]; then However, when we set number to nothing (number=), the shell saw this after the substitution: if [ = "1" ]; then which is an error. It also explains the rest of the error message we received. The "=" is a binary operator; that is, it expects two items to operate upon - one on each side. What the shell was trying to tell us was that there was only one item and there should have been a unary operator (like "!") that only operates on a single item. To fix this problem, change line 5 to read: if [ "$number" = "1" ]; then Now when the shell performs t
of Contents Debugging a script Use a unique name for your script Read the error messages Use a good editor Write logfiles Inject debugging code Use shell debug output Simple example of how to interpret xtrace output Making xtrace more useful Debugging commands depending on a set variable Dry-run STDIN driven commands Common error messages Unexpected end of file Unexpected end of file while looking for matching ... Too many arguments !": event not found syntax error near unexpected token `(' The CRLF issue What is the CRLF issue? How did a CR end up in my file? Why do CRs hurt? How can I find and eliminate them? See also Discussion Debugging a script These few lines are not intended as a full-fledged debugging tutorial, but as hints and comments about debugging a Bash script. Use a unique name for your script Do not name your script test, for example! Why? test is the name of a UNIX®-command, and most likely built into your shell (it's a built-in in Bash) - so you won't be able to run a script with the name test in a normal way. Don't laugh! This is a classic mistake Read the error messages Many people come into IRC and ask something like "Why does my script fail? I get an error!". And when you ask them what the error message is, they don't even know. Beautiful. Reading and interpreting error messages is 50% of your job as debugger! Error messages actually mean something. At the very least, they can give you hints as to where to start debugging. READ YOUR ERROR MESSAGES! You may ask yourself why is this mentioned as debugging tip? Well, you would be surprised how many shell users ignore the text of error messages! When I find some time, I'll paste 2 or 3 IRC log-snips here, just to show you that annoying fact. Use a good editor Your choice of editor is a matter of personal pr