Bash If Statement Syntax Error Unexpected End Of File
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 the company syntax error unexpected end of file bash script Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Syntax Error Unexpected End Of File Shell Script
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers,
Syntax Error, Unexpected End Of File Php
just like you, helping each other. Join them; it only takes a minute: Sign up in bash script unexpected “syntax error: unexpected end of file” on if statement up vote 4 down vote favorite W/hen i run
Bash Unexpected End Of File While Looking For Matching
the following code snippet #!/bin/bash if [ "foo" = "foo" ]; then echo "true" else echo "false" fi echo "end" i get sfm_write_buffer_test.sh: line 9: syntax error: unexpected end of file this doesn't make any sense. echo statements works fine, but when the if statement is encountered it gives the above mentioned error. linux bash if-statement syntax-error unexpectendoffile share|improve this question edited Aug 29 '11 at 23:47 Keith Thompson 145k19190339 asked Aug 29 '11 syntax error unexpected end of input at 23:23 bicepjai 398417 4 Try removing the trailing semicolon on the if statement –Rafe Kettler Aug 29 '11 at 23:24 1 I just copy-and-pasted the above into a script on my system and ran it. The output was two lines: true and end. Same result with bash versions 2.01.1, 3.2.25, and 4.2.8. –Keith Thompson Aug 29 '11 at 23:25 add a comment| 2 Answers 2 active oldest votes up vote 8 down vote accepted You're on Cygwin, right? As I said in a comment, when I copy-and-paste your script and run it on my system, it works; the output is true end But when I change the line endings from the Unix style '\n' to the Windows style '\r\n', I get the same error you got. With the Windows-style line endings, bash doesn't see the then keyword; it sees a command named then\r. It never tries to execute it because it's scanning for the matching then or fi for the if keyword (which it recognized because it's not at the end of the line). Make sure your shell scripts use Unix-style line endings. share|improve this answer answered Aug 29 '11 at 23:33 Keith Thompson 145k19190339 add a comment| up vote 1 down vote The problem is the CRLF at the end of the script. Shell scripts t
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 [ $number integer expression expected = "1" ]; then echo "Number equals 1" else echo "Number does not equal 1" fi When bash syntax checker you run this script, it should output the line "Number equals 1" because, well, number equals 1. If you don't get the expected output, check your syntax error near unexpected token `fi' 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 operator expected. Number does not http://stackoverflow.com/questions/7237118/in-bash-script-unexpected-syntax-error-unexpected-end-of-file-on-if-statement 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 builtin. From this we can determine http://linuxcommand.org/wss0100.php 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 the substitution it will see: if [ "" = "1" ]; then which
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 http://unix.stackexchange.com/questions/155166/got-syntax-error-near-unexpected-end-of-file-in-bash-script Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags http://askubuntu.com/questions/145900/syntax-error-unexpected-end-of-file-simple-script 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 Got “syntax error syntax error near unexpected end of file” in bash script up vote 0 down vote favorite 2 I'm making an script that gives me a day like this jjj/yyyy when I give it a day like this dd/mm/yyyy and I need it to have an error when you don't write correctly but I'm starting and it's not working :( #! /bin/bash #Primero debes ingresar el mes echo "Ingresa el número de un mes del año" read mes #Condicional #Dependiendo unexpected end of si coloca bien $mes if [ "$mes" -lt 12 -a "$mes" -gt 0 ]; then echo "muy bien, sigamos." else if [ "$mes" -gt 12 -a "$mes" -lt 0 ]; then echo "Creo que eso ya no es un mes!"; exit fi When I run it it says "syntax error near unexpected end of file" Can anyone help me? bash shell scripting share|improve this question edited Sep 12 '14 at 3:59 cuonglm 71.1k13111196 asked Sep 12 '14 at 3:59 Divshah 1113 Yes, someone can help you. –Anthon Sep 12 '14 at 4:05 Your code doesn't make sense. A number cannot be at the same time less than 0 and greater than 12, so your second condition can never be true. Also note that -lt and -gt are for strictly greater. -lt 12 and -gt 0 means from 1 to 11. Use -le for less than or equal. –Stéphane Chazelas Jul 28 at 7:33 add a comment| 2 Answers 2 active oldest votes up vote 8 down vote You missed a fi for inner if: if [ "$mes" -lt 12 -a "$mes" -gt 0 ]; then echo "muy bien, sigamos." else if [ "$mes" -gt 12 -a "$mes" -lt 0 ]; then echo "Creo que eso ya no es un mes!" # Missed fi here fi exit fi share|improve this answer edited Dec 16 '14 at 10:
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 “syntax error: unexpected end of file” simple script up vote 1 down vote favorite i need some help for a very siple script, i don't get were the fault is. The script : #!/bin/bash declare -i s declare -i m declare -i h if [ "$3" < 50 ]; then s=$3+10 m=$2 h=$1 else if ["$2" < 50 ]; then s=$3-50 m=$2+1 h=$1 else s=$1-50 m=$2-50 h=$1+1 fi fi echo "$h:$m:$s" The script add 10sc to the time we've entered. I get this error message : "sub_change_dirrect: line 14: syntax error: unexpected end of file" bash scripts share|improve this question edited Jun 3 '12 at 12:54 adempewolff 5,60011943 asked Jun 3 '12 at 12:41 Sam 4444915 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted There are three obvious errors: On the line else s=$1-50 m=$2-50 h=$1+1 fi, the word fi is not treated as a keyword, because it is not the first word in the command. To the shell, this looks like three assignments that apply to the command fi. If you ever got to execute this line, you'd see an error bash: fi: command not found. Put fi on a line of its own (or put a ; before it). [ "$3" < 50 ] is the same as [ "$3" ] < 50 — it's the command [ … ] (which can also be written test) with the sole argument "$3", and with an input redirection from the file 50. Either use the numeric comparison operator -lt, or use an arithmetic instruction (( … )). The singl