I found some code to parse command line arguments (is there an easier way? sed always seems so complicated...)

#!/bin/bash

for i in $*
do
	case $i in
    	--files=*)
		FILES=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--default)
		DEFAULT=YES
		;;
    	*)
                # unknown option
		;;
  	esac
done

echo $FILES
echo "File 0:"
echo ${FILES[0]}

My question is, how do I loop through the filenames in FILES if I pass multiple files, ie

./Example.sh --files=*.txt

My example above (trying to access it like an array does not split the string at " ", so FILES[0] is the entire FILES.

Any ideas?

Thanks,

Dave

May I be allowed to direct your attention to the following variant of your script and its corresponding output:

#!/bin/bash

for i in $*
do
    case $i in
    --files=*)
            FILES=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
            ;;
    --default)
            DEFAULT=YES
            ;;
    *)
            # unknown option
            ;;
    esac
done

echo "Files: $FILES"
echo "File[0]: ${FILES[0]}"

$ ./test.sh --files=*.z*
Files: *.z*
File[0]: *.z*

Now, the real turning point appears when you feed the script with a spec matching no files. I added the following to your script:

for i in $FILES
do
        echo $i,
done

and the output is:

*.z*,

Can you deduct what is going on? Hint: Your interface to the Operating System is called "Command Shell" because it protects the Operating System from you, not the other way around...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.