Questions tagged [expr]
expr - evaluate arguments as an expression
27 questions
4
votes
1
answer
414
views
expr "/foo" : "/" gives a syntax error
The following:
expr "/foo" : "/"
Results in a syntax error. I don't understand why?
These variations do not cause a syntax error:
expr "/foo" : "/*"
expr "...
1
vote
1
answer
105
views
What is expr doing when processing arrays?
I have a BASH script problem that I've narrowed down but I can't understand what I'm seeing. I want to get a substring of an array in a succinct way. Here's what I'm seeing in bash, with 'set -x' so ...
2
votes
2
answers
128
views
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 ...
4
votes
1
answer
482
views
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 ...
0
votes
1
answer
308
views
Getting syntax error exception for “expr”
I have an xml file which looks like below
INPUT XML FILE
<ReconSummary>
<entryName>Total Deep</entryName>
<Code>777</Code>
<License>L</License>
<Tran>...
3
votes
3
answers
3k
views
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" +...
5
votes
3
answers
3k
views
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 ...
1
vote
2
answers
621
views
My expr is being printed but not evaluated
The code for the script is:
echo "Years:"
read age
x=`expr $age*365`
echo -e $x
The output when I call this script from the command line is as follows:
Years:
(say I put 20)
20*365
Why is it not ...
0
votes
1
answer
5k
views
'expr: syntax error: unexpected argument' - result from alias [duplicate]
I recently added an alias to my .bash_aliases file:
alias runhole="perfect && cd data_series_test && doa=$(ls -1 | wc -l) && a=$(expr $doa / 2 ) && perfect && ...
8
votes
2
answers
14k
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-variable ...
1
vote
1
answer
1k
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
2
answers
2k
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
1
answer
138
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 ...
2
votes
1
answer
2k
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 "...
2
votes
1
answer
992
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
2
answers
7k
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
1
answer
3k
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
1
answer
2k
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
2
votes
3
answers
3k
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
2
answers
231
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"...
21
votes
3
answers
51k
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 ...
2
votes
2
answers
2k
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 matching.
...
-1
votes
1
answer
454
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
1
answer
285
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
1
answer
3k
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
3
answers
948
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 ...
80
votes
7
answers
154k
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?