The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleteing one character after an special character mohsin.quazi Shell Programming and Scripting 1 4 Weeks Ago 04:27 PM
Special character \ amit_arora Shell Programming and Scripting 7 09-10-2009 07:36 AM
check for a particular character inside a file and substitute with a given character? karthikprasathk AIX 1 07-01-2008 03:29 AM
Special Character Check in Shell script mradul_kaushik Shell Programming and Scripting 1 03-24-2006 05:54 PM
special character ? mile1982 High Level Programming 1 10-19-2004 08:15 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
lavnayas lavnayas is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 6
How to check for special character in a value

Hi,

I have a variable and to it always alphanumeric value will be assigned.

If the value has any special characters in it then in the if statement it should exit like below

if (value has any speacial character)
then
exit
else
....
fi

can any one suggest how to acheive this?
  #2 (permalink)  
Old 1 Week Ago
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
something like:

Code:
#   var=1234a ; [ ! $(echo $var|grep "[^0-9]") ] && echo $var

#   var=12345 ; [ ! $(echo $var|grep "[^0-9]") ] && echo $var
12345
chould get you on the right path
  #3 (permalink)  
Old 1 Week Ago
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Did not work for me , might be the issue with the shell

how ever with out negate worked fine.

Code:
TES>var=1234; [ ! $(echo $var | grep "[^0-9]") ] && echo $var
sh: test: Specify a parameter with this command.
Code:
TES>var=1234a; [  $(echo $var | grep "[^0-9]") ] && echo $var
1234a
  #4 (permalink)  
Old 1 Week Ago
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Quote:
Originally Posted by lavnayas View Post
I have a variable and to it always alphanumeric value will be assigned.

If the value has any special characters in it then in the if statement it should exit
For bash / ksh
Code:
 var='1234a;'
 if [ ! -z ${var//[[:alnum:]]/} ]
	then 
		exit
	else 
		echo OK; # Do something here
 fi
  #5 (permalink)  
Old 4 Days Ago
lavnayas lavnayas is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 6
hi,

thanks. It worked for me..

I have to add one condition while checking for the special characters. That is if there is any comma then it should go to else part. if there is any other special characters other than comma then the script has to exit.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0