0

The code below is just a rough outline of what I am trying to do. Don't worry about what the code is doing after each case is evaluated.

My question is that in the case *','* ) I am trying to recognize a dynamic pattern of files separated by commas.

Ex: getfile.sh -fileextension file1,file2,file3,file4 would be my input. How would i go about recognizing that the input from $2(this is equal to var2) follows the *','* pattern where * represents the file?

    if [ $flag -eq 1 ]; then
            case $var2 in
                #lists files if option is selected
                list | l | LIST | L | ls | LS)      do stuff here
                all | a | ALL | A)                  do stuff here
                *','* )                             do stuff here
                * )                                 do stuff here
            esac
        fi

I should add that the program is supposed to list a set of files based on their extension and then allow the user to either get all those files, or select mutliple (or a single) file. Because i will not know what the user is going to input i'm trying to match a pattern instead of a static input.


So here is what my new code looks like and is much closer to what i'm trying to accomplish:

Let's jsut concentrate on the case so as to not get confused.

case $var2 in
        #lists files if option is selected
        list | l | LIST | L | ls | LS)      ls -1 $DIRECTORY*$extension
                        exit
                        ;;
        #copy all files in the directory
        all | a | ALL | A)  
                        echo ">Cleaning temp dir"
                        allcheck=1
                        cleantemp   
                        copy
                        zip
                        exit
                        ;;
        [0-9][0-9a-zA-Z] | [0-9]','[0-9a-zA-Z]) echo "this is valid input"
                        exit
                        ;;
        * )             
                        echo ">>Bad argument"
                        #help
                        exit 1
                        #fi
    esac

This is the pattern i'm trying to match

[0-9]','[0-9a-zA-Z]

I figured it out after finding a link that gave me a clue: How do I test if a variable is a number in Bash?

Anyways here's what i ended up with:

case $var2 in
        #lists files if option is selected
        list | l | LIST | L | ls | LS)      ls -1 $DIRECTORY*$extension
                        exit
                        ;;
        #copy all files in the directory
        all | a | ALL | A)  
                        echo ">Cleaning temp dir"
                        allcheck=1
                        cleantemp   
                        copy
                        zip
                        exit
                        ;;
        [0-9][0-9a-zA-Z] | [0-9][0-9a-zA-Z],[0-9][0-9a-zA-Z],*) echo "this is valid input"
                        exit
                        ;;
        * )             
                        echo ">>Bad argument"
                        #help
                        exit 1
                        #fi
    esac

This test is for input such as 56 or 5A [0-9][0-9a-zA-Z] This test is for input that is separated by a comma such as what i was tryign to accomplish. [0-9][0-9a-zA-Z],[0-9][0-9a-zA-Z],*

(I will still have to make sure invalid characters are not entered, however, it really cut down on the work i have to do now.)

Thanks for trying to help 'the other guy'.

Community
  • 1
  • 1
tc90
  • 297
  • 2
  • 5
  • 16
  • It's unclear to me what your question is. Are you trying to figure out how to distinguish something with a comma from something without a comma? – Richard Hansen Jul 04 '13 at 02:37

1 Answers1

0

Your code correctly matches values of var2 containing commas:

flag=1
var2="file1,file2,file3,file4"
if [ $flag -eq 1 ]; then
    case $var2 in
        #lists files if option is selected
        list | l | LIST | L | ls | LS)      stuff here ;;
        all | a | ALL | A)                  stuff here ;;
        *','* )                             echo "It works" ;;
        * )                                 stuff here ;;
    esac
fi

will print "It works".

If the problem is how to separate a comma separated list, you can use

IFS=","
for file in $var2
do
    echo "Processing $file"
done
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Sorry, that's not what i'm looking for. I just found something almost to the extent that i'm looking for. Will update soon, maybe you can help me out then. – tc90 Jul 03 '13 at 17:17
  • Feel free to edit your question to clarify if what you're doing is not actually "recognizing that the input from $2(this is equal to var2) follows the `*','*` pattern". – that other guy Jul 03 '13 at 17:19
  • I'm not trying to recognize if $2 -eq var2. They are set to equal one another. I'm trying to compare $var2 with potential cases to take appropriate action. sorry for the confusion. – tc90 Jul 03 '13 at 17:22
  • To see whether "$2" is equal to the contents of the variable "$var2", use `if [[ "$2" = "$var2" ]]; then ...`. To check whether it's equal to the literal string "var2", use `if [[ "$2" = "var2" ]]; then ...` – that other guy Jul 03 '13 at 18:39