Bash Syntax Error In Conditional Expression
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 in conditional expression unexpected token and policies of this site About Us Learn more about Stack Overflow syntax error in conditional expression unix the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags syntax error in conditional expression near then 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; it only syntax error in conditional expression syntax error near `]] takes a minute: Sign up Syntax error in [[ conditional expression with bash up vote 1 down vote favorite When I execute this regex='^[-a-z0-9]+$' string='abcd1--' if [[ $string =~ $regex ] -a ![ grep - "--" ]] then echo "valid" else echo "not valid" fi I get ~$ sh t.sh t.sh: line 3: syntax error in conditional expression t.sh: line
Bash Syntax Error In Expression (error Token Is
3: syntax error near `]' t.sh: line 3: `if [[ $string =~ $regex ] -a [ grep - "--" ]]' ~$ It is suppose to return not valid. Can someone figure out what's wrong? linux bash share|improve this question edited Feb 28 '12 at 14:48 Mat 135k20234273 asked Feb 28 '12 at 14:36 Sandra Schlichting 6,5561755109 2 Your syntax for [[ is incorrect. –Ignacio Vazquez-Abrams Feb 28 '12 at 14:39 1 You have to execute the script using bash, not sh (which usually is (d)ash). There is no =~ operator using the test utility (which is being used by (d)ash). –Gandaro Feb 28 '12 at 14:49 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted You're mixing [ and [[ syntax in a strange way. Try: if [[ ( $string =~ $regex ) && !( $string =~ "--" ) ]] and check bash's man page. share|improve this answer answered Feb 28 '12 at 14:45 Mat 135k20234273 note, /usr/bin/[ is not part of bash, and its special syntax is accessible w
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
Syntax Error Near Unexpected Token `fi'
about Stack Overflow the company Business Learn more about hiring developers or posting ads else if bash with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow bash check if variable is empty is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error in conditional expression in shell script up vote -1 down http://stackoverflow.com/questions/9483851/syntax-error-in-conditional-expression-with-bash vote favorite I write a shell script. I have a problem that I want to determine folder name manually. Actually, I solved with @Suhas's helping. When I execute my script, I have an error ./scriptname: line 5: syntax error in conditional expression ./scriptname: line 5: syntax error near `"${folder2}"' ./scriptname: line 5: ` if [[ ! -d "${folder1}" "${folder2}" ]]; then' Can someone figure out what's wrong? shell share|improve this http://stackoverflow.com/questions/19368095/syntax-error-in-conditional-expression-in-shell-script question edited Oct 15 '13 at 0:07 Wrikken 46.3k55298 asked Oct 14 '13 at 19:49 user2874061 18 Check them separately. –Wrikken Oct 14 '13 at 19:54 They can not separate. Because I need "-d folder1 folder2". Not "-d folder1" or "-d folder2" –user2874061 Oct 14 '13 at 20:00 && / logical AND seems to be what you want... –Wrikken Oct 14 '13 at 23:00 BTW, I rolled back the edit, as the question on its own after your edit would have to little information to go one, and the answer won't make sense without them. You could trim it down if you like, but please keep the information of the original in there for the ones finding this question & the answers later. –Wrikken Oct 14 '13 at 23:04 "./scriptname: line 5: syntax error in conditional expression" Yes, it abundantly clear. You're missing a closing quote ". That would also be a syntax error. –Wrikken Oct 15 '13 at 0:06 | show 4 more comments 2 Answers 2 active oldest votes up vote 1 down vote This: if [[ ! -d "${folder1}" "${folder2}" ]]; then needs to be separated into 2: if [ ! -d "${f
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/168511/syntax-error-in-conditional-expression 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://helios.cs.ifmo.ru/~ad/Documentation/Shells_by_Example/ch15lev1sec4.html 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 syntax error in conditional expression up vote 1 down vote favorite I am writing a script where I am using the combination of logical 'OR' and logical 'AND' statement. This is the script: #!/bin/bash echo "Enter the value of a" read $a echo "Enter the value of b" read $b if [[ $a != STARTED && syntax error in $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then echo "Either of the JVMs is not yet up, so lets wait for some more time" i=$(($i+1)) sleep 1s fi and getting the following error while executing it: line 13: syntax error in conditional expression line 13: syntax error near `]' line 13: `[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then' I am using bash shell. Any help on this is really appreciated. bash shell-script share|improve this question edited Nov 18 '14 at 8:53 Gilles 369k666681119 asked Nov 17 '14 at 19:05 Sudev Jash 2513 single brace is old style of shell programming and you have to use -o and -a for OR and AND. –PersianGulf Nov 18 '14 at 2:37 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted You have mismatched [[ with ]. [[ should always be closed with ]] and [ with ]. Use: if [[ $a != STARTED && $b == STARTED ]] || [[ $b != STARTED && $a == STARTED ]]; then Better yet, since you are using [[ anyway: if [[ ($a
this may seem more convenient than having to declare every variable explicitly, there is an unfortunate consequence: An inadvertent spelling error might introduce an extra variable that you had no intention of creating. And because UNIX/Linux is case sensitive, even changing from uppercase to lowercase can cause a program to fail. When setting a variable in the C/TC shells, the set command is used, and the = sign must be surrounded with space (or no space at all). The set command in bash, sh, and ksh is used to set shell options or create positional parameters, but is not used to define variables. Space is not allowed on either side of the = sign. When switching from one shell to another, it is easy to forget when to use set and when to use or not use spaces. The C and TC shells will tell you when you are using an undefined variable. The Bourne, Bash, and Korn shells display a blank line, unless you request error checking with the set –u or the shell's –u invocation option. The –u option flags any variables that have not been defined, called unbound variables. In the Bourne, Korn, and Bash shells, a variable is set as follows: x=5 name=John friend="John Doe" empty= or empty="" To check for undefined variables: set -u echo "Hi $firstname" (Output) ksh: firstname: parameter not set In the C and TC shells: set x = 5 set name = John set friend = "John Doe" set empty = "" The shell checks for undefined variables: echo "Hi $firstname" (Output) firstname: Undefined variable Example 15.7. #!/bin/tcsh 1 set friend1 = "George" set friend2 = "Jake" set friend4 = "Danny" 2 echo "Welcome $friend3 " (Output) 3 friend3: Undefined variable. EXPLANATION Three variables are set. When variables are so similar in thier names, it is easy to mistype one of them later in the program.The variable friend3 was never defined.The C and TC shells send an error to let you know that you have an undefined variable. The Bourne, Korn, and Bash shells leave a blank line. 15.4.2 Incomplete Programming Statements Missing Keywords When using a programming statement such as an if statement or while loop, you may accidentally omit part of the statement. For example, you may forget