LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-23-2008, 06:28 AM   #1
chochem
LQ Newbie
 
Registered: May 2008
Posts: 29

Rep: Reputation: 15
ash test: is string A contained in string B


I'm trying to port a bash script to a busybox setup. Since busybox uses ASH rather than BASH, some things don't work. IN particular:
Code:
[[ "ab bc de" =~ "ab" ]]
tests if ab is contained within "ab bc de" in Bash. I'm stumped as to how to do something similar in Ash (if at all possible). Any suggestions?
 
Old 09-23-2008, 07:24 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could use grep like: if echo "ab bc de" | grep "ab"; then...
 
Old 09-23-2008, 07:25 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'll bite---what is ASH? Does it have a manual?
Quote:
Since busybox uses ASH rather than BASH
Why?
 
Old 09-23-2008, 07:50 AM   #4
chochem
LQ Newbie
 
Registered: May 2008
Posts: 29

Original Poster
Rep: Reputation: 15
D'oh, grep... Yeah that should do it, although I'm trying to avoid calling too many outside programs (the computing power on this thing is laughable).

pixellany: http://en.wikipedia.org/wiki/Ash_shell
 
Old 09-23-2008, 08:31 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by chochem View Post
D'oh, grep... Yeah that should do it, although I'm trying to avoid calling too many outside programs (the computing power on this thing is laughable).[...]
Use case:

Code:
case $string in
  *ab*) return 0 ;;
     *) return 1 ;;
esac
 
1 members found this post helpful.
Old 09-23-2008, 09:21 AM   #6
chochem
LQ Newbie
 
Registered: May 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov View Post
Code:
case $string in
  *ab*) return 0 ;;
     *) return 1 ;;
esac
Very impressive - I'd nver have thought of that. Thanks, radulov
 
Old 09-24-2008, 06:59 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I wonder if the spaces in your string ("ab bc de"), I will call it "S", indicate that you are mentally dividing it into words & assuming that your pattern (P="ab") is being tested for as a word? All the solutions discussed so far will give a positive when S="abbcde" or S="zab bc de". Is this what you intend?

The bash & grep solutions can narrow the match by using the regex word boundary symbol "\b" (see the grep man page):
Code:
[[ "$S" =~ "\b$P\b" ]]
echo "$S"  | grep "\b$P\b"
,
but the case method (brilliant suggestion, BTW) needs a more complicated solution:
Code:
case $S in
  "$P "*|*" $P "*|*" $P")   return 0 ;;
                       *)   return 1 ;;
esac
(Forgive the double pun -- I couldn't resist) It's a case of considering the corner cases.

It may well be that your actual string is very well structured & you don't need to consider this.
HTH someone.
 
  


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
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

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

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