Why won't this script run in the Command screen

Post new topic   Reply to topic    DD-WRT Forum Index -> General Questions
Author Message
aaronburro
DD-WRT Novice


Joined: 12 Nov 2018
Posts: 38

PostPosted: Wed Mar 27, 2019 6:35    Post subject: Why won't this script run in the Command screen Reply with quote
Code:
#!/bin/sh
FOO=food
BAR=bard
if [ $FOO -ne $BAR ]
then
    echo "hmmm"
fi


I have tried every single permutation of an if I can think of, and it will not run. It keeps saying
Code:
sh: eval: line 7: syntax error: unexpected "fi" (expecting "then")


Seriously, what am I doing wrong here?
Sponsor
Dr_K
DD-WRT User


Joined: 23 Mar 2018
Posts: 442

PostPosted: Wed Mar 27, 2019 20:29    Post subject: Re: Why won't this script run in the Command screen Reply with quote
aaronburro wrote:
Seriously, what am I doing wrong here?


Use SSHd or telnet...

The Command window in the webif is hit or miss on what it interprets correctly??...only causing erratic resaults...been that way for a long time...
Some functions work....so we can't call it broken Mad

_________________
Location 1
R7800- DD-WRT v3.0-r53562 (10/03/23) Gateway
WNDR3400v1 DD-WRT v3.0-r35531_mega-nv64k (03/26/18 ) Access Point
WRT160Nv3 DD-WRT ?v3?.0-r35531 mini (03/26/18 ) Access Point
WRT54GSv5 DD-WRT v24-r33555_micro_generic (10/20/17) Repeater
Location 2
R7800- DD-WRT v3.0-r51855 (02/25/23) Gateway
R6300v2- DD-WRT v3.0-r50671 (10-26-22) Access Point
WNDR3700v2 DD-WRT v3.0-r35531 std (03/26/18 ) Access Point
E1200 v2 DD-WRT v3.0-r35531 mega-nv64k (03/26/18 ) Gateway(for trivial reasons)
RBWAPG-5HACT2HND-BE RouterOS-v6.46.4 (2/21/20) Outdoor Access Point
2x RBSXTG-5HPACD RouterOS-v6.46.4 (2/21/20) PTP Bridge 866.6Mbps-1GbpsLAN
Location 3
2x R7000- DD-WRT v3.0-r50671 (10/26/22) Access Points
2x RBWAPG-60AD RouterOS-v6.45.9 (04/30/20) PTP Bridge 2.3Gbps-1GbpsLAN
2x RBSXTsqG-5acD RouterOS-v6.49.7 (10/14/22) PTP Bridge 866.6Mbps-1GbpsLAN

Thank You BrainSlayer for ALL that you do & have done, also to "most" everyone here that shares their knowledge
aaronburro
DD-WRT Novice


Joined: 12 Nov 2018
Posts: 38

PostPosted: Wed Mar 27, 2019 23:30    Post subject: Reply with quote
OK. I tried that in a script from SSH, and I get
Code:
sh: food: out of range


I'm feeling like I can't code today Sad
Wildlion
DD-WRT Guru


Joined: 24 May 2016
Posts: 1465

PostPosted: Thu Mar 28, 2019 1:22    Post subject: Reply with quote
if you do an echo $food is it blank? If so that is not a valid value and gives you an out of range. The assignments are just variable to variable ie number. If your script is trying to assign words to a variable you use "food" and "bard" (put in quotes) but then you need to change your comparison

Code:

!/bin/sh

FOO="food"
BAR="bard"
if [ $FOO != $BAR ]
then
    echo "hmmm"
fi


or for numbers:
Code:

FOO=3
BAR=5
if [ $FOO -ne $BAR ]
then
    echo "hmmm"
fi

egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 13880
Location: Netherlands

PostPosted: Thu Mar 28, 2019 10:57    Post subject: Reply with quote
The quotation marks are not necessary in this case (but do not hurt either) but @wildlion is correct string comparison is done with =

STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
aaronburro
DD-WRT Novice


Joined: 12 Nov 2018
Posts: 38

PostPosted: Thu Mar 28, 2019 15:32    Post subject: Reply with quote
Yes, I was trying to do string comparisons. I'm not use to needing to quote strings in sh. Changing to != fixed everything. Oi.
Wildlion
DD-WRT Guru


Joined: 24 May 2016
Posts: 1465

PostPosted: Thu Mar 28, 2019 22:25    Post subject: Reply with quote
True... I always use the quotation marks to tell myself that it is a string, instead of a variable or anything else.
aaronburro
DD-WRT Novice


Joined: 12 Nov 2018
Posts: 38

PostPosted: Fri Mar 29, 2019 21:19    Post subject: Reply with quote
Thx for helping me remove my head from my butt. I didn't expect there to be a difference between -ne and !=. Still feeling my way around Linux and bash/sh
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 13880
Location: Netherlands

PostPosted: Sat Mar 30, 2019 8:44    Post subject: Reply with quote
https://ryanstutorials.net/bash-scripting-tutorial/bash-script.php
_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> General Questions All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum