LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-13-2009, 10:37 AM   #1
Sebbern
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
Question Bash script to check if string1 contains string2


Help!

I'm going to make a bash script that checks if string1 contains string2.

So far i've got:

Code:
#!/bin/bash

input=$(echo $@)
if [ input == .txt ]
then
echo "yes"
else
echo "no"
fi
But that dosen't work!

Please help
 
Old 12-13-2009, 10:47 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Some notes about your script:

1) you don't really need the assignment
Code:
input=$(echo $@)
since you can use $@ directly in the expression inside the if/then construct.

2) you forgot to put a $ sign before the variable name inside the expression:
Code:
if [ $input == .txt ]
without that you just evaluate the equivalence between two strings, which is obviously always false.

3) if using bash version 3 you can try the operator for Regular Expression Match so that your script will look like
Code:
#!/bin/bash
if [[ $@ =~ [.]txt ]]
then
  echo yes
else
  echo no
fi
 
Old 12-13-2009, 08:21 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use case/esac

Code:
string1="abcdef"
string2="abc"
case "$string1" in
    *$string2* )
        echo "found"
        ;;
    *) echo "not found"
esac
 
1 members found this post helpful.
Old 12-14-2009, 01:05 AM   #4
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
awk implementation of the same.
BEGIN {
string1="abcdef"
string2="abcd"
if(match(string1,string2))
{
print "found"
}
else
{
print "not found"
}
}
 
Old 12-14-2009, 01:38 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you should put your code in code tags.
Or this as well:
Code:
BEGIN {
  string1="abcdef"
  string2="abcd"
  if( string1 ~ string2){
      print "found"
  }else{
      print "not found"
  }
}
 
1 members found this post helpful.
Old 12-15-2009, 05:19 AM   #6
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
oh, ok I will do that in future . thanks for the info ghostdog :-)
 
  


Reply

Tags
bash, help, if, unix


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
awk: get lines starting from String1 and finish String2 frenchn00b Programming 9 10-29-2007 09:16 AM
Shell: string1 += string2. How to do this? jhwilliams Linux - Software 6 06-19-2007 02:49 PM
bash script to check mount is ok procfs Programming 2 01-17-2007 07:19 PM
how to have bash script check whether it's a x session? bkeeper Linux - Newbie 9 02-21-2006 07:31 AM
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 02:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:56 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
Open Source Consulting | Domain Registration