Bash to Ash, errors and adjustments


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu Bash to Ash, errors and adjustments
# 1  
Old 06-23-2014
Bash to Ash, errors and adjustments

I wrote Bash script and now I want to convert it to Ash. One headache is this function:

Code:
do_adduser() {
    setaddprompt
    _arr_add=("Add manually" "Add via TXT" "return to main menu" "exit program")
    select add_action in "${_arr_add[@]}"
    do
        case "$REPLY" in
            1) do_manual_add ;;
            2) do_txt_add ;;
            3) return ;;
            4) exit 0 ;;
            *) badchoice ;;
        esac
        setaddprompt
    done
}

This function prints out the options and allows selection. First the ash error handler had me change the brackets as such

Code:
   _arr_add={"Add manually" "Add via TXT" "return to main menu" "exit program"}
    select add_action in "$(_arr_add[@])"

Which I think will be better. But now it is complaining about a missing "}" bracket.

Code:
line 72: syntax error: unexpected "do" (expecting "}")

When I get rid of the do statement, the error goes away. I tried adding a while before hand. It didn't make a difference so it's presently uncommented, and the code is bellow.

Code:
do_adduser() {                                                         
    setaddprompt                                                       
   # _arr_add={"Add manually" "Add via TXT" "return to main menu" "exit program"}
    #select add_action in "$(_arr_add[@])"                                       
    echo "Select Option:                                                         
1) Add manually                                                                  
2) Add via TXT                                                                   
3) Return to Main Menu                                                           
4) Exit Program                                                                  
"                                                                                
   # while[true];                                                                
     do;                                                                         
                                                                                 
        case "$REPLY" in                                                         
            1) do_manual_add ;;                                                  
            2) do_txt_add ;;                                                     
            3) return ;;                                                         
            4) exit 0 ;;                                                         
            *) badchoice ;;                                                      
        esac                                                                     
        setaddprompt                                                             
     done                                                                        
}

Does anybody know how I can change this to an 'ash' script successfully?

---------- Post updated at 06:17 PM ---------- Previous update was at 06:14 PM ----------

To summarize, I can't get the 'do' statement to work.

Last edited by Scrutinizer; 06-24-2014 at 03:48 AM.. Reason: CODE tags
# 2  
Old 06-23-2014
In what way does it "not work"?
# 3  
Old 06-24-2014
Thanks in helping me out.

The do statement needs the following syntax:

Code:
while ...
do ...
done ...

I didn't have a "while" section to it when I was using Bash, and the command prompt just waited for me to enter a key. Now that I require the while statement, I don't know how to replicate that behavior.

I could do
Code:
while read ...
do..
done..

But then the user needs to press enter to continue. That's not as nice.

Last edited by Scrutinizer; 06-24-2014 at 03:49 AM.. Reason: CODE tags
# 4  
Old 06-24-2014
Which ash? busybox? I don't think either allow arrays or implements select ...; do...; done. You'll have to implement your own menu altogether.

Code:
#!/bin/busybox sh
while :; do
        setaddprompt

        printf '%d.) %s\n' 1 "Add manually" 2 "Add via TXT" 3 "return to main menu" 4 "exit program"
        read var
        case $var in
                1) do_manual_add; ;;
                2) do_txt_add; ;;
                3) break; ;;
                4) exit 0; ;;
                *) badchoice; ;;
        esac

        setaddprompt
done

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 06-24-2014
Thank you Scott, your code worked without any modification. I'm still new to scripting.
# 6  
Old 06-26-2014
How do you make it so that, for the case statement, the user doesn't have to press 'enter'. I want it to be as soon as they press "1", for example, they're taken to that menu.

Tricky details..
# 7  
Old 06-26-2014
The version of ash I have appears to support -n:

Code:
$ busybox ash
# IFS="" read -r -n 1 REPLY
q
# echo $REPLY
q
#

It returns immediately after 1 byte is read.

You could also do lots of tricky things with dd and stty, which might work more reliably. From this thread:

Code:
inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 if=/dev/tty 2>/dev/null` ; }

The reason this is more reliable is, if someone hits F1 and dumps \x1b[11~ into your terminal, that'd be one keystroke with inkey() and five keystrokes with read -n.

Also, inkey times out eventually, leaving "char" blank if nothing was typed. The timeout is in tenths of a second, for 1 second it'd be "time 10" for example.

If you gave it an infinite timeout, it might start waiting for entire lines again, terminals are tricky like that.

It works in ash for me, though it mostly depends on whether whatever embedded thing you're using has stty.

Last edited by Corona688; 06-26-2014 at 11:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Ubuntu

Convert a bash to ash

hello everybody, i'm a beginner in ash and i want to convert this bash script to ash. this script send a xml file to a nagios server : #!/bin/bash PROGNAME=$(basename $0) RELEASE="Revision 0.3" print_release() { echo "$RELEASE" } print_usage() { echo "" echo "$PROGNAME... (6 Replies)
Discussion started by: mdijoux25
6 Replies

2. Ubuntu

Bash to ash port, character-matching problem

I'm trying to convert this working bash script into an Ash script, read -p "Username:" _username if ! ]]; then echo "Valid" else echo "INVALID" fi However, Ash does not recognize the "=~" character. How can I do this? Also, is there a good reference guide, so I... (5 Replies)
Discussion started by: fzivkovi
5 Replies

3. Shell Programming and Scripting

Bash Script to Ash (busybox) - Beginner

Hi All, I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder. But... (5 Replies)
Discussion started by: sgtbobie
5 Replies

4. Shell Programming and Scripting

Bash script errors when executing

I'm working on a script that will search through a directory for MKV files and remux them to MP4. I found a few other scripts around the net that do this, but they all have limitations that don't always get the job done. The main limitation I've found is that most scripts look for the video track... (2 Replies)
Discussion started by: rayne127
2 Replies

5. UNIX for Dummies Questions & Answers

Differences in BASH and ASH shells regarding if command?

Guys I now have a script that's working in a BASH environment, however one line doesn't appear to be working on an embedded device that has a busybox therefore ASH shell. I've googled but there's very little I can find regarding the ASH shell. In BASH the following line works... if ] ;... (6 Replies)
Discussion started by: Bashingaway
6 Replies

6. Shell Programming and Scripting

Errors in bash with if statements

Hello everyone, I got this type of error when programming in bash new.bat: 16: cannot create : Directory nonexistent $bool new.bat: 37: Syntax error: "then" unexpected (expecting "fi") Does anyone know why? Here is my code #!bin/bash #function helps(){... (4 Replies)
Discussion started by: bbbash
4 Replies

7. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

8. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

9. UNIX for Advanced & Expert Users

Bash/Unix Command Errors

Hi all, I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: # root@host # pwd /bin... (0 Replies)
Discussion started by: wcmmlynn
0 Replies
Login or Register to Ask a Question