LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2017, 07:00 PM   #1
Pzykostyle
LQ Newbie
 
Registered: Oct 2017
Posts: 2

Rep: Reputation: Disabled
BASH will not allow a subtraction symbol in my IF


so here is the issue. this IF statement returns the following error:

if [ $(( $actmin - $loginmin )) -lt 0 ]; then
ttimehr=$(( ($acthr - $loginhr) - 1 ))
ttimemin=$(( ( $actmin + 60 ) - $loginmin ))


./timeOn1: line 45: 49 - 47
1
39 : syntax error in expression (error token is "1
39 ")

im not sure why it changed but several times it would tell me that the subtraction symbol was not valid either.
I'm mostly confused here because if I run the preceding variables and that IF statement in the parent shell it works fine with no errors but it will NOT work within the subshell.
 
Old 12-17-2017, 09:25 PM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Perhaps the subshell is not bash. Try calling the subshell with the bash command:
Code:
bash my_script.sh
 
Old 12-18-2017, 12:21 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
please use code tags for code and terminal output.
like this:
Code:
if [ $(( $actmin - $loginmin )) -lt 0 ]; then
    ttimehr=$(( ($acthr - $loginhr) - 1 ))
    ttimemin=$(( ( $actmin + 60 ) - $loginmin ))
(...)
./timeOn1: line 45: 49 - 47
1
39 : syntax error in expression (error token is "1
39 ")
since you post only part of your script we do not know if the error message refers to the bit you posted.
but anyhow.
your code is very convoluted, too many brackets.
try this:
Code:
if [ $(( actmin - loginmin )) -lt 0 ]; then
    ttimehr=$(( acthr - loginhr - 1 ))
    ttimemin=$(( actmin + 60 - loginmin ))
are all the variables initialized with numerical values?
would it be possible to post the whole script instead, or a working example that can reproduce the problem?
is this bash?
have you tried
Code:
set -x
???
 
Old 12-18-2017, 05:24 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
As with ondoho's post, you need to learn more about how bash does arithmetic.
And the first part of that is to remove all square brackets:
Code:
if (( actmin - loginmin  < 0 )); then
  (( ttimehr  = acthr  - loginhr  - 1  ))
  (( ttimemin = actmin - loginmin + 60 ))
As indicated though, without knowing what all these variables are set to, this may still error
 
Old 12-18-2017, 05:34 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
newline character in variable?
 
  


Reply

Tags
error, math



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 ... Date /days subtraction Addition and compare em31amit Programming 5 05-24-2012 01:12 PM
[SOLVED] How to use the % symbol in bash shell scripts RedNeck-LQ Programming 5 08-12-2011 02:01 AM
BASH Symbol help Requested. theKbStockpiler Programming 8 04-20-2011 10:30 PM
What does symbol ^ do in bash? Mr. Alex Linux - Newbie 4 09-14-2010 10:48 AM
Simple subtraction using bash and bc madpear Programming 2 11-04-2009 01:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:19 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
Open Source Consulting | Domain Registration