LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-22-2004, 02:26 AM   #1
adz
Senior Member
 
Registered: Jun 2003
Location: Sydney
Distribution: Debian, FreeBSD
Posts: 1,713

Rep: Reputation: 53
Bash script problems with "if" statement


Hi there.

I've been trying to get a script of mine working. The part that doesn't work (below) is involved with determining how long the script was running for. Here is the code:
Code:
#!/bin/sh

TOTAL_HOURS=-22
TOTAL_MINUTES=-10

#If TOTAL_HOURS is negative then add 24 to it
if [ "$(TOTAL_HOURS)" -lt "0" ];then
	TOTAL_HOURS= `expr $(TOTAL_HOURS) + 24`
fi

#If TOTAL_MINUTES is negative then add 60 to it and takeaway 1 from TOTAL_HOURS
if [ "$(TOTAL_MINUTES)" -lt "0" ];then
	TOTAL_HOURS= `expr $(TOTAL_HOURS) - 1`
	TOTAL_MINUTES= `expr $(TOTAL_MINUTES) + 60`
fi

#Formulate the total time taken into one line
TOTAL_TIME=`echo $TOTAL_HOURS`h`echo $TOTAL_MINUTES`m

#Output TOTAL_TIME to stdout
echo $TOTAL_TIME
The output I get from this is:
Code:
adz@hades:~/temp$ ./test 
./test: line 1: TOTAL_HOURS: command not found
./test: line 7: [: : integer expression expected
./test: line 1: TOTAL_MINUTES: command not found
./test: line 12: [: : integer expression expected
-22h-10m
The output I want from it is just "1h50m". I've tried with and without the brackets around the variable name in the expr expression. I'm convinced it's something simple but I can't put my finger on it.

Last edited by adz; 05-22-2004 at 02:28 AM.
 
Old 05-22-2004, 03:19 AM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
All I did was remove the quotes around the $VARIABLES (makes only cosmetic differences in this case) and especially the $(parentheses); $() is similar to backticks; both are forms of command substitution. You were trying to execute TOTAL_HOURS and TOTAL_MINUTES .
Code:
#!/bin/sh

TOTAL_HOURS=-22
TOTAL_MINUTES=-10

#If TOTAL_HOURS is negative then add 24 to it
if [ $TOTAL_HOURS -lt 0 ];then
        TOTAL_HOURS=`expr $TOTAL_HOURS + 24`
fi

#If TOTAL_MINUTES is negative then add 60 to it and takeaway 1 from TOTAL_HOURS
if [ $TOTAL_MINUTES -lt 0 ];then
        TOTAL_HOURS=`expr $TOTAL_HOURS - 1`
        TOTAL_MINUTES=`expr $TOTAL_MINUTES + 60`
fi

#Formulate the total time taken into one line
TOTAL_TIME=`echo $TOTAL_HOURS`h`echo $TOTAL_MINUTES`m

#Output TOTAL_TIME to stdout
echo $TOTAL_TIME
HTH!
 
Old 05-22-2004, 04:34 AM   #3
adz
Senior Member
 
Registered: Jun 2003
Location: Sydney
Distribution: Debian, FreeBSD
Posts: 1,713

Original Poster
Rep: Reputation: 53
Brilliant. I knew it was something simple. I was sure I tried your solution previously but maybe buggered something else up. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 03:20 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM


All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration