LinuxQuestions.org
Visit Jeremy's Blog.
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, 08: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, 10:25 PM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 524

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

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
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, 06:24 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
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, 06:34 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

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


Reply

Tags
error, math


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

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

All times are GMT -5. The time now is 02:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration