Wildcards and quoting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Wildcards and quoting
# 1  
Old 09-11-2002
Wildcards and quoting

Hi All

In a script, I want a user to enter 4 characters, these can be a mix of letters (uppercase and lowercase) and numbers.

In this example $var represents what the user has entered.

eg $var can be A9xZ, 3DDL, bbHp .........etc

I need to check that the user has only entered characters so I added the following code:

# Check for invalid characters
echo "$var" | grep [^a-zA-Z0-9] >/dev/null
if [ $? -eq 0 ]
then
echo "$var is not valid."
fi

This appears to work ok unless the user enters a wildcard character.

For example if the user enters bin*, the script expands the * and makes $var equal to all files in the current directory which start with bin.

So $var is "bin bindir binary"

My question is how can I process $var literally as "bin*" in the above grep statement?

Thanks for your help
Helen
Smilie
# 2  
Old 09-11-2002
Change this line in your code from:

echo "$var" | grep [^a-zA-Z0-9] >/dev/null

to:

echo "$var" | grep "[:alnum]\{4\}">/dev/null
# 3  
Old 09-16-2002
Hammer & Screwdriver

Thanks for your reply Photon, I'll give it a try Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Bash quoting

I am trying to write a BASH script that will prompt a user to enter a number of days, then calculate the date. My problem is the date command uses single or double quotes. For Example.. date -d "7 days" Here is an example of some same code I am trying to work through. echo "when do you... (4 Replies)
Discussion started by: javajockey
4 Replies

2. Shell Programming and Scripting

Osascript quoting issue

I am at a loss on this one. I am trying to run this command on a bunch of (OS 10.7.4) macs:osascript -e "tell application \"System Events\" to return name of every process whose frontmost is true" On some, it works fine. On others, I get this error: I have also tried (note the single quotes):... (3 Replies)
Discussion started by: nextyoyoma
3 Replies

3. Shell Programming and Scripting

Quoting the values in second field

Hi, I have got a file comp_data containing the below data : 38232836|9302392|49 39203827|8203203,3933203|52 72832788|567,3245,2434324|100 This file can have many rows like shown above. I want the values separated by "," in second column(taking "|" as delimiter) to be in quotes. These... (2 Replies)
Discussion started by: msabhi
2 Replies

4. Shell Programming and Scripting

Quoting Characters

I have this data how do i add ' ' to them like '-AAL00L' , '-BBE4577' , 'ABC' -AAL00L -BBE4577 ABC (5 Replies)
Discussion started by: dinjo_jo
5 Replies

5. Shell Programming and Scripting

csh quoting enigma

(no, i can't switch shells, although i'd love to. yes, i have read http://www.grymoire.com/Unix/CshTop10.txt.) i finally managed to get an alias working the way i want it to, but i don't understand how one part of it works (highlighted in red): alias log ' \\ set x=`echo \!:1 | /bin/sed... (1 Reply)
Discussion started by: acheong87
1 Replies

6. Shell Programming and Scripting

quoting question

hi guys, i have a question related to quoting but i am not sure how to formulate it... lets say we want to simulate the following shell actions cd ~/project-dir ctags /home/work/folder1/*.sh /home/work/folder2/*.sh /home/work/folder3/*.sh so i make the following script buidtags.sh ... (2 Replies)
Discussion started by: aegis
2 Replies

7. Shell Programming and Scripting

BASH quoting behavior

The block below isn't a surprise:$ ls file1 file2 file3 $ x=* $ echo $x file1 file2 file3 $ echo '$x' $x $ echo "$x" * $But I found this block a bit bewildering:$ echo $x' >' * $I'm wondering why substitution wasn't performed on the $x, since it was unquoted (as far as I can tell).... (5 Replies)
Discussion started by: na5m
5 Replies

8. Shell Programming and Scripting

quoting in conditional statement

can somebody help, what quote i should use in below statement or what wrong of it ? the 1st (*) is a char, the 2nd and 3rd (*) is a wildcard if ] && ] && ] ................^ .............^ then echo "ok" fi thanks in advance. (2 Replies)
Discussion started by: 3Gmobile
2 Replies

9. Shell Programming and Scripting

*.pm globs without quoting, *.pl doesn't.

Can someone explain the following? I can use find on *.pm without quotes, but find on *.pl makes on error, I need quotes for the second version. What's up with that? $find -name *.pm ./tieProxyStatus/Status.pm $find -name *.pl find: paths must precede expression Usage: find $find... (2 Replies)
Discussion started by: tphyahoo
2 Replies

10. Shell Programming and Scripting

M4 quoting drives me crazy

Hi, I have trouble with quotations of the M4 preprocessor. I want to write a basic makro that removes all spaces and newlines at the end and at the beginning of a string. I tried this: define(`TRIM_END', `patsubst(`$1', `\(\\n\| \)*$', `')') define(`TRIM', `patsubst(`TRIM_END(`$1')',... (0 Replies)
Discussion started by: hindman
0 Replies
Login or Register to Ask a Question