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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-09-2009
Registered User
 

Join Date: Jan 2009
Posts: 3
How to use shell var for pattern string at KSH

Hi there, In the following test, how to use shell var for pattern, regular expression. I need to accept pattern at argument, use it to pattern matching at shell script.

Test:

Code:
#!/bin/ksh
# name t.sh
exp="a@(a|b)"
touch aa ab ac
echo "\nTest without variable"
echo "---------------------"
ls -1 a@(a|b)
echo "\nTest with with variable(exp=$exp)"
echo "----------------------"
ls -1 $exp
######################################
# test 
######################################
# chmod 775 t.sh
# t.sh 
######################################
# test output
######################################
# Test without variable
# ---------------------
# aa
# ab
#
# Test with with variable(exp=a@(a|b))
# ----------------------
# a@(a|b) not found     # <-- probelm
######################################


Last edited by Franklin52; 09-09-2009 at 08:21 AM.. Reason: Please use code tags!
Sponsored Links
  #2 (permalink)  
Old 09-09-2009
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,220

Code:
ksh-M 93t 2008-11-04$ ./t.sh 

Test without variable
---------------------
aa  ab

Test with with variable (exp=a@(a|b))
----------------------
aa  ab
ksh-M 93t 2008-11-04$ cat t.sh 
#!/bin/ksh
# name t.sh
exp='a@(a|b)'
touch a{abc}
print -- "\nTest without variable"
print -- ---------------------
ls a@(a|b)
print -- "\nTest with with variable (exp=$exp)"
print -- ----------------------
eval ls $exp

  #3 (permalink)  
Old 09-09-2009
Registered User
 

Join Date: Jan 2009
Posts: 3
Thanks

eval ls ${exp} may be more efficient than using a new child sh,
such as ksh -c "ls $exp".

Radoulov, Much thanks for your help.
Sponsored Links
Closed Thread

Bookmarks

Tags
ksh, regexp, variable

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search for a pattern a string? Leo_NN UNIX for Dummies Questions & Answers 12 10-16-2008 02:38 PM
extract a string after a pattern using sed gunaah Shell Programming and Scripting 1 10-11-2008 04:19 PM
Compare string to a pattern jerryte Shell Programming and Scripting 2 01-25-2008 08:37 PM
how to get the last part of a string followed by a pattern bluemoon1 Shell Programming and Scripting 4 09-08-2007 12:10 PM
how to know if a string contains a certain pattern Deanne Shell Programming and Scripting 11 08-09-2007 09:19 PM



All times are GMT -4. The time now is 11:35 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0