Sponsored Content
Full Discussion: expr command help
Top Forums Shell Programming and Scripting expr command help Post 302523391 by Tewg on Wednesday 18th of May 2011 07:09:14 PM
Old 05-18-2011
expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is:
$expr "${var}" : . # expr STRING : regrep
where the "." is the grep wildcard for any single character.

Whats wrong with my code here and is there a better way to check if the input is 1 character?

also if i want to pass a variable into a arguement of another function like expr but the content of the variable might cause a syntax error if treat as other then a string, how do i escape ("\") the contents of the variable and not the variable call itself.

Last edited by Tewg; 05-18-2011 at 08:18 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

expr command

I am looking for the correct syntax on the expr command in UNIX. I have a script that I am building at the moment. the script is creating file1 that is an actual .sql file that is going inside the oracle database to get some information in there. It take that information, puts it inside another... (2 Replies)
Discussion started by: wolf
2 Replies

2. UNIX for Dummies Questions & Answers

Problems with expr command

Hi All, I might be making a silly mistake but I need ur help. I have initialized various variables: cur_month=`date +%m` cur_year=`date +%y` last_year=`expr $cur_year \- 1` It works fine for cur_month & cur_year....but has problems with last_year. FOR LAST YEAR IT GIVES A FOLLOWING... (2 Replies)
Discussion started by: rooh
2 Replies

3. Shell Programming and Scripting

about accecing `expr` command

Hi All, I have created 85 users on Linux server. I use bsh shell for all users.But after any user get logged in on unix,he/she is not able to access the expr command.So anyone can tell me that if i have to include some files to access expr command.Because when i write-> expr 6+2 on $ prompt i'll... (1 Reply)
Discussion started by: bhumi
1 Replies

4. UNIX for Dummies Questions & Answers

expr command

hi guys.... i hava a command expr... where i m adding a value in a loop like Tc=`expr $Tc\+ $l` where Tc is declred as a variable and every time l contains a new vaue if Tc =0 initially and l =2 Tc should be equal to 0+ 2 and then l = 4 Tc = 2+4 and dispaly as 6 but after... (5 Replies)
Discussion started by: madhu_aqua14
5 Replies

5. UNIX for Dummies Questions & Answers

problem with expr command

:) hi Unix gurus, Pls consider the following piece of code str='hello' length=echo $str|wc -c echo $length y= ` expr \( 80 - $length \) ` echo $y :confused: The last echo stmt is displaying 0 as the result. If i put direct value like 6 instead of $length in i 3rd stmt it is giving... (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

6. UNIX for Dummies Questions & Answers

using the expr command

Hi friends how can i execute expr $va1 * $var2 provided i m not supposed to use '/' also the nglob variable is turned off. (4 Replies)
Discussion started by: ashishj
4 Replies

7. Shell Programming and Scripting

expr command

Hi Can anyone explain me the usage of this command and the arguments used here and what will be the expected output : v_num=`expr nav_d_20100204_1759 : '*\(*\)'` what will be the value returned in v_num. Thanks in Advance!!! Regards Naveen Purbia (3 Replies)
Discussion started by: trying_myluck
3 Replies

8. Shell Programming and Scripting

help with expr command in script

Hi, I am trying to code a unix function to calculate date difference between two date variables. I am stuck at a point where I am trying to convert hours into minutes. Below is the code I am doing. function get_elapsed_time { export PROPS_FILE=temp.properties export... (8 Replies)
Discussion started by: Nutan
8 Replies

9. UNIX for Dummies Questions & Answers

substring without using expr command

Hi guys, For some reason the terminal on my mac does not let me run string manipulations commands using the expr command. I'm not sure how to fix this so I'm requesting a "work-around" to using the expr command... This is the string I'm working with: "neo_opls01_1.log" And I'm trying to... (9 Replies)
Discussion started by: ah7391
9 Replies

10. Homework & Coursework Questions

expr to translate the date command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. Write a script called "tod" that will display the time of day in am or pm notation rather then the 24 hour clock time. Use expr to convert from 24-hour clock time. Use... (13 Replies)
Discussion started by: linuxtraining
13 Replies
expr(1) 						      General Commands Manual							   expr(1)

NAME
expr - Evaluates arguments as expressions SYNOPSIS
expr expression STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: expr: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None OPERANDS
The single expression evaluated by expr will be formed from the operands, as described in the DESCRIPTION section. Each of the expression operator symbols: ( ) | & = > >= < <= != + - * / % : and the symbols integer and string in the DESCRIPTION sec- tion must be provided as separate arguments to expr. Expression to be evaluated, formed as explained in the DESCRIPTION section. DESCRIPTION
The expr command reads an expression, evaluates it, and writes the result to standard output. Within the expression argument, you must separate each term with spaces, precede characters special to the shell with a (backslash), and quote strings containing spaces or other special characters. Note that expr returns 0 to indicate a zero value, rather than the null string. Integers can be preceded by a unary minus sign. Internally, integers are treated as 64-bit or 32-bit two's complement numbers, depending on the word size of the hardware platform. The operators and keywords are described in the following listing. Characters that need to be escaped are preceded by a (backslash). The list is in order of increasing precedence with equal precedence operators grouped within {} (braces). Returns expression1 if it is neither null nor 0 (zero); otherwise, returns expression2. Returns expression1 if neither expression1 nor expression2 is null nor 0; oth- erwise, returns 0. Returns the result of an integer comparison if both expressions are integers; otherwise, returns the result of a string comparison. Adds or subtracts integer-valued arguments. Multiplies, divides, or provides the remainder from the division of integer-val- ued arguments. Compares expression1 with expression2, which must be a basic regular expression, with syntax as described for grep, except that all patterns are anchored, so ^ (circumflex) (which anchors a pattern to the beginning of a line) is not a special character in this context. Normally, the matching operator returns the number of characters matched. Alternatively, you can use the (...) symbols in expres- sion2 to return a portion of expression1. Provides expression grouping. An argument consisting only of an (optional) unary minus followed by digits. A string argument. Refer to the STRING OPERAND section. To avoid unpredictable results when using a range expression to match a class of characters, use a character class expression rather than a standard range expression. For information about character class expressions, see the discussion of this topic included in the description of the grep command. FUNCTIONS [Tru64 UNIX] The expr command provides the following string-expression functions. Strings containing white space must be quoted. A string argument should not be one of the expression operator symbols shown in the OPERANDS section or one of the string function names below. You cannot use, for example, match "$subject" "$pattern" unless it is confirmed that subject and pattern do not match this viola- tion. The first character of a string is position 1. The information returned by all these functions is in the form of a string: [Tru64 UNIX] Compares each character in the second string against each character in the first string and returns the position in the first string of the first match found, where the first match is the match closest to the beginning of string1. [Tru64 UNIX] Returns the length of the string argument. [Tru64 UNIX] See the description of the : (colon) match operator, above. [Tru64 UNIX] Returns the substring of string that begins at character position start and is length characters long. STRING OPERAND A string argument is an argument that cannot be identified as an integer argument or as one of the expression operator symbols shown in the OPERANDS section. The use of string arguments length, substr, index or match produces unspecified results. EXIT STATUS
The expr command returns the following exit values: The expression is neither null nor 0. The expression is null or 0. The expression is invalid. An error occurred. EXAMPLES
To increment a shell variable, enter: COUNT=`expr $COUNT + 1` This adds 1 to the COUNT shell variable (see sh for details). To find the length of a shell variable, enter: RES=`expr "$VAR" : ".*"` Note that VAR is in double quotes to avoid problems where VAR is NULL or contains embedded spaces. The regular expression is also quoted to avoid expansion by the shell. This operation can also be performed as: RES=`expr length "$VAR"` To use part of a shell variable, enter: RES=`expr "$VAR" : "-*(.*)"` This removes leading - (dashes), if any, from VAR. If the ( ) characters were omitted, RES would contain the length of VAR. To find a character from one string in another string, enter: INDEX=`expr index "Hello World!" "Wo"` Note that the returned value is 5, not 7. The first match is the fifth character in string1, matching the o in string2 rather than the W, which is the seventh character in string1. Special considerations: RES=`expr "x$VAR" : "x-*(.*)"` This succeeds even if VAR has the value - (dash). RES=`expr "x$VAR" = "x=" This succeeds even if VAR has the value = (equal sign). ENVIRONMENT VARIABLES
The following environment variables affect the execution of expr: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments) and the behavior of character classes in regular expressions.. Determines the locale for the format and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: grep(1), Bourne shell sh(1b), POSIX shell sh(1p), test(1) Functions: string(3), wcscat(3) Standards: standards(5) expr(1)
All times are GMT -4. The time now is 07:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy