Bash Expr Index Syntax Error
Contents |
enter a title. You can not post a blank message. Please type your message and try again. This discussion is locked Andreas Haselbacher Level 1 (5 points) Q: bash: "expr index string1 string2" gives "syntax error" In expr syntax error length a bash script, I was trying to use the commandexpr index string1 string2to find solaris expr substr syntax error the position of string2 in string1, but this does not work on OS X while it works ok on several Linux mac bash expr machines. Reading the man page, I realized expr does something quite different on OS X than on Linux. Can anybody suggest which command I should use to get the same functionality of the above unix shell expr index on OS X?ThanksAndreas MacPro, Mac OS X (10.4.9), Server Posted on Apr 11, 2007 11:45 AM I have this question too by LittleSaint,Solvedanswer LittleSaint Level 4 (2,900 points) A: The expr in Linux is generally GNU expr. Mac OS X uses BSD expr which does not have the index command. You could install/compile GNU expr or use this:echo string1 | sed -n "s/[string2].*//p" | wc -cNote the double quotes
Expr Syntax Error Multiplication
in the sed statement otherwise string2 will not be expanded. Posted on Apr 11, 2007 2:06 PM See the answer in context Close Q: bash: "expr index string1 string2" gives "syntax error" All replies Helpful answers by LittleSaint,Solvedanswer LittleSaint Apr 11, 2007 2:06 PM in response to Andreas Haselbacher Level 4 (2,900 points) Apr 11, 2007 2:06 PM in response to Andreas Haselbacher The expr in Linux is generally GNU expr. Mac OS X uses BSD expr which does not have the index command. You could install/compile GNU expr or use this:echo string1 | sed -n "s/[string2].*//p" | wc -cNote the double quotes in the sed statement otherwise string2 will not be expanded. Helpful (0) Reply options Link to this post by Andreas Haselbacher, Andreas Haselbacher Apr 11, 2007 2:27 PM in response to LittleSaint Level 1 (5 points) Apr 11, 2007 2:27 PM in response to LittleSaint Thanks a lot - it does exactly what I need. Andreas Helpful (0) Reply options Link to this post by Gary Kerbaugh, Gary Kerbaugh Apr 11, 2007 6:58 PM in response to LittleSaint Level 6 (18,040 points) Apr 11, 2007 6:58 PM in response to LittleSaint Hi LittleSaint,That is so cool! and so ... UNIX. I would
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
Expr Syntax Error In Shell Script
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss bash expr index example 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 expr index mac takes a minute: Sign up Syntax error expr up vote 2 down vote favorite Why does the following returning a syntax error: stringZ=abcABC123ABCabc echo `expr match "$stringZ" 'abc[A-Z]*.2'` This works on my ubuntu machine but when I try it on https://discussions.apple.com/thread/923299?tstart=0 my mac running OS X 10.9.4 I get expr: syntax error? bash expr share|improve this question asked Aug 30 '14 at 23:00 rudolph9 4,00073163 Have you tried using echo $(expr match "$stringZ" 'abc[A-Z]*.2')? –Tom Fenech Aug 30 '14 at 23:04 @TomFenech same issue. –rudolph9 Aug 30 '14 at 23:07 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted This seems like a bash version difference. The : syntax works on my http://stackoverflow.com/questions/25587446/syntax-error-expr OSX 10.9.4 machine (which has bash 3.2.51, not very current): echo `expr "$stringZ" : 'abc[A-Z]*.2'` share|improve this answer answered Aug 30 '14 at 23:08 Wm Annis 1084 1 Online documentation: developer.apple.com/library/mac/documentation/Darwin/Reference/… –glenn jackman Aug 30 '14 at 23:11 add a comment| up vote 2 down vote expr is quite old-fashioned. On newer bash you may prefer to use the more modern regular expression syntax: re='abc[A-Z]*.2' [[ $stringZ =~ $re ]] && echo ${#BASH_REMATCH} The =~ operator is available since bash version 3.0. For maximum compatibility across older versions of bash, it is recommended to store the pattern to be matched in a separate variable and expand it without quotes. Successful matches are stored in the BASH_REMATCH array. If capturing groups are used, each group will be stored as a separate element in the array. share|improve this answer edited Aug 31 '14 at 8:29 answered Aug 30 '14 at 23:10 Tom Fenech 38.6k53063 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged bash expr or ask your own question. asked 2 years ago viewed 3058 times active 2 years ago Related 0Syntax error
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 http://serverfault.com/questions/501230/can-not-seem-to-get-expr-substr-to-work or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up Here's how it works: http://georgejhunt.com/olpc/pydebug/bash/expr.html Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Can not seem to get expr substr to work up vote 0 down vote favorite Can anyone tell me what I am syntax error doing wrong ? [ /jlr/sub/libexec/vmr-staging ] $ expr substr "onetwothree" 1 4 expr: syntax error I got the syntax from http://stackoverflow.com/a/220464/1150847 bash scripting shell-scripting share|improve this question asked Apr 20 '13 at 19:24 Jeffrey L. Roberts 1524 which OS du you use? substr is a non standard extention. The came be achieved with cut: echo "onetwothree" | cut -c 1-3 –kofemann Apr 20 '13 at 19:39 I am developing in OS X, Production is CentOS, cut worked! expr syntax error Please post it as an answer and I will check it off =] –Jeffrey L. Roberts Apr 20 '13 at 20:37 Id love to know why this was down voted –Jeffrey L. Roberts Apr 22 '13 at 17:43 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted The substr operation is a non standard extension and not supported on all OSes (probably linux only). The same be achieved with cut: echo "onetwothree" | cut -c 1-3 share|improve this answer edited Nov 25 '15 at 14:51 answered Apr 21 '13 at 7:02 kofemann 1,418817 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged bash scripting shell-scripting or ask your own question. asked 3 years ago viewed 649 times active 10 months ago Related 1Why is this bash script not working?2awk + perl + get two arguments in to awk syntax2Command does not execute in crontab while command itself works just fine-1shell: create Shortcut command (alias or function) for working with IP address as arguments3echo $? doesn't work when executed remotely1SSH issues: keyfile and host file not working as expected0Us
anything appearing in an operand position to an integer or a string depending on the operation being applied to it. Strings are not quoted for `expr' itself, though you may need to quote them to protect characters with special meaning to the shell, e.g., spaces. Operators may given as infix symbols or prefix keywords. Parentheses may be used for grouping in the usual manner (you must quote parentheses to avoid the shell evaluating them, however). String expressions ------------------ `expr' supports pattern matching and other string operators. These have lower precedence than both the numeric and relational operators (in the next sections). `STRING : REGEX' Perform pattern matching. The arguments are coerced to strings and the second is considered to be a (basic, a la GNU `grep') regular expression, with a `^' implicitly prepended. The first argument is then matched against this regular expression. If the match succeeds and REGEX uses `\(' and `\)', the `:' expression returns the part of STRING that matched the subexpression; otherwise, it returns the number of characters matched. If the match fails, the `:' operator returns the null string if `\(' and `\)' are used in REGEX, otherwise 0. Only the first `\( ... \)' pair is relevant to the return value; additional pairs are meaningful only for grouping the regular expression operators. In the regular expression, `\+', `\?', and `\|' are operators which respectively match one or more, zero or one, or separate alternatives. SunOS and other `expr''s treat these as regular characters. (POSIX allows either behavior.) `match STRING REGEX' An alternative way to do pattern matching. This is the same as `STRING : REGEX'. `substr STRING POSITION LENGTH' Returns the substring of STRING beginning at POSITION with length at most LENGTH. If either POSITION or LENGTH is negative, zero, or non-numeric, returns the null string. `index STRING CHARSET' Returns the first position in STRING where the first character in CHARSET was found. If no character in CHARSET is found in STRING, retu