Traveltime script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Traveltime script
# 1  
Old 08-07-2010
Traveltime script

Hi, i',m new to bash scripting and i have the following scrip,
PHP Code:
#!/bin/bash 
# traveltime - a program to calculate how long it will
# take to travel a fixed distance 
# syntax: traveltime miles mph 

X60=$(($60)) 
TOTMINUTES=$(($X60 / $2)) 
HOURS=$(($TOTMINUTES 60)) 
MINUTES=$(($TOTMINUTES 60)) 
echo 
"The trip will take $HOURS hours and $MINUTES minutes" 
however, when i try to executig it i get an error saying "traveltime: line 6: * 60: syntax error: operand expected (error token is "* 60")"
however, i can't see why it would say that. I though that would be ok. Can somebody tell me how i can solve this please. thanks
# 2  
Old 08-07-2010
Try this in place of the existing MINUTES= line. It takes the number of minutes in the derived HOURS and subtracts from the original number of minutes.

Code:
MINUTES=$(( $TOTMINUTES - ($HOURS \* 60) ))

./testscript 121 60

The trip will take 2 hours and 1 minutes


Stop press.

Got it working by escaping the percent sign (i.e. the remainder from the calculation) with:

Code:
MINUTES=$(($TOTMINUTES \% 60))


Last edited by methyl; 08-07-2010 at 05:15 PM.. Reason: late edit
# 3  
Old 08-07-2010
works out of the 'box' for me on FC12.
nice work, btw.
# 4  
Old 08-07-2010
You don't need to put dollar signs inside the evaluation brackets. $((A+B)) is perfectly valid if A and B are variables containing integers.
# 5  
Old 08-08-2010
Quote:
Originally Posted by nvidiaaaa2
however, when i try to executig it i get an error saying "traveltime: line 6: * 60: syntax error: operand expected (error token is "* 60")"
however, i can't see why it would say that.
You'd get this error if you didn't pass any parameters in on the command line. If no parameters are passed to the script, the $1 (line 6) would evaluate to null and the resulting expression would be

Code:
$(( * 60 ))

which is missing an operand. You could avoid this by testing for the number of parameters passed to the script and aborting if one is missing:

Code:
if $(( $# < 2 )) 
then
   echo "missing parameter(s); expected: distance speed"
fi

If you are not familiar with it, the variable $# evaluates to the number of parameters passed to the script (or to a function if the code is inside of a function).
# 6  
Old 08-08-2010
Quote:
Originally Posted by methyl
Try this in place of the existing MINUTES= line. It takes the number of minutes in the derived HOURS and subtracts from the original number of minutes.

Code:
MINUTES=$(( $TOTMINUTES - ($HOURS \* 60) ))
 
./testscript 121 60
 
The trip will take 2 hours and 1 minutes


Stop press.

Got it working by escaping the percent sign (i.e. the remainder from the calculation) with:

Code:
MINUTES=$(($TOTMINUTES \% 60))

Mythl, thanks for your reply, i adjusted my code to

Code:
 

#X60=$(($1 * 60))
TOTMINUTES=$(($X60 / $2))
HOURS=$(($TOTMINUTES / 60))
MINUTES=$(($TOTMINUTES \% 60))

i get this error ": line 12: / : syntax error: operand expected (error token is "/ ")" what should it look like in full?
Also, i'm using sh traveltime to execute my script, is that still a valid way or doing it as i'm still new to this?
# 7  
Old 08-08-2010
Quote:
Originally Posted by nvidiaaaa2
Mythl, thanks for your reply, i adjusted my code to

Code:
 

#X60=$(($1 * 60))
TOTMINUTES=$(($X60 / $2))
HOURS=$(($TOTMINUTES / 60))
MINUTES=$(($TOTMINUTES \% 60))

i get this error ": line 12: / : syntax error: operand expected (error token is "/ ")" what should it look like in full?
Also, i'm using sh traveltime to execute my script, is that still a valid way or doing it as i'm still new to this?
Hi.

Code:
#X60=$(($1 * 60))

Is this a comment, a typo or a command prompt?

Code:
TOTMINUTES=$(($X60 / $2))

would certainly fail it it's a comment.

It's often helpful to run your script with
Code:
set -x

This will show you exactly what is failing.

You might also want to add some validation for command-line parameters.

Either, for example:
Code:
if [ $# -ne 2 ]; then
  echo "Usage: ...."
  exit
fi

or

Code:
X60=$(1:-1}


Last edited by Scott; 08-08-2010 at 12:12 PM.. Reason: -ne 1 -> -ne 2
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question