Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've many notes gleaned from the web and have written bash scripts to manage them.

I've some scripts working that depend on select to make menus.

Question: in a nested menu context, is it possible to revert to a preceding menu within the script ? I've a strong feeling that this may not be possible but am posting this in the hope there may be a way.

To explain what I mean: say the user makes a choice against menu-1 which then determines what shows up in menu-2 which follows. If the user selects "done" in menu-2, is there a loop device to bring the user back to menu-1 ?

The script below contains 3 selects - I'd like to be able to revert from the 3rd one back to the 2nd:

curd=($(ls)); # array

select dirup in ${curd[@]} done 

do


cd $dirup;
    ar=($(ls | awk -F"." '{ print $1 }' | awk -F"-" '{ for(i=2; i<=NF; i++) print $i }' | sort | uniq)); 


select choice in ${ar[@]} done # select list
    do 
        echo "you selected: $choice"
        [[ $choice == "done" ]] && exit 
        ar=($( find . -type f -maxdepth 1 -name "*$choice*" ));
        ((cnt=${#ar[@]}+1))
        select ntit in ${ar[@]} done
            do 
                echo "choice is:[ $REPLY ]"
                for i in $REPLY; # $REPLAY contains numbers
                    do
                      if [ $i -lt $cnt ]
                        then 
                            ((var=$i-1)); 
                            open ${ar[$var]}; 
                        else 
                            exit
                      fi
                    done

            done

      done

done
share|improve this question
    
Ugh. The first ar=... one-liner parses ls; this is not recommended at all. And should you insist on parsing ls after all, use preferably ls | cut -f1 -d. instead: way less typing effort. Caveat: Both your and my solutions will also mistake anything following a . (dot) for an extension. For example, a file called invoice.miller.txt will be shown as "invoice", not "invoice.miller". -- Lastly: For first line, to avoid regular files, use find . -maxdepth 1 -type d. –  syntaxerror Nov 20 at 10:10

1 Answer 1

up vote 1 down vote accepted

Use break instead of exit. Note, though, that the menu is not reprinted when returning to a higher menu.

Example:

select x in nothing inner quit ; do
    [[ $x == quit ]] && break
    if [[ $x == inner ]] ; then
        select y in NOTHING BACK ; do
            [[ $y == BACK ]] && break
        done
    fi
done
share|improve this answer
    
Thanks. So how would you handle "returning to a higher menu" ? Your thoughts on that might help a lot. –  Tom Oct 26 '12 at 16:23
    
@Tom: See the example. –  choroba Oct 26 '12 at 16:41
    
Thanks: the answer is "no". What about if == BACK && function->(uses stored value of inner).... –  Tom Oct 26 '12 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.