LinuxQuestions.org
Visit Jeremy's Blog.
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 04-07-2012, 09:37 PM   #1
violatedsmurf80
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
Question About a Script...


Well I work out most of the problems with this script but I am still having problems with it showing the files in the home directory when yes is inputted and when no is selected it also does not exit the terminal. If someone could show me where I went wrong or if I missed something I would be thankful.

Code:
#!/bin/bash 
Echo –n “May I see come of your files? [yes or no]”
Read yno
Case $yno in 

	    [yY] | [yY] [Ee] Ss] )
                            Echo “$pwd”
                             ;;

                [nN] | [n|N] [O|o])
                            Echo “$exit” ;
                             Exit 1
                              ;;

                 *) echo “$exit”
                        ;;
esac
 
Old 04-07-2012, 10:21 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
For starters, it looks like you typed this up in Word and then posted it here. Second, the case statement pattern matching is not correct. You can get that to work, perhaps someone will show you how, but I find it easier to just convert the input to lower case and test that. Once I did that, it seemed to work just fine.

Code:
#!/bin/bash
echo "May I see come of your files? [yes or no]."
read yno
LOWER=`echo $yno | tr '[:upper:]' '[:lower:]'`
case $LOWER in

     yes )
           echo  "$pwd"
           ls -l
           ;;

     no )
           echo no "$exit" ;
           exit 1
          ;;

     *) echo "$exit"
         ;;
esac
 
2 members found this post helpful.
Old 04-08-2012, 01:43 AM   #3
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
I am with crabboy on the format but like to use bash to alter the case, so sticking with lower case:
Code:
read yno

case ${yno,,} in
...
 
Old 04-08-2012, 08:51 AM   #4
violatedsmurf80
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes it was in word format because I am working off a VM machine. I changed the " to back tick in hopes of being able get $pwd to work but it still does not any advice? Just a question, why was the LOWER=`echo $yno | tr '[:upper:]' '[:lower:]'` add for? I appreciate the help.
 
Old 04-08-2012, 11:54 AM   #5
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
The changing to lower (or upper as you wish) is to remove the issue of not knowing what case the user may type their answer in.

I am not sure what is not working for you know? Did you try the code shown by crabboy?
 
Old 04-08-2012, 12:15 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Both crabboy's and grail's code convert the contents of yno to lowercase before it is tested. This makes the glob patterns in the case statement easier.

crabboy's technique pipes the content through the external tr command, while grail's uses bash's built-in parameter substitution (case manipulation was included in version4).


I personally wouldn't bother worrying about matching multiple patterns, and just simply test the first character of the string:

Code:
case ${yno,,} in

	y*) echo "$PWD" ;;
	n*) echo "$exit" ; exit 1 ;;
	 *) echo "“$exit" ;;

esac
$PWD (capitalized) is a shell built-in variable, by the way. $pwd is not, but pwd is a shell command that prints the same info.

Another option is simply to tell read to only accept a single character of input, with the -n option. You can also use the -p option to supply the prompt, instead of echoing it separately.

Code:
read -p 'May I see some of your files? [y/n]: ' -n 1 yno
echo

case ${yno,,} in

     y) echo "$PWD" ;;
     n) echo "$exit" ; exit 1 ;;
     *) echo "$exit" ;;

esac

PS: $(..) is highly recommended over `..`

Last edited by David the H.; 04-08-2012 at 12:17 PM. Reason: minor errors and addition
 
1 members found this post helpful.
Old 04-08-2012, 10:09 PM   #7
violatedsmurf80
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
David the H,

The way you explained it and the link help me a lot to understand this.

I know I still have a lot to learn but I am sure with time I will be able to write scripts better. Maybe even move in to python to write them. Or is that anther bear that I should wait on?
 
  


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-script] A question about using grep in the script thomas2004ch Linux - Software 2 03-05-2012 04:27 AM
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 10:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
script question paul_mat Linux - Networking 1 04-18-2006 01:01 AM
some script-question G.P.P. Linux - Newbie 3 01-11-2003 06:41 PM


All times are GMT -5. The time now is 01:46 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