LinuxQuestions.org
Visit Jeremy's Blog.
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 09-17-2008, 04:40 PM   #1
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 0
Bash text search query


Hi

I'm new to Bash and I would like to compare a character with a string and if the character exists then perform a function e.g.

If [ 'z' in $var ] #this is where I need help
then
echo "exists"
else
echo "doesn't"
fi

Thanks
 
Old 09-17-2008, 04:54 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Do you mean that yo want to find a match if the value of var contains a 'z' character anywhere in it?

You can use the case statement for this, as it does glob pattern matching:
Code:
case "$var" in
*z*)
    echo "found a z"
    ;;
*)
    echo "didn't find a z"
    ;;
esac
 
1 members found this post helpful.
Old 09-17-2008, 08:41 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Or Regex
Code:
if [[ "$t" =~ "z" ]]
then
    echo "match"
else
    echo "no match"
fi
Requires bash v3 or better
 
  


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
Help with BASH to search text files on disk purveshk Linux - Newbie 5 06-23-2015 03:31 PM
text search and replacement: bash scripting jettachamp26 Programming 35 02-25-2008 07:00 PM
help building a mysql search query Rickead2000 Linux - Software 1 10-17-2007 03:34 PM
MYSQL search query by column name using wildcard AQG Programming 9 08-07-2007 10:41 AM
PHP search query? cdc5205 Programming 2 03-17-2006 09:15 PM


All times are GMT -5. The time now is 12:35 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