LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question About a Script... (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-a-script-938688/)

violatedsmurf80 04-07-2012 08:37 PM

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


crabboy 04-07-2012 09:21 PM

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


grail 04-08-2012 12:43 AM

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
...


violatedsmurf80 04-08-2012 07:51 AM

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.

grail 04-08-2012 10:54 AM

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?

David the H. 04-08-2012 11:15 AM

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 `..`

violatedsmurf80 04-08-2012 09:09 PM

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?


All times are GMT -5. The time now is 11:26 AM.