The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
anyone ever play with omniback(data protector) command "omnir"? mr_manny HP-UX 1 02-01-2008 11:10 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
Please Help (Urgent) - Comparing 2 files with "awk" zom_chp Shell Programming and Scripting 10 03-30-2005 07:34 AM
Error : "No data written to object that was write locked" nileshkarania UNIX for Dummies Questions & Answers 1 06-07-2003 11:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-20-2008
rssrik rssrik is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
comparing scalars contaning "DOUBLE QUOTES" as data

Hello to all,

Does anyone know the solution ?

Two strings A and B are present. I want to check whether B is a Substring of A.

1. The value of A is - 29 * * * /bin/ls "test" "tmp*" "log*"
(Note: Pl note that A contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

2. The value of B is - /ls "test" "tmp*" "log*"
(Note: again B contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

Since the variable scalars A and B have DOUBLEQUOTES as a part of the data stored "=~" operator is not working.

[ $A =~ $B ] - results in error..

Is there any other way (any sed/awk/perl one liners). Requesting help to solve this.

Thanks
rssrik
  #2 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
First of all, you need to learn to quote arguments properly. Simply double-quote anything with a variable in it, and you should be fine, most of the time.

If the strings also contain regex specials (in this case, asterisks), those need to be escaped somehow, or you need to use a different comparison function. May I suggest the plain old case statement?

Code:
case $A in *"$B"*) echo "$A contains $B";; *) echo does not;; esac

Last edited by era; 04-20-2008 at 08:12 AM.. Reason: Had $A and $B mixed up
  #3 (permalink)  
Old 04-20-2008
rssrik rssrik is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
thanks .. it works

just wondering how come double quotes are escaped properly in case statement ! while [ "$A" =~ "$B" ] does not work ! seems like I have lot of catching up to do in shell basics ..

Thanks again era
rssrik
  #4 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The double quotes are not the problem, it's that you are using the regular expression match operator on something which is just a string, and which happens to contain characters which have a special meaning in regular expressions. " *" (space asterisk) matches zero or more spaces in a regular expression, but doesn't match a literal asterisk. "log*" matches "lo", "log", "logg", "loggg", "logggg" etc but doesn't match "log*".
  #5 (permalink)  
Old 04-20-2008
rssrik rssrik is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
Gotcha !

Thanks to you "ERA"...
  #6 (permalink)  
Old 04-20-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,926
You can also use a conditional expression to test if B is a substring of A i.e.
Code:
$!/usr/bin/ksh93

# ANSI C string quoting!
A=$'29 * * * /bin/ls "test" "tmp*" "log*"'
B=$'/ls "test" "tmp*" "log*"'

[[ ${A} == *${B}* ]] && print "Yes, B is a substring of A"
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0