Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [expr]

expr - evaluate arguments as an expression

6
votes
2answers
255 views

How to read string as hex number in bash?

I have the bash line: expr substr $SUPERBLOCK 64 8 Which is return to me string line: 00080000 I know that this is, actually, a 0x00080000 in little-endian. Is there a way to create integer-...
1
vote
1answer
182 views

awk arithmetic differs from expr

There are some contrast output between awk arithmetic and expr. Example expr 11111111111111111111 / 22 gives 505050505050505050 but with awk: echo '11111111111111111111' | awk '{q=$1/22;;print q}...
1
vote
2answers
77 views

Echo calculation to text file [duplicate]

I am working on a project to calculate my overtime at work with a shell script. I have two inputs and want to find if my number is over 800 = 8 hours; if it is bigger, then it has to print out the ...
0
votes
1answer
52 views

Changing a variable value balance

I am practicing unix, self learning so I done some basic coding of creating a cash machine. so far I have done the following and need some guidance on the final hurdle I am stuck on how I can update ...
3
votes
1answer
851 views

What does this expr do in a shell script?

What does the following do exactly? newProg=`expr "${newProg}" : ".* -> \(.*\)$"` if expr "x${newProg}" : 'x/' >/dev/null; then prog="${newProg}" else progdir=`dirname "...
1
vote
1answer
299 views

expr and variables

How do I run the script: ./script.sh 1 \* 2 Eventually: ./script.sh 1 '*' 2 How does my script look like: args="$@" # the first line of the script result=$(($args)) echo "$args = $result" Does it ...
1
vote
2answers
3k views

shell - using expr “multiplication table”

I'm learning shell to create a multiplication table, I write code like that: #!/ in/bash for i in 1 2 3 4 5 6 7 8 9 do for j in 1 2 3 4 5 6 7 8 9 do if [ $j -le $i ] ...
0
votes
1answer
753 views

What is the difference between bcl and expr?

I know that both of them can do simple arithmetic. I'm wondering when it will be easier to use one versus the other. I know that expr evaluates an expression from its arguments, while bcl evaluates ...
2
votes
1answer
1k views

arithmetic operation with expr

I am doing add operation as #!/bin/sh a=10 b=20 c='expr $a + $b' echo "$c" echo "$a" echo "$b" but it is showing output as expr $a + $b 10 20 what is wrong with expr
3
votes
3answers
534 views

Proper type casting in a shell script for use with while loop and modulus

I am trying to write a script to get a random, even hex number. I have found the the openssl command has a convenient option for creating random hex numbers. Unfortunately, I need it to be even and my ...
1
vote
2answers
82 views

Error while counting characters in a file

I have a source code to find a number of words and characters in a file: #!/bin/bash w=0 cc=0 for i in `cat $1` do j=$i echo $j w=$(($w+1)) c=`expr $j:'.*'` cc=$(($cc+$c)) done echo "no of characters"...
6
votes
2answers
11k views

Using expr, $(()), (())

In shell script we can substitute expr $a*$b with $(($a+$b)). But why not just with (($a+$b)), because in any resource it is written that (()) is for integer computation. So we use $(()) when ...
0
votes
2answers
242 views

Meaning of `expr “hello” : “\([a-z]*\)”`?

I need a explanation of why: $test=`expr "hello" : "\([a-z]*\)"`; echo $test would print out hello, where as: $test=`expr "hello" : "hel"`; echo $test would return the number of characters ...
-1
votes
1answer
333 views

Trying to sum a decimal column from a csv file for each different vendor

I am trying to sum a column for each vendor in a loop. I apologize for the vagueness. I will try to explain. It is suppose to read from a file and based on the vendor name total the field labeled ...
0
votes
1answer
103 views

Raspbian: evaluate conditional strings and/or numbers

I am having a really a hard time to evaluate conditional strings and numbers. After hours of reading and trying with (my Beginners Guide) I keep getting the error msgs like syntax error in conditional ...
1
vote
1answer
2k views

Substring extraction with expr

I was working on extraction of sub-string from the beginning & at the end of the string. I experimented with and with out ".*" $expr "ab1cd1efgfedcbaAAAA" : ".*\(1[a-l]*\)" # Result: 1efgfedcba ...
0
votes
3answers
246 views

significance behind escaping parentheses in expr match

Can anybody give me the difference between the following two commands in the context of escaping parentheses, or refer some document that would clear my doubt. string=abcABC123ABCabc $ echo `expr ...
58
votes
6answers
115k views

Parenthesis in expr arithmetic: 3 * (2 + 1)

expr does not seem to like parenthesis (used in mathematics to explicit operator priority): expr 3 * (2 + 1) bash: syntax error near unexpected token `(' How to express operator priority in bash?