|
|
|
|
|
|
| Author |
Message |
William *nix forums Guru Wannabe
Joined: 04 Apr 2005
Posts: 196
|
Posted: Wed Jul 19, 2006 8:04 pm Post subject:
Re: Interpret variables in strings in ksh
|
|
|
<greenhakan@gmail.com> wrote in message
news:1153317834.681930.8790@i42g2000cwa.googlegroups.com...
| Quote: | Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume
that a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and
there can be many variables in sequence: bla?var1??var2?bla
|
This doesn't sound like interpreting, just string extraction.
Here's a hack that seems to work (probably has some issues,
I had to cheat and append ?? to the input string to get it to
drop the trailing /etc, for example). Just a starting point
for you:
#!/bin/sh
testStr='blabla/something/?var1?/somethingelse/?var2?/etc'
set -- `echo "$testStr??" | sed -e 's|[^?]*?\([^?]*\)?|\1 |g'`
echo "String: $testStr"
echo "String contains $# variables:"
while [ $# -gt 0 ] ; do
echo " $1"
shift
done
-Wm |
|
| Back to top |
|
 |
John DuBois *nix forums addict
Joined: 30 Mar 2005
Posts: 61
|
Posted: Wed Jul 19, 2006 3:19 pm Post subject:
Re: Interpret variables in strings in ksh
|
|
|
In article <1153317834.681930.8790@i42g2000cwa.googlegroups.com>,
<greenhakan@gmail.com> wrote:
| Quote: | Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume that
a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and there can
be many variables in sequence: bla?var1??var2?bla
Any ideas?
|
j=bla?var1??var2?bla
oIFS=$IFS
IFS=\?
set -- $j
IFS=$oIFS
while (( $# > 1 )); do
print -- "$2"
shift 2
done
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/ |
|
| Back to top |
|
 |
greenhakan@gmail.com *nix forums beginner
Joined: 19 Jul 2006
Posts: 1
|
Posted: Wed Jul 19, 2006 2:03 pm Post subject:
Interpret variables in strings in ksh
|
|
|
Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume that
a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and there can
be many variables in sequence: bla?var1??var2?bla
Any ideas?
Regards, Håkan |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Dec 04, 2008 1:36 am | All times are GMT
|
|
Ipod shuffle | Tutorial eBook PDF Download | Buy Anything On eBay | Loan | Payday Loan
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|