LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2011, 10:19 AM   #1
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Rep: Reputation: 0
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".
 
Old 07-01-2011, 10:34 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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.
 
Old 07-01-2011, 10:51 AM   #3
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by weibullguy View Post
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
 
Old 07-01-2011, 10:58 AM   #4
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 07-01-2011, 11:03 AM   #5
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 07-01-2011, 12:25 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
Old 07-01-2011, 07:06 PM   #7
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash scripting ZAMO Linux - General 1 01-19-2009 03:25 AM
Bash Scripting Help? jhyland87 Programming 7 12-14-2008 12:58 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Bash scripting pete1234 Programming 1 09-27-2005 02:48 AM
Bash scripting kbeaver Programming 5 07-18-2003 09:35 PM


All times are GMT -5. The time now is 06:05 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration