LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-22-2015, 07:33 AM   #1
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Rep: Reputation: Disabled
if statement for non alphanumeric character


Afternoon forumer's

Im trying to have a if statement perform a check to throw out a string that contains alphanumeric

so anything like ^*&(*"

ive been racking my brain with if statements and checks like

if [ $string = "\^|\*" ]
then
echo "string contains incorrect characters"
fi

however i cant figure it :\
 
Old 05-22-2015, 07:51 AM   #2
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MrLinuxDonnelly View Post
Afternoon forumer's

Im trying to have a if statement perform a check to throw out a string that contains alphanumeric

so anything like ^*&(*"

ive been racking my brain with if statements and checks like

if [ $string = "\^|\*" ]
then
echo "string contains incorrect characters"
fi

however i cant figure it :\

I figured it

if ! expr match "$PROC" "[a-zA-Z0-9][a-zA-Z0-9]*$" > /dev/null
then
echo "Process has non alphanumeric characters"
fi
 
Old 05-22-2015, 07:59 AM   #3
shefla
Member
 
Registered: Oct 2012
Location: France
Distribution: Archlinux
Posts: 36

Rep: Reputation: 5
Yes, the problem was the "=", it checks for a string not for a regular expression.

read string
if [ `echo $string|sed 's/[a-zA-Z0-9]//g'|wc -w` -eq 0 ]
then
echo string ok
else
echo special characters founds
fi
 
Old 05-22-2015, 12:10 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
maybe regex test ?
Code:
if [[ $string =~ [&^*] ]] ; then whatever
 
1 members found this post helpful.
  


Reply


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
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 07:33 PM
[SOLVED] sorting alphanumeric columns captainentropy Programming 5 09-14-2011 02:14 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 09:05 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 01:38 AM


All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration