Need help with "expr"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with "expr"
# 1  
Old 06-17-2011
Need help with "expr"

Hi,

I have a script that works perfectly fine, but need to know the use of a command.
I was trying to gain some knowledge from the script but I couldn't understand the use of expr in it.
Code:
  expr "trycnt + 1"
  if [ $trycnt -eq $maxtry ]; then
         boolean=0
         echo "tried $maxtry time.... giving up"
  fi

     else
  boolean=0
  fi
  done
  fi


1. expr isn't getting executed, expr "trycnt + 1" -> it gave me output as trycnt + 1 and nowhere in this script it has been used. Just want to understand it.

Siddhesh.K

Last edited by Scott; 06-17-2011 at 05:15 PM.. Reason: Code tags
# 2  
Old 06-17-2011
The use of expr there is simply wrong. Expr works like this:

Code:
$ expr "5" "+" "3"
8
$ expr "5 + 3"
5 + 3
$

Each number, symbol, and individual bracket has to be their own, separate parameter to get a sensible result. And even when it works, it only prints the numbers, it doesn't actually alter any variables. You have to put it in backticks to get the number back into the shell(because expr is often not a part of the shell at all) so it's a bit awkward to use.

Code:
v=1
v=`expr "$v" "+" "1"`

If you have a shell that supports it, like ksh or bash, you can use the more intuitive (( )) math syntax.

Code:
v=1
echo "v is $v"
((v=v+1))
echo "v is now $v"
echo "v plus one is $((v+1))"

# 3  
Old 06-17-2011
Hello,

Thanks for the reply. I have a script the works perfectly fine with me. Here is my script.
Code:
#!/bin/bash

printf " ---------------------------------------------------------------------------------------------------\n" >> $SHEPHERD_INFO.$DATE
printf " File Name               : Status Of SHEPHERD Process `hostname` \n" >> $SHEPHERD_INFO.$DATE
printf " Author                  : Siddhesh Khavnekar \n" >> $SHEPHERD_INFO.$DATE
printf " Description             : Displays Information on AMS Shepherd Process\n" >> $SHEPHERD_INFO.$DATE
printf " Last Modified       : `date +%m/%d/%y` \n" >> $SHEPHERD_INFO.$DATE
printf " ----------------------------------------------------------------------------------------------------\n" >> $SHEPHERD_INFO.$DATE

maxtry=5
trycnt=0
SHEPHERD_INFO=/root/shepherd_pid
DATE=`date +%Y%m%d%H%M%S`
log_file="shepherd.$$.pid"
echo $DATE >> $SHEPHERD_INFO.$DATE

printf "\n\n################### Shepherd PID Before restart #######################\n\n" >>  $SHEPHERD_INFO.$DATE


present_pid=`ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}'`
echo "$present_pid" >> $SHEPHERD_INFO.$DATE

sleep 10
sh /root/ams_rc stop_shepherd > /dev/null

if [ "$?" -eq "0" ]
then
sleep 30
sh /root/ams_rc start_shepherd > /dev/null
else
boolean=1
echo "Shepherd not stopped Properly" > /root/$log_file

#check the process again
#boolean : to run the while loop for 3 times before giving up. If the condition is satisfied and when the variable has zero value, then it comes out of loop

while [ $boolean ]
do
ps -ef | grep -v grep | grep -w $present_pid
if [ "$?" -eq "0" ]
then
sh /root/ams_rc stop_shepherd > /dev/null
sleep 30

expr "trycnt + 1"
if [ $trycnt -eq $maxtry ]; then
       boolean=0
       echo "tried $maxtry time.... giving up"
fi

else
boolean=0
fi
done
fi

sleep 30

printf "\n\n################### Shepherd PID After restart #######################\n\n" >>  $SHEPHERD_INFO.$DATE
ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' >> $SHEPHERD_INFO.$DATE
ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' >> /root/$log_file
echo "Shepherd Started" > /root/$log_file


#mail the log file
mail -s " shepherd Restart Status `date` " coreservicesvfnz@vodafone.com < /root/$log_file
mail -s " shepherd Restart Log `date` " coreservicesvfnz@vodafone.com < /root/shepherd_pid

exit 0

Siddhesh.K

Last edited by Scott; 06-17-2011 at 05:14 PM.. Reason: Added code tags
# 4  
Old 06-17-2011
Quote:
Originally Posted by Siddheshk
Hello,

Thanks for the reply. I have a script the works perfectly fine with me. Here is my script.
Please use code tags for code. [ code ] stuff [ /code ] without the extra spaces in the tags.

I don't think it works perfectly fine. It's full of logical errors and doesn't even print everything you thought it did.

Code:
#!/bin/bash

# You are using $SHEPHERD_INFO and $DATE before you set them.
# This will cause errors or weird named files.  Move the statements up here:
SHEPHERD_INFO=/root/shepherd_pid
DATE=`date +%Y%m%d%H%M%S`

# If you're printing dozens of statements to the same destination, a
# redirection would be more efficient, done like this:


exec 5<&1  # save stdout in case you want it back
exec 1>$SHEPHERD_INFO.$DATE # make stdout go to $SHEPHERD_INFO.$DATE

#printf " ---------------------------------------------------------------------------------------------------\n" >> $SHEPHERD_INFO.$DATE
#printf " File Name               : Status Of SHEPHERD Process `hostname` \n" >> $SHEPHERD_INFO.$DATE
#printf " Author                  : Siddhesh Khavnekar \n" >> $SHEPHERD_INFO.$DATE
#printf " Description             : Displays Information on AMS Shepherd Process\n" >> $SHEPHERD_INFO.$DATE
#printf " Last Modified       : `date +%m/%d/%y` \n" >> $SHEPHERD_INFO.$DATE
#printf " ----------------------------------------------------------------------------------------------------\n" >> $SHEPHERD_INFO.$DATE

# For printing large chunks of data you can use a here-document, like this.
# Also, hostname is available as $HOSTNAME letting you avoid launching
# an external program.
cat <<EOF
---------------------------------------------------------------------------------------------------
 File Name               : Status Of SHEPHERD Process $HOSTNAME
 Author                  : Siddhesh Khavnekar
 Description             : Displays Information on AMS Shepherd Process
 Last Modified       : `date +%m/%d/%y`
----------------------------------------------------------------------------------------------------
EOF

maxtry=5
trycnt=0
log_file="shepherd.$$.pid"
echo $DATE

printf "\n\n################### Shepherd PID Before restart #######################\n\n"

# You can avoid needing grep -v grep by altering the first pattern a little.
# [s]hep does the same thing as shep but won't match grep '[s]hep-'.

# We also don't need to save this in a variable.  We can redirect it
# directly to where it's needed -- or not redirect it at all since we
# changed stdout earlier
ps -ef | grep '[s]hep-1 | grep -v wls100ctl | awk '{print $2}'`
# echo "$present_pid" >> $SHEPHERD_INFO.$DATE

sleep 10

# "something ; if [ $? -eq 0 ]" is equivalent to "if something".
# 

# sh /root/ams_rc stop_shepherd > /dev/null
# if [ "$?" -eq "0" ]
if sh /root/ams_rc stop_shepherd > /dev/null
then
        sleep 30
        sh /root/ams_rc start_shepherd > /dev/null
else
        boolean=1
        echo "Shepherd not stopped Properly" > /root/$log_file

#check the process again
#boolean : to run the while loop for 3 times before giving up. If the condition is satisfied and when the variable has zero value, then it comes out of loop

# while [ $boolean ] doesn't do what you think it does.
# the loop will never end.
# while [ "$boolean" -eq 1 ] is more what you wanted.
# But you don't even need a variable to end the loop, you can
# break it anywhere you want with break.
while true # loop forever
do
        ps -ef | grep -v grep | grep -w $present_pid > /dev/null
        if [ "$?" -eq "0" ]
        then
                sh /root/ams_rc stop_shepherd > /dev/null
                sleep 30

                # As my last post explained, expr doesn't work this way.
                # expr "trycnt + 1"
                trycnt=`expr "$trycnt" "+" "1"`
                
                if [ $trycnt -eq $maxtry ]; then
                        echo "tried $maxtry time.... giving up"
                        break
                fi
        else
                break
        fi
done
# Not sure what this extra fi was for.
# fi

sleep 30

cat <<EOF

printf "\n\n################### Shepherd PID After restart #######################\n\n"

ps -ef | grep '[s]hep-1' | grep -v grep | grep -v wls100ctl | awk '{print $2}'
ps -ef | grep '[s]hep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' >> /root/$log_file
echo "Shepherd Started" > /root/$log_file


#mail the log file
mail -s " shepherd Restart Status `date` " coreservicesvfnz@vodafone.com < /root/$log_file
mail -s " shepherd Restart Log `date` " coreservicesvfnz@vodafone.com < /root/shepherd_pid

exit 0

# 5  
Old 06-18-2011
Quote:
Originally Posted by Corona688
If you have a shell that supports it, like ksh or bash, you can use the more intuitive (( )) math syntax.

Code:
v=1
echo "v is $v"
((v=v+1))
echo "v is now $v"
echo "v plus one is $((v+1))"


In any POSIX shell, use the standard syntax:
Code:
v=$(( $v + 1 ))

# 6  
Old 06-18-2011
HI,

Thanks for the reply.

Do u mean to say, that my previous script will not work. So far i have not encountered the failure while stopping the process. It has been clean so far.

what do u suggest? should i use the older script or the new one posted by you.

Thanks again.

Siddhesh.K
# 7  
Old 06-20-2011
Quote:
Originally Posted by Siddheshk
Do u mean to say, that my previous script will not work. So far i have not encountered the failure while stopping the process. It has been clean so far.
It means that I suggest you read the comments I left when correcting some of your program, and the rest of this thread for that matter. You asked what was wrong with your use of expr, we explained, then you went ahead and did the same wrong way?

Your script may work now but your error checking wasn't doing what you thought it did, if things go wrong I don't know what will happen.

And no, don't blindly use my program. Read the comments I left in it so you can understand my modifications and incorporate what things you think are necessary in your own.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document <<EOF echo "Sum is: `expr $1 + $2`" EOF Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Homework & Coursework Questions

How to lclear "expr: syntax error" eventhough everything looks fine?

Hi All, As per my knowledge in unix, my code looks fine. But still I am getting error (expr:syntax error). Please help me to resolve this error. Script : PRE_LBNO=0 PRE_DATE=0 TOT_PAY=0 TOT_REM=0 TOTAL=1 for Record_Type in `cut -c 1 Inputt.dat` do if ; then CURR_LBNO=` cut -c... (6 Replies)
Discussion started by: lathanandhini
6 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

convert a=(b/100)*a into a "expr" expression

Hi Guys, THis is the first time am using the expr expression. I like to know how to write the expression a=(b\100)*a. THis works fine if it gives without a bracket. the bracket should be present as i wanted to define the order of execution. Help me out. Thanks for your help in advance.... (2 Replies)
Discussion started by: mac4rfree
2 Replies

7. Shell Programming and Scripting

HowTo: reg expr doing grep "timestamp>$DesiredTime" logfile ?

I know I asked a similar question but I want to know if there is a regular expression existing that with a korn shell cmd, finds any timestamp data records in a file where it is greater then a timestamp in a shell variable ? something like : grep all records where it has a timestamp >... (5 Replies)
Discussion started by: Browser_ice
5 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit?

I found below script to check whether the variable is a digit in ksh. ############################ #!/bin/ksh REPLY="3f" if ]*\)'` != ${REPLY} && "${REPLY}" != "0" ]] then print "is digit\n" else print "not digit\n" fi ############################ Although it works fine, but... (6 Replies)
Discussion started by: sleepy_11
6 Replies
Login or Register to Ask a Question