Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-26-2011, 08:07 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2010
Posts: 14
Rep:
|
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 
|
|
|
|
09-26-2011, 08:26 AM
|
#2
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
What is that grep line supposed to do? Are you talking about multi-byte characters like UTF-8?
Kevin Barry
|
|
|
|
09-26-2011, 08:27 AM
|
#3
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,691
|
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.
Last edited by druuna; 09-26-2011 at 08:35 AM.
Reason: Removed exception (single quote itself)
|
|
|
|
09-26-2011, 08:27 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,893
|
Some of these characters have a special meaning in regular expressions or grep patterns. Try:
Code:
grep -q "[[:punct:]]" testfile && echo found it
|
|
|
|
09-26-2011, 08:29 AM
|
#5
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
Quote:
Originally Posted by druuna
Code:
$ ./myprogram -g 'foo bar' -i '\$ % & \' ( ) * + , -'
|
Good point, but you can't escape ' like that.
Kevin Barry
|
|
|
|
09-26-2011, 08:33 AM
|
#6
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,691
|
Hi,
Quote:
Originally Posted by ta0kira
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.
|
|
|
|
09-26-2011, 08:35 AM
|
#7
|
|
LQ Newbie
Registered: Oct 2010
Posts: 14
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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 
|
|
|
|
09-27-2011, 05:11 PM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,558
|
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.
Last edited by David the H.; 09-27-2011 at 05:19 PM.
Reason: added additional info
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:59 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|