LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 07-16-2016, 10:40 PM   #1
notchy
LQ Newbie
 
Registered: Jul 2016
Posts: 5

Rep: Reputation: Disabled
Convert Bash Script to Ash Script


Hi All,

I am just beginning coding and I used a script that was created in bash script and I need this to run with ash shell script.

This will need to run on a router but my router is only has ash shell script installed.

When I run the script it complains about blank lines so I inputted '#' as comments and that seems to make those error messages go away but I am struggling with the last error msg.

"line 121: syntax error: unexpected "done" (expecting "then")"

Is this error occuring because I have not converted this to ash?

Can anyone assist and tell me the steps to make this be converted from bash to ash. and also let me know if there is anything wrong with the syntax as it seems correct to me and this has worked for other people using this code.

I already changed the first line to be "#!/bin/sh"

Code:
#!/bin/sh
#set -x			# activate debugging when launched from cmd
# folder location:  /jffs/scripts 
# file name: VeraOccupancy
# version: 2.00
#
# Instructions:
# modifying code - search for and replace all XX
#
# when using less than 4 MAC addresses: you must remove code for the other MACs
# when adding additional MAC addresses: ensure you understand each section needs to be replicateduplicate
#
# to be added to future versions... User Constants for all variables; Single code Loop to iterate through all MACs
#
#
# Vera Device & Button reference - place your info here to remind yourself
# MAC1 = Device: Occupancy Trigger (246) button 5
# MAC2 = Device: Occupancy Trigger (246) button 6
# MAC3 = Device: Wifi Tracking (278) button 3
# MAC4 = Device: Wifi Tracking (278) button 1
#
# User Constants
MAC_ADDRESS_1="0C:1D:AF:DF:4A:F5" # Mi4
MAC_ADDRESS_2="C8:60:00:11:E5:FF"  # Testing Device on Network
#MAC_ADDRESS_3="XX:XX:XX:XX:XX:XX"  # WindowsPC
#MAC_ADDRESS_4="XX:XX:XX:XX:XX:XX"  # FireTV
#
VERAURL="http://192.168.1.111:3480/data_request?id=lu_action&output_format=xml&DeviceNum=21&serviceId=urn:dcineco-com:serviceId:MSwitch1&action="
#
#
#
WATCHDOG_SLEEP_SEC=10
a1_2ghz_last="0"
a1_5ghz_last="0"
a2_2ghz_last="0"
a2_5ghz_last="0"
#a3_2ghz_last="0"
#a3_5ghz_last="0"
#a4_2ghz_last="0"
#a4_5ghz_last="0"
x=0
y=0
#
while sleep $WATCHDOG_SLEEP_SEC
do
        if [ "$x" == 60 ]; then
                # Every 10 minutes or so we do them all again, just in case Vera missed something
                x=0
                a1_2ghz_last="0"
                a1_5ghz_last="0"
				a2_2ghz_last="0"
				a2_5ghz_last="0"
				#a3_2ghz_last="0"
				#a3_5ghz_last="0"
				#a4_2ghz_last="0"
				#a4_5ghz_last="0"
        fi
        x=$(( $x + 1 ))

# Monitor MAC_ADDRESS_1

# 2.4ghz band
a1_2ghz_new=`wl_atheros -i ath0 assoclist | grep $MAC_ADDRESS_1`
if [ "$a1_2ghz_new" == "$a1_2ghz_last" ] ; then
	sleep 0
   else
	a1_2ghz_last="$a1_2ghz_new"
if [ "$a1_2ghz_new" == "assoclist $MAC_ADDRESS_1" ]; then
	wget -qs "${VERAURL}SetStatus1&newStatus1=1"
fi
fi


# 5ghz band
a1_5ghz_new=`wl_atheros -i ath1 assoclist | grep $MAC_ADDRESS_1`
if [ "$a1_5ghz_new" == "$a1_5ghz_last" ] ; then
	sleep 0
   else
	a1_5ghz_last="$a1_5ghz_new"
if [ "$a1_5ghz_new" == "assoclist $MAC_ADDRESS_1" ]; then
	wget -qs "${VERAURL}SetStatus1&newStatus1=1"
fi
fi


if [ -z "$a1_2ghz_new" ] && [ -z "$a1_5ghz_new" ] ; then
wget -qs "${VERAURL}SetStatus1&newStatus1=0"
fi


# Monitor MAC_ADDRESS_2

# 2.4ghz band
a2_2ghz_new =`wl_atheros -i ath0 assoclist | grep $MAC_ADDRESS_2`
if [ "$a2_2ghz_new" == "$a2_2ghz_last" ] ; then
	sleep 0
   else
	a2_2ghz_last="$a2_2ghz_new"
if [ "$a2_2ghz_new" == "assoclist $MAC_ADDRESS_2" ]; then
	wget -qs "${VERAURL}SetStatus2&newStatus1=1"
fi
fi


