Bash If 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 the company Business Learn more about hiring developers or posting ads with us Stack Overflow bash script syntax error Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Bash If Statement Command
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 syntax error near unexpected token fi' shell script 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 syntax error near unexpected token fi' in unix 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
Syntax Error Near Unexpected Token Fi' Cygwin
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
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 syntax error near unexpected token fi mac Learn more about Stack Overflow the company Business Learn more about hiring developers syntax error near unexpected token for or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Syntax Error Fi Unexpected (expecting Then )
Overflow Community Stack Overflow 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… then… syntax error up vote http://stackoverflow.com/questions/7124903/bash-if-statement-syntax-error 0 down vote favorite I am trying to remove something based on user input using bash/sh, here is my code : echo "remove ? [Y/n]" read REMOVE if [ $REMOVE != "n" ] || [ $REMOVE !="N" ];then # ... do something ... echo "done" fi the error I am getting is something like: ./run.sh: line 8: syntax error near unexpected symbol « then » ./run.sh: line 8: http://stackoverflow.com/questions/30412511/bash-if-then-syntax-error `if [ $REMOVE != "n" ] || [ $REMOVE !="N" ];then' I tried to add/remove spaces many times, and I still don't understand what happens. I also don't really understand all the differences between [[ statement ]] [statement] or ((statement)). If someone can help... bash shell if-statement share|improve this question asked May 23 '15 at 12:11 n00dl3 1,8531519 Related: stackoverflow.com/questions/19691082/… –fvu May 23 '15 at 12:24 @fvu $SHELL is zsh but the script is actually running in bash –n00dl3 May 23 '15 at 12:31 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote accepted Please try this: echo "remove ? [Y/n]" read REMOVE if [ $REMOVE != "n" ] && [ $REMOVE != "N" ] then # ... do something ... echo "done" fi share|improve this answer edited May 23 '15 at 12:26 answered May 23 '15 at 12:14 shruti1810 2,3231725 ./run.sh: line 8 : [: n : expected unary operator (maybe it's not the english output, I translated it myself) –n00dl3 May 23 '15 at 12:18 As far as the logic which you are trying to achieve, I feel you should be using &&
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 http://unix.stackexchange.com/questions/87776/shell-script-syntax-error-near-unexpected-token-else Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is http://www.linuxquestions.org/questions/programming-9/bash-syntax-error-near-unexpected-token-%60else'-806909/ 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 syntax error can answer The best answers are voted up and rise to the top Shell Script - syntax error near unexpected token `else' up vote 4 down vote favorite With the following shell script, why I am getting errors syntax error near unexpected token `else' Shell Script echo "please enter username" read user_name echo "please enter password" read -s pass echo ${ORACLE_SID} SID=${ORACLE_SID} if ["${ORACLE_SID}" != 'Test'] then sqlplus -s syntax error near -l $USER_NAME/$PASS@$SID < Search Tutorials/Articles Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming [SOLVED] bash: syntax error near unexpected token `else' User Name Remember Me? Password Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 05-10-2010, 03:32 AM #1 lucmove Member Registered: Aug 2005 Location: Brazil Distribution: Debian Jessie Posts: 711 Rep: bash: syntax error near unexpected token `else' Code: for i in $(ls -1a); do if [ "$i" == '.' ] then echo 'one dot' elif [ "$i" == '..' ] then echo 'two dots' else echo 'yay' fi; done bash: syntax error near unexpected token `else' Why?!! Yes, I googled. I also searched this forum. Still stumped. How is my code any different from these examples or these examples? lucmove