Questions tagged [expr]

expr - evaluate arguments as an expression

The expr utility shall evaluate an expression and write the result to standard output.

25 questions
77
votes
7 answers

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?
Nicolas Raoul
  • 7,945
  • 14
  • 43
  • 55
16
votes
3 answers

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 there are variables instead of integer values do we?…
Stranger
  • 187
  • 1
  • 1
  • 7
8
votes
2 answers

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-variable from it in bash in big-endian like 0x80000?
DenisNovac
  • 285
  • 2
  • 3
  • 10
4
votes
3 answers

Convert only parts of a filename to uppercase

In a shell script program, I need to convert the filenames to uppercase if the converted filename does not already exist. In this particular case I need to change only the basename to uppercase leaving the extension (if any) as it is. My idea of…
Esha
  • 183
  • 6
3
votes
1 answer

Understanding of the regexp match feature of the expr utility

Could anyone help me to understand how the regex match works with the expr utility? I've read its man page and below is an excerpt: STRING : REGEXP anchored pattern match of REGEXP in STRING But I can't understand how it works. I did some…
Fajela Tajkiya
  • 965
  • 5
  • 15
3
votes
3 answers

Set a variable to the result of a division and subtraction command

I am trying to get the difference of 2 dates in epoch form and convert the number back to days: EXPIRYEPOCH=$(date --date="$EXPIRYDATE" +%s) TODAYEPOCH=$(date --date="$TODAYSDATE" +%s) DAYSLEFT=$(expr ($EXPIRYEPOCH - $TODAYEPOCH) / 86400 ) The…
Kahn
  • 1,652
  • 2
  • 19
  • 36
2
votes
2 answers

How to add mathematically the results of two commands in Linux

In a Debian Linux shell, I am trying to add the results of two separate commands so that I know the sum of both. I already tried: echo $(expr $(du -sh /srv/mysql) + $(du -sh /srv/www)) and variations of it. It's returning: expr: "syntax error:…
davidman77
  • 121
  • 3
2
votes
1 answer

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 "${prog}"` prog="${progdir}/${newProg}" fi …
Jim
  • 9,750
  • 15
  • 57
  • 84
2
votes
1 answer

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 work: yes What I want to achieve: I want to use…
SantaXL
  • 355
  • 1
  • 15
2
votes
1 answer

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
user183924
  • 21
  • 2
2
votes
3 answers

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 script has a type casting error somewhere. Bash…
resu
  • 133
  • 1
  • 6
2
votes
2 answers

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 matching. And also: $ test=`expr "hello123there" :…
夢のの夢
  • 491
  • 6
  • 10
  • 20
1
vote
1 answer

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}' gives 505050505050505024 Can somebody explain?
user305910
1
vote
2 answers

Echo calculation to text file

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 result to my text file. It has to print out my…
peter
  • 17
  • 4
1
vote
2 answers

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 ] then echo -ne…
Lost Vanity
  • 13
  • 1
  • 3
1
2