PDA

View Full Version : Shell - how do you check for * in the input string



akvino
March 18th, 2009, 02:48 PM
I am not sure if this is possible, but how would you check for * in the input string when calling executable.(C shell or Bash shell)

Exmp myprogram * - is there a way to check for a wildchar-star in the input string?

geirha
March 18th, 2009, 02:53 PM
No, the program never sees the *. The shell expands the * to all files in the current directory and then executes the program.

If you have a directory with

$ ls
file1.txt file2.txt myprog

And run:
./myprog *.txt, then the shell first replace *.txt with file1.txt and file2.txt, so the command actually run is equivalent to running

./myprog file1.txt file2.txt

So with "./myprog *.txt", argv[1] and argv[2] of your program will contain the filename of file1.txt and file2.txt.

And if you actually want to pass the symbol * in an argument, escape it or quote it:

./myprog \*.txt
# or
./myprog "*.txt"

rendon
March 18th, 2009, 04:13 PM
agreed, I think you'd have to find a work-around test case for such a scenario.

Something like this will tell you if ONLY a * was passed



#!/bin/bash

all_args=${@}
wildcard=*
stuff=`echo $wildcard`



if [ "$all_args" == "$stuff" ]
then echo "wildcard"
else echo "no wildcard"
fi



and you might play with that a little to get your desired result

ghostdog74
March 18th, 2009, 04:22 PM
I am not sure if this is possible, but how would you check for * in the input string when calling executable.(C shell or Bash shell)

Exmp myprogram * - is there a way to check for a wildchar-star in the input string?

before executing your script, you can set noglob


# set -o noglob
# myprogram *

however your wildcard expansion will be disabled.

akvino
March 18th, 2009, 05:21 PM
noglob -hmmm

can this be set within the program and then unset at the end of the program?

akvino
March 18th, 2009, 05:22 PM
agreed, I think you'd have to find a work-around test case for such a scenario.

Something like this will tell you if ONLY a * was passed



#!/bin/bash

all_args=${@}
wildcard=*
stuff=`echo $wildcard`



if [ "$all_args" == "$stuff" ]
then echo "wildcard"
else echo "no wildcard"
fi



and you might play with that a little to get your desired result


Thanks will try it.

snova
March 18th, 2009, 07:55 PM
noglob -hmmm

can this be set within the program and then unset at the end of the program?

Yes, but it won't do any good. The point is to prevent the shell from processing the * automatically. Setting it within the script is too late.

Actually testing for the character shouldn't be difficult (an example has already been posted), it's escaping it so the program sees it that is tricky. The simplest solution is probably just to escape or quote the character in the first place, as geirha suggested at the end of his post.