LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 11-07-2010, 05:43 AM   #1
binbash
LQ Newbie
 
Registered: Sep 2010
Posts: 12

Rep: Reputation: 0
simple if's in bash scripts?


I have been looking at: http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

but a lot of it is to do with files and numerical comparisons

what would be the bash equivalent of:

if (http isin $2) { do something }

thanks
 
Old 11-07-2010, 07:53 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not sure what you want it to do exactly, but an if statement is really quite simple.
Code:
if <test>; then

  commands

fi
<test> can actually be any command. What if looks at is the exit code of the command used. If the exit code is 0, then the subsequent commands are executed. The most common command used here is, of course, test, which simply exits with 0 if the expression inside it evaluates as true. The [ ] brackets are actually a synonym for test.

So if you want to know whether a variable holds a certain string, then simply use a string comparison.
Code:
if [ "$var" = string ]; then
It's usually best to quote variables, which preserves whitespace and reserved characters.

However, your isin sounds more like a partial string match, which is a bit more difficult. In that case you usually need to use bash's extended [[ ]] test mechanism. This allows regex matches, among other advanced comparisons.
Code:
if [[ "$var" =~ substring ]]; then
I generally use the extended test for all my scripts, as it's more robust overall.

All of this is explained in the Bash Beginner's Guide page you're looking at. If you want more specific help, you'll have to give us more details.

Last edited by David the H.; 11-07-2010 at 07:54 AM. Reason: fixed tag
 
1 members found this post helpful.
Old 11-07-2010, 11:28 AM   #3
binbash
LQ Newbie
 
Registered: Sep 2010
Posts: 12

Original Poster
Rep: Reputation: 0
yes "partial string match" was what I was looking for

thanks for your help - your suggestion worked
 
  


Reply



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
KDE 4 Autostart Program Bash Script to Write Autostart Bash Scripts for You! jdmcdaniel3 SUSE / openSUSE 1 05-03-2010 06:17 AM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 11:42 AM
Another simple question - cannot execute scripts Stevie-B-242 Linux - Newbie 3 02-18-2008 04:53 AM
simple bash scripts and linking to them kylenewton Programming 4 07-21-2005 10:15 PM
Simple scripts for making isos and burning them on cd! jlturbos Linux - General 3 02-27-2004 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 PM.

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
Open Source Consulting | Domain Registration