Bash Syntax Error End Of File Unexpected
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings syntax error unexpected end of file in shell script and policies of this site About Us Learn more about Stack bash if Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs bash else if Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; bash syntax error near unexpected token it only takes a minute: Sign up Shell Script Syntax Error: Unexpected End of File up vote 8 down vote favorite 2 In the following script I get an error: syntax error: unexpected end of file What is this error how can I resove it? It is pointing at the line whee the function is called. #!/bin/sh expected_diskusage="264" expected_dbconn="25" expected_httpdconn="20"
Bash Syntax Error Near Unexpected Token Newline
expected_cpuusage="95" #expected_fd="100" httpdconn=`ps -ef|grep -i httpd|grep -v grep|wc -l` #httpd connections cpu_usage=`ps aux|awk 'NR > 0 { s +=$3 }; END {print s}'` disk_usage=`df -h|awk {'print $2'}|head -n3|awk 'NF{s=$0}END{print s}'` #db_connections=`mysql -uroot -pexxxxxx -s -N -e "show processlist"|wc -l` db_connections=6 cld_alert() { nwconn=$1 cpu_usage=$2 disk_usage=$3 db_connections=$4 message=$5 `touch /tmp/alert.txt && > /tmp/alert.txt` date=`date` echo -e "$date\n" > /tmp/alert.txt echo -e "$message" >> /tmp/alert.txt path="/proc/$httpd/fd/"; cd $path tfd=`ls -l|wc -l`; sfd=`ls -ltr|grep sock|wc -l`; echo "Total fds: $tfd" >> /tmp/alert.txt echo "Socket fds: $sfd" >> /tmp/alert.txt echo "Other fds: $[$tfd - $sfd]" >> /tmp/alert.txt freememory=`vmstat | awk '{if (NR == 3) print "Free Memory:"\$4}'`; echo "Free memory :$freememory" >> /tmp/alert.txt Bufferedmemory=`vmstat | awk '{if (NR == 3) print "Buffered Memory:"\$5}'`; echo "Buffered memory $Bufferedmemory" >> /tmp/alert.txt CacheMemory=`vmstat | awk '{if (NR == 3) print "Cache Memory:"\$6}'`; echo "Cache memory : $CacheMemory" >> /tmp/alert.txt sshconn=`netstat -an|grep 22|wc -l` #ssh connections httpsconn=`netstat -an|grep 443|wc -l` #https connections wwwconn=`netstat -an|grep 80|wc -l` #www connections echo "Disk usage is $disk_usage" >> /tmp/alert.txt echo "DB connections $db_connections" >> /tmp/alert.txt echo "Network connections $nw
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
Bash Syntax Error Redirection Unexpected
the following script called trouble.bash. Be sure to enter it exactly as written. syntax error end of file unexpected (expecting fi ) #!/bin/bash number=1 if [ $number = "1" ]; then echo "Number equals 1" else echo "Number does not equal syntax error end of file unexpected (expecting done ) 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 expected output, check your typing; you made a http://stackoverflow.com/questions/9886268/shell-script-syntax-error-unexpected-end-of-file 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 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 http://linuxcommand.org/wss0100.php 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 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 s
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 http://unix.stackexchange.com/questions/155166/got-syntax-error-near-unexpected-end-of-file-in-bash-script 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 Got “syntax error near unexpected end of file” in bash script up vote 0 down vote favorite 2 I'm making syntax error 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 si coloca bien $mes if [ "$mes" -lt 12 -a "$mes" -gt 0 ]; then echo "muy bien, sigamos." else if [ "$mes" -gt 12 bash syntax error -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:34 answered Sep 12 '14 at 4:03 cuonglm 71.1k13111196 Thanks! That worked. –Divshah Sep 12 '14 at 4:23 Now I have another issue, it doesn't display the second if part... I mean, If I put the number 13 it just ends, it doesn't say "Creo que esto