LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-26-2011, 08:07 AM   #1
rpcaldeira
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Rep: Reputation: 0
Question 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
 
Old 09-26-2011, 08:26 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What is that grep line supposed to do? Are you talking about multi-byte characters like UTF-8?
Kevin Barry
 
Old 09-26-2011, 08:27 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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)
 
Old 09-26-2011, 08:27 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Some of these characters have a special meaning in regular expressions or grep patterns. Try:
Code:
grep -q "[[:punct:]]" testfile && echo found it
 
Old 09-26-2011, 08:29 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Code:
$ ./myprogram -g 'foo bar' -i '\$ % & \' ( ) * + , -'
Good point, but you can't escape ' like that.
Kevin Barry
 
Old 09-26-2011, 08:33 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 09-26-2011, 08:35 AM   #7
rpcaldeira
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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
 
Old 09-27-2011, 05:11 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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
 
  


Reply

Tags
bash, characters, find, grep, special


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
program regarding special characters in c++ ajcapri Programming 5 12-04-2009 12:47 AM
Special Characters SimeonV Suse/Novell 14 07-07-2006 01:29 PM
special characters greenbox Linux - Software 9 12-23-2005 07:33 PM
Special characters consty Programming 3 08-07-2005 05:53 AM
using special characters one_ro Mandriva 5 11-04-2004 08:52 AM


All times are GMT -5. The time now is 12:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration