$ expr "7 > 5" 7 > 5
$ expr "7 > 5" 7 > 5
$ expr 7 \> 5 1
$ expr 2 + 3 5
$ expr 2 + 3.1 expr: non-numeric argument
$ [ `expr 7 \> 5 ` ] || echo aaa
$ [ `expr 7 \> 5 ` ] && echo aaa aaa
$ if [ `expr 7 \> 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi aaa
$ if [ `expr 7 \< 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi bbb
$ if [ `expr 7 \> 5 ` ]; then echo aaa; else echo bbb; fi aaa
$ if [ `expr 7 \< 5 ` ]; then echo aaa; else echo bbb; fi aaa
$ expr \( 5 + 10 \) / 2 7
$ vv=1 $ vv=`expr $vv + 1` $ svs vv vv=2
$ expr `echo 2` '>' 1; echo $? 1 0
$ expr `echo 1` '>' 1; echo $? 0 1
$ expr `echo ' 2'` '>' 1; echo $? 1 0
— space is not a problem
$ a='aaa' $ expr "$a" = aaa 1 $ expr "$a" = aab 0
$ b=5 $ expr "$b" + 5 \>= 10 1 $ expr "$b" + 5 \>= 11 0
$ p="version.100" $ expr "$p" : '.*' 11 $ [ `expr "$p" : '.*'` -gt 0 ] && echo yes yes $ expr "$p" : '\(.*\)' version.100
$ expr "$p" : '[a-z]*' 7 $expr "$p" : '\([a-z]*\)' version
# return no more than 4 char... $ x='12345' $expr "$x" : '\(....\)' \| "$x" 1234 $ x='123' $ expr "$x" : '\(....\)' \| "$x" 123 $ expr "$x" : '\(....\)'
$ expr "-b4" : '-b\([0-9]*\)' \| 2 4
but '+' nok:
$ expr "-b4" : '-b\([0-9]+\)' \| 2 2
$
$ a=/opt/SUNWconn/chorus_4.0LA/chorus-powerpc/os/root $ expr $a : '.*/\(.*\)' '|' $a root
$ v="version1234." $ expr $v : '.*\([0-9]+\).*'
$ expr $v : '.*\([0-9][0-9]*\).*' 4 $ expr $v : '.*n\([0-9]+\).*'
$ expr $v : '.*\(n[0-9]+\).*'
$ expr $v : '.*\(n[0-9][0-9]*\).*' n1234 $ expr $v : '.*n\([0-9][0-9]*\).*' 1234
: Perform pattern matching. Its arguments are coerced to strings and the second one is considered to be a regular expression, with a `^' implicitly added at the beginning. The first argument is then matched against this regular expression. If the match succeeds and part of the string is enclosed in `\(' and `\)', that part is the value of the : expression; otherwise an integer whose value is the number of characters matched is returned. If the match fails, the : operator returns the null string if `\(' and `\)' are used, otherwise 0.
Only one `\(' and `\)' pair can be used.
The following may be used to print the non-directory part of the file name stored in variable a ( if the value does not contain `/', use $a):
expr $a : '.*/\(.*\)' '|' $a
Note the quoted shell metacharacters.
expr returns the following exit status:
0 if the expression is neither null nor 0, 1 if the expression is null or 0, 2 for invalid expressions.
$ tty /dev/pts/0
$ expr `tty` : '/dev/pts/'; echo $? 9 0
$ expr `tty` : '/dev/tty'; echo $? 0 1
$ TTY=`tty` $ if `expr $TTY : '/dev/tty' > /dev/null` ; then echo from tty; else echo not form tty; fi not form tty
$ TTY='/dev/tty1' $ if `expr $TTY : '/dev/tty' > /dev/null` ; then echo from tty; else echo not form tty; fi from tty
documented on: Sun 03-28-99 09:47:32
$ expr substr "asdfsadf" 1 5 $ expr length asfasdf
Extract the substring of string starting at position i_start and of length i_length characters. If i_start has a value greater than the length of string, expr returns a null string. If you try to extract more characters than there are in string, expr returns all the remaining characters from string. Beware of using negative values for either i_start or i_length as expr tends to run forever in these cases.
Report the first position in string at which any one of the characters in character-list matches a character in string.
Return the length (that is, the number of characters) of string.
$ expr sbustr 'asdfaffasdfasdfsaf' 1 5 expr: syntax error
$ expr substr asfasdf 1 3 asf
— '' don't work, without it work
$ expr substr ############ 1 3 expr: syntax error
$ expr substr "############" 1 3 ###
— without quote wont't work, with "" works
$ expr substr "############" 1 30 ############
— doesn't matter over-want
$ expr substr "asdfsadf" 1 5 asdfs
— with "", always works
$ expr length asfasdf 7
— and you can get lenght too.
documented on: Mon 07-12-99 13:26:07
>How can I use expr to pick out the filename (quoted in "'s) in the >following line (should be one line). > >03-Sep.15:47 tongsun checkout version "/clearlib/.../component.options" from /main/7 (reserved) > >I used > xargs -i expr {} : '"\(.*\)"' >But it doesn't work.
expr expressions are implicitly anchored on both ends, so you need to make that:
xargs -i expr {} : '.*"\(.*\)".*'
>Similar things, I want to use expr to pick out the email address from >the following addresses. And I didn't make it working... > >Some other names <name2@...>
expr "$line" : '.*<\([^>]*\).*'
Ken Pizzini
Why do you need to use expr? Try:
<whatever_you_did>|cut -d'"' -f2
Claus.
documented on: 1999.09.24 Fri 17:56:39
>I'd like an if-test (in bash or ksh) that matches a string >against a regexp. The best I can come up with is > >if { echo $givenstring | grep "regexp" > /dev/null ; } >then et cetera >fi
First, using expr can simplify the expression some:
if expr "$givenstring" : "regexp" then et cetera fi
(Note that expr implictly anchors the regexp on both ends, so you need to add a .* to any end which you don't want anchored.)
Next, sometimes a full-blown regexp isn't really necessary, and the glob-matching of "case" is sufficient.
Finally, bash with "shopt -s extglob" enabled and ksh both support special matching syntax in [[ ]] tests which give you regexp abilitites, albeit with non-standard syntax; for example the ERE "foo.b*a?r$" would be rendered:
if [[ $givenstring == *foo?*(b)?(a)r ]] then et cetera fi
Ken Pizzini
documented on: 1999.09.27 Mon 15:14:50
Newsgroups: comp.os.linux.misc Date: 8 Dec 2002 16:38:40 GMT
> I'm trying to come up with a Perl script that will allow me to type in a > command-line math expression, push enter, and have the expression's evaluation > printed in the terminal. Here's a simple example:
Perl is overkill.
calc () { awk 'BEGIN {print '"$*"'}' }
USAGE: calc expression
NOTE: Characters special to the shell must be escaped.
Chris F.A. Johnson
N=3*2 perl -e "print eval $N"
Chris F.A. Johnson
Newsgroups: comp.security.unix
> Do you know whether the ascii table can be look up in man pages? thanks
try 'man ascii'.
Ulf
Or you could use 'awk' to produce a (printable) character map of some description;
awk 'BEGIN{print "char octal hex decimal" for(i=32;i<127;++i)printf "%c \\%03o %02x %3d\n",i,i,i,i exit}'
$ jot 20 | hexdump -C 00000000 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| 00000010 39 0a 31 30 0a 31 31 0a 31 32 0a 31 33 0a 31 34 |9.10.11.12.13.14| 00000020 0a 31 35 0a 31 36 0a 31 37 0a 31 38 0a 31 39 0a |.15.16.17.18.19.| 00000030 32 30 0a |20.| 00000033 $ jot -w'%c' -s '' 60 | hexdump -c 0000000 001 002 003 004 005 006 \a \b \t \n \v \f \r 016 017 020 0000010 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037 0000020 ! " # $ % & ' ( ) * + , - . / 0 0000030 1 2 3 4 5 6 7 8 9 : ; < \n 000003d
"%06.6_ad: " 16/1 "%02.2X " " " "%_p" "\n"
> hexdump -f hexformat_1.txt -n 400 FILE2DUMP
-f: formatfile to be used (see above) -n: number of bytes to dump -s: offset from where to begin to dump binary file in bytes FILE2DUMP: binary file that should be dumped
$ jot 20 | hexdump -f /export/histories/tdat/hexdump.fmt 000000: 31 0A 32 0A 33 0A 34 0A 35 0A 36 0A 37 0A 38 0A 1.2.3.4.5.6.7.8. 000016: 39 0A 31 30 0A 31 31 0A 31 32 0A 31 33 0A 31 34 9.10.11.12.13.14 000032: 0A 31 35 0A 31 36 0A 31 37 0A 31 38 0A 31 39 0A .15.16.17.18.19. 000048: 32 30 0A 20.
"%06.6_ad : " 8/1 "%3_u " "\t\t" "%_p " "\n"
> hexdump -f hexformat_2.txt -n 400 FILE2DUMP
000000 : del E L F soh soh soh nul . E L F . . . . 000008 : nul nul nul nul nul nul nul nul . . . . . . . . 000016 : stx nul etx nul soh nul nul nul . . . . . . . . 000024 : 90 a7 eot bs 4 nul nul nul . . . . 4 . . . 000032 : A ac etx nul nul nul nul nul A . . . . . . . 000040 : 4 nul nul ack nul ( nul 4 . . . . ( . 000048 : esc nul sub nul ack nul nul nul . . . . . . . . 000056 : 4 nul nul nul 4 80 eot bs 4 . . . 4 . . . 000064 : 4 80 eot bs c0 nul nul nul 4 . . . . . . . 000072 : c0 nul nul nul enq nul nul nul . . . . . . . . 000080 : eot nul nul nul etx nul nul nul . . . . . . . . 000088 : f4 nul nul nul f4 80 eot bs . . . . . . . . 000096 : f4 80 eot bs dc3 nul nul nul . . . . . . . . 000104 : dc3 nul nul nul eot nul nul nul . . . . . . . . 000112 : soh nul nul nul soh nul nul nul . . . . . . . .
Newsgroups: comp.unix.shell Date: 1995/09/14
|> I want to convert a hex number to the character value. In C I can do the following |> |> |> printf("%c\n", 0x21);
You can use the desk calculator /bin/dc:
Num=21 echo "16i${Num}P" | dc
Explaination of commands:
dc uses postfix (reverse Polish) notation. The command string is interpreted as:
"16" - Pushes 16 on stack
"i" - Pops top of stack and uses this value to set the input base. Thus, all further inputs are hex.
"21" - Push 21 on stack. Interpreted as hex value.
"P" - Pop top of stack, interpret as a char, and print it.
Praveen Puri
Newsgroups: comp.unix.questions Date: 1995/11/16
|> Is there any tools in unix that can do HEX --> Decimal (and the |> reverse) conversions...
dec to hex
lucio 291 > bc ibase=10 obase=16 10 A <-- indicates computer reply 11 B <-- 16 10 <-- ctl-D
hex to dec
lucio 295 > bc ibase=16 do NOT specify obase !! 0A 10 <-- 0B 11 <-- 10 16 <-- FF 255 <--
Lucio Chiappetti
$ bc
ibase=16 0A 10 0a (standard_in) 3: parse error
Newsgroups: comp.os.linux.misc > Is there a calculator for Linux (Suse 6.3) for the shell available ? (no > X)
Linux is the operating system of simple tools to get wonderful results combining them. For a calculator with traditional (infix) notation, try command bc with option -l (bc -l). You'll be put in interactive mode, where (surprisingly enough), 4*4 yields 16 and so on.
You can even have a shell alias like Debian's Spanish localization:
calculator () { echo $* | bc -l }
So you can exec "calculator 4*4" from the shell prompt, and get the result in your stdout.
Jose Luis Domingo Lopez
$ echo 1.2 2.34 + p | dc 3.54
$ echo 1.2 2.34 + 2 / p | dc 1
$ echo 2 k 1.2 2.34 + 2 / p | dc 1.77
Dc is a reverse-polish desk calculator which supports unlimited pre- cision arithmetic. It also allows you to define and call macros. Normally dc reads from the standard input;
Normally dc reads from the standard input; if any command arguments are given to it, they are filenames, and dc reads and executes the contents of the files before reading from standard input.
Printing Commands p Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.
n Prints the value on the top of the stack, popping it off, and does not print a newline after.
Parameters Dc has three parameters that control its operation: the precision, the input radix, and the output radix.
i Pops the value off the top of the stack and uses it to set the input radix.
o Pops the value off the top of the stack and uses it to set the output radix.
k Pops the value off the top of the stack and uses it to set the precision.
$ bc 4 + 2*3 - 2/2 - (1 + 2) 6
$ echo "1.2 + 2.34 " | bc 3.54
$ echo "1.2 / 2.34 " | bc 0
$ echo "1.2 / 2.34 " | bc -l .51282051282051282051
$ echo "scale=4; 1.2 / 2.34 " | bc -l .5128
$ echo 'ibase=16; 11' | bc 17
If bc is invoked with the -l option, a math library is preloaded and the default scale is set to 20. The math functions will calculate their results to the scale set at the time of their call.
Thank you all who answered my question. Here is my conculsion. Just FYI.
> >>Is there any tools under Unix to show files similar to dos debug? I mean > >>the output looks like the following: so that I can see both the hex code > >>and the ascii codes: > >> > >In terms of "always available" there's "od -cx".
Sample output:
0000000 # ! / u s r / b i n / s h \n # \t 2321 2f75 7372 2f62 696e 2f73 680a 2309 0000020 C o p y r i g h t ( c ) 1 9 436f 7079 7269 6768 7420 2863 2920 3139
BTW, od -cx1 doesn't work
> >With GNU od > >(at the very least; I'm not certain how GNU-specific the "z" > >format is) you can also use "od -tx1z" to better match your > >sample format.
od: invalid character `z' in type string `x1z'
od -tx1 /bin/true 0000000 23 21 2f 75 73 72 2f 62 69 6e 2f 73 68 0a 23 09 0000020 43 6f 70 79 72 69 67 68 74 20 28 63 29 20 31 39
od -tx1 -c
Sample output:
0000000 23 21 2f 75 73 72 2f 62 69 6e 2f 73 68 0a 23 09 # ! / u s r / b i n / s h \n # \t 0000020 43 6f 70 79 72 69 67 68 74 20 28 63 29 20 31 39 C o p y r i g h t ( c ) 1 9
This is the best result I can get.
> >There are also a few "hexdump" programs to be > >found in the comp.sources.* archives. > > > > --Ken Pizzini > > Or, if you're an emacs user, did you try > > M-x hexl-mode ? > > On the other hand, the format is a bit different but then you can also edit > the file... > > Jens
I settle down with this one. Thanks!
$ od -t x1 TuGnrl.txt | grep ' 00 ' 0020320 72 65 73 73 69 6f 6e 5d 00 0a 20 0a 20 2d 2d 20 0035060 00 20 62 75 74 20 74 68 65 20 64 6f 63 75 6d 65 0064620 2e 00 0a 0a 3d 3d 20 4e 6f 74 65 73 0a 0a 66 66 0112040 00 0a 0a 23 23 20 57 68 61 74 20 22 64 6f 74 22
od -x means od -t x2, which is not a good selection. *M*: always use "od -t x1"
documented on: 1999.10.10 Sun 16:44:37