LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-889386/)

implosions 07-01-2011 09:19 AM

Bash scripting
 
Code:

if [[ ! $DISPLAY && $(tty) = /dev/tty1 ]]; then
        read -p 'Do you want to start X? (y/n): '
        case $REPLY in
                [Yy]) printf '%s\n' 'Environment?' ;;
                        case $REPLY in ;;
                                awesome) exec ck-launch-session dbus-launch --exit-with-session startxfce4 ;;
                                xfce4) exec ck-launch-session awesome ;;
                        esac ;;
                [Nn]) break ;;
                *) printf '%s\n' 'Please answer y or n.' ;;
        esac
fi

What does bash want from me? I have a feeling it doesn't like "case $REPLY" in another "case $REPLY".

weibullguy 07-01-2011 09:34 AM

What is the problem you are having (i.e., what errors are being thrown by BASH)? There is nothing that causes REPLY to change from the 'Y' or 'y' you provide when you answer the first question. So you'll never launch anything anyway. The real problem is all the ;; you have. Take a look at http://tldp.org/LDP/abs/html/abs-guide.html#CASEESAC1 You only need the ;; to terminate each block within the case construct, not a ;; at the end of each line.

implosions 07-01-2011 09:51 AM

Quote:

Originally Posted by weibullguy (Post 4401406)
What is the problem you are having (i.e., what errors are being thrown by BASH)? There is nothing that causes REPLY to change from the 'Y' or 'y' you provide when you answer the first question. So you'll never launch anything anyway. The real problem is all the ;; you have. Take a look at http://tldp.org/LDP/abs/html/abs-guide.html#CASEESAC1 You only need the ;; to terminate each block within the case construct, not a ;; at the end of each line.

Does that mean I need to reset $REPLY? If that's the case, how?

Code:

bash: /etc/bash.bashrc: line 33: syntax error near unexpected token `$REPLY'
bash: /etc/bash.bashrc: line 33: `                        case $REPLY in ;;'
bash-4.2$ sudo mousepad /etc/bash.bashrc


implosions 07-01-2011 09:58 AM

Okay, I got the ;; thing solved as you suggested and no errors are shown, but the problem you mentioned again happens, $REPLY is not flushed

Code:

if [[ ! $DISPLAY && $(tty) = /dev/tty1 ]]; then
        read -p 'Do you want to start X? (y/n): '
        case $REPLY in
                [Yy]) printf '%s\n' 'Environment?'
                        case $REPLY in
                                awesome) exec ck-launch-session dbus-launch --exit-with-session startxfce4 ;;
                                xfce4) exec ck-launch-session awesome ;;
                        esac
                        ;;
                [Nn]) break ;;
                *) printf '%s\n' 'Please answer y or n.' ;;
        esac
fi


implosions 07-01-2011 10:03 AM

I love bash, I figured it out, this script works:
Code:

if [[ ! $DISPLAY && $(tty) = /dev/tty1 ]]; then
        read -p 'Do you want to start X? (y/n): '
        case $REPLY in
                [Yy]) read -p 'Environment? '
                        case $REPLY in
                                xfce4) exec ck-launch-session dbus-launch --exit-with-session startxfce4 ;;
                                awesome) exec ck-launch-session awesome ;;
                        esac
                        ;;
                [Nn]) break ;;
                *) printf '%s\n' 'Please answer y or n.' ;;
        esac
fi


grail 07-01-2011 11:25 AM

You could go a little further to check your data being entered:

1. Your first questions allows the user to enter any number of key strokes but you only require 1 (read can limit this)

2. No need for multiple options of upper and lower case. Have bash force the answer to one or the other (, ,, ^ ^^ ... the options prior are clues)

3. What are my choices of Environment?? What if I say 'gnome' or Xfce4 or AwEsOmE ?? (silly some of these but I do not know your target audience) { you may even want to look at select so
use can choose an option }

4. As part of 3, there is no catch all rule so as opposed to entering the wrong n or y, now nothing launches and there is no message to say why?

Just some ideas.

implosions 07-01-2011 06:06 PM

Quote:

Originally Posted by grail (Post 4401514)
You could go a little further to check your data being entered:

And then I would end up with a command line login manager, now we don't want that, do we?


All times are GMT -5. The time now is 04:09 PM.