LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Special Characters in program argument (http://www.linuxquestions.org/questions/programming-9/special-characters-in-program-argument-905060/)

rpcaldeira 09-26-2011 08:07 AM

Special Characters in program argument
 
Hello, I'm making a bash script that receives arguments in the command prompt like so:

$ ./myprogram -g arg -i anotherarg

but the arguments cannot contain any special characters at all.

I tried the test capability in bash and some kinds of grep but I can't seem to figure it out.

Can anyone help me?

#My Test File
rpcaldeira@MacBook:~$ cat testfile
dsfja 2389 91235123%!"#!" #&#$6 !$#& # /!#$/ 27 "47237 2437 -<<<><

#My attempt in grep
rpcaldeira@MacBook:~$ grep -q "! # \$ % & \' ( ) * + , - . / : ; & < = > ? @ [ \\ ] ^ _ { | } ~" testfile && echo found it
rpcaldeira@MacBook:~$

Thanks alot in advance ;)

ta0kira 09-26-2011 08:26 AM

What is that grep line supposed to do? Are you talking about multi-byte characters like UTF-8?
Kevin Barry

druuna 09-26-2011 08:27 AM

Hi,

Putting single quotes around the input should do the trick. It, using single quotes, prevents bash from doing anything with the characters, where putting double (or no) quotes around the input doesn't.

Code:

$ ./myprogram -g 'foo bar' -i '\$ % &  ( ) * + , -'
Hope this helps.

colucix 09-26-2011 08:27 AM

Some of these characters have a special meaning in regular expressions or grep patterns. Try:
Code:

grep -q "[[:punct:]]" testfile && echo found it

ta0kira 09-26-2011 08:29 AM

Quote:

Originally Posted by druuna (Post 4482542)
Code:

$ ./myprogram -g 'foo bar' -i '\$ % & \' ( ) * + , -'

Good point, but you can't escape ' like that.
Kevin Barry

druuna 09-26-2011 08:33 AM

Hi,
Quote:

Originally Posted by ta0kira (Post 4482544)
Good point, but you can't escape ' like that.
Kevin Barry

True!

All, but the single quote (').

Although I'm not 100% sure what the OP wants, using [:punct:] (colucix's answer) might be best.

rpcaldeira 09-26-2011 08:35 AM

Quote:

Originally Posted by colucix (Post 4482543)
Some of these characters have a special meaning in regular expressions or grep patterns. Try:
Code:

grep -q "[[:punct:]]" testfile && echo found it

Thanks colucix that did the trick ;)

David the H. 09-27-2011 05:11 PM

There are several other regex character classes available. There's a section in the grep info page that documents all of them and what characters they cover.

The real point of the above solution though is not the character class, but the character range construct, which allows you to test for any of a list of individual characters. All a character class is is just a pre-defined range of characters.

As an example, to test for only a subset of punctuation marks, plus spaces and tabs, plus digits, you could use this:

Code:

grep -q "[./:;<=>?[:blank:][:digit:]]" testfile && echo found it

The bash [[ extended test can also handle regex, of course. You could use this to test for the existence of punctuation in input arguments, for example:
Code:

re='[[:punct:]]'

for arg in "$@"; do
        [[ $arg =~ $re ]] && echo "found punctuation in [$arg]"
done

It's usually best to store the regex pattern in a separate variable first. Otherwise you'll have to worry about escaping shell-reserved characters.


By the way, please use [code][/code] tags around your code, to preserve formatting and to improve readability.


All times are GMT -5. The time now is 09:01 AM.