How to use shell var for pattern string at KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use shell var for pattern string at KSH
# 1  
Old 09-09-2009
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!
# 2  
Old 09-09-2009
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  
Old 09-09-2009
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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to replace string in AIX ksh shell

My variable contains the following string I wish to replace \n with "space" so the expected output is: I understand that the /n is not a new linein this case. I'm on AIX using ksh shell. Below is all that I tried. echo $str | sed -e "s#\n# #g"; echo $str | sed -e "s#\n#' '#g";... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

4. Shell Programming and Scripting

Shell Script (ksh) - SQLPlus query filter using a string variable

Using ksh, I am using SQLPlus to execute a query with a filter using a string variable. REPO_DB=DEV1 FOLDER_NM='U_nmalencia' FOLDER_CHECK=$(sqlplus -s /nolog <<EOF CONNECT user/pswd_select@${REPO_DB} set echo off heading off feedback off select subj_name from subject where... (5 Replies)
Discussion started by: nkm0brm
5 Replies

5. Shell Programming and Scripting

ksh - building a var

This works #!/bin/ksh FILE="file.txt" dosumtin () { date >> FILE } for i in {1..5} do dosumtin done cat $FILE But instead of building a file, I want to do the same with a var or an array. That is, to build one that saves all 5 of the subs execution responses in a var or an... (8 Replies)
Discussion started by: popeye
8 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

ksh: what does var=$(command) mean?

hi, i can see in a script it contains var=$( myFile | grep -i err ) why has this person done it like this? why not just var=`myFile | grep -i err` thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

9. Shell Programming and Scripting

${!var} does not work in ksh

Anyone knows why the following function does not work in ksh (it does in bash)? var() # Displays var value; case insensitive { _var="$1" if ; then echo ${!_var} else _var=$(echo "$_var" | tr 'a-z' 'A-Z') echo ${!_var} fi unset _var }$ var home ksh:... (4 Replies)
Discussion started by: victorbrca
4 Replies

10. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question