# 5ghz band
a2_5ghz_new=`wl_atheros -i ath1 assoclist | grep $MAC_ADDRESS_2`
if [ "$a2_5ghz_new" == "$a2_5ghz_last" ] ; then
	sleep 0
   else
	a2_5ghz_last="$a2_5ghz_new"
if [ "$a2_5ghz_new" == "assoclist $MAC_ADDRESS_2" ]; then
	wget -qs "${VERAURL}SetStatus2&newStatus1=1"
fi
fi

if [ -z "$a2_2ghz_new" ] && [ -z "$a2_5ghz_new" ] ; then
wget -qs "${VERAURL}SetStatus1&newStatus1=0"
fi


done
 
Old 07-17-2016, 11:05 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
It's not obvious to follow all the if and their closed fi because of not well formated code

Could you fix the indentation? Thanks
 
Old 07-17-2016, 11:21 AM   #3
notchy
LQ Newbie
 
Registered: Jul 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
It's not obvious to follow all the if and their closed fi because of not well formated code

Could you fix the indentation? Thanks
@keefaz - please bare with me, I am not a coder and just copied the code and made revisions to my environment.
I see for every 'if' there is a 'fi'. Do you want me to indent all the 'if' statements ?
 
Old 07-17-2016, 12:57 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am a little confused at exactly what work you have done to convert this script yourself?

1. Have you looked at what the differences are between ash and bash online?

2. Have you looked up any tutorials about ash to give you some ideas on what constitutes a valid line of code for if, while, process substitution, parameter substitution and arithmetic operations? (all of which are used in your bash code)

3. Your token efforts so far seem to be:
a - You altered the interpreter from bash to sh (which if not a symbolic link to ash will not make this an ash script)
b - You say you put in # marks to make up for an error about blank lines, yet I still count 14 blank lines ... so why do these ones not error?

4. The formatting referred to by keefaz is to make your code readable and easily identify where your control structures (if and while) begin and end. Without these it is like looking through a haystack to find what should be obvious

5. Another formatting inconsistency, which may or may not matter, is that some of your if tests place the semi-colon immediately after the test (ie. if [ some_test ] while others leave a space (ie. if [ some_test ] ; ) ... maybe ash doesn't like one of these versions??

6. Have you tested any of the code individually to perhaps assist in tracking down the errors?


As you can see from the above, there would appear to have been little actual attempt made by yourself to solve this issue apart from being given code.

You need to look into most of the above if you are to understand not only the code you are trying to alter, but if those providing solutions are actually assisting or not.
 
Old 07-17-2016, 03:52 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by notchy View Post
When I run the script it complains about blank lines so I inputted '#' as comments and that seems to make those error messages go away but I am struggling with the last error msg.
Did you edit the script on a windows editor? Maybe it has Windows line endings? What's the exact error messages you get when it "complains about blank lines"?
 
Old 07-17-2016, 05:20 PM   #6
notchy
LQ Newbie
 
Registered: Jul 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
I used notepad++ and saved the file as encoded UTF-8
 
Old 07-17-2016, 06:29 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
To repeat myself:
Quote:
Originally Posted by ntubski View Post
What's the exact error messages you get when it "complains about blank lines"?
 
Old 07-17-2016, 10:46 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Even the strictest shell (i.e. dash) allows empty lines in the script, so it must be the line-ends. Use some 'rmcr' or 'dos2unix' program.
 
Old 07-18-2016, 12:01 PM   #9
notchy
LQ Newbie
 
Registered: Jul 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
The error msg I get when I run it with blank lines in the scripts is


: not foundes01: line 14:
: not foundes01: line 15:
 
Old 07-18-2016, 12:23 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
using notepad++ you can set unix style instead of windows format to save that file (I can't recall the name of that menu, but you definitely should save in unix format).
 
Old 07-18-2016, 12:25 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Well, corrupted looking messages like that are typical when having Windows line endings.

Here's how to change line endings in Notepad++ (you want to click the "UNIX/OSX Format" entry): http://www.larshaendler.com/2013/06/...-line-endings/
 
Old 07-19-2016, 06:59 PM   #12
notchy
LQ Newbie
 
Registered: Jul 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Well, corrupted looking messages like that are typical when having Windows line endings.

Here's how to change line endings in Notepad++ (you want to click the "UNIX/OSX Format" entry): http://www.larshaendler.com/2013/06/...-line-endings/

@ntubski - THank you so much, the issue was the EOL not saved in Unix. I followed the instructions from the url that you posted and was able to have this running on the router.

Thank you everyone for your assistance. I am certainly greatful.
 
Old 07-20-2016, 11:40 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution.
 
  


Reply



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
convert a bash to ash mdijoux25 Programming 6 02-25-2015 12:31 PM
how to convert bash script to binary? dwarf007 Programming 3 07-24-2012 07:07 AM
convert bash script to sh script? parsbin Programming 4 07-21-2012 09:26 PM
[SOLVED] Bash to ash: Help converting a script? Mouse750 Programming 7 09-08-2011 01:32 PM
Convert a Bash script to C Linh Programming 5 04-25-2004 12:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 PM.

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