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.
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 06:15 PM..
Reason: Code tags
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))"
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
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)