LinuxQuestions.org
Visit Jeremy's Blog.
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 11-21-2001, 01:35 AM   #1
Barbarian
Member
 
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Rep: Reputation: 15
Question BASH string expansions


I'm a bit of a newbie to Linux and BASH and I've been BASHing my head again my monitor with a problem with string expansions. Perhaps one of you clever types can help me out....

To cut a long story short I'm 'grepping' a configuration file and extracting a number of file paths to be used for populating new project accounts. From the read string I want to expand any shell variable (such as $HOME) before using the expanded string in a 'cpy' or 'cvs' command e.g:-

$HOME/dir1a/dir2a/foo
dir3/$AWAY/dir4/dir5/bar
When a parse and (attempt to) expand my string I always end up with the unexpanded variable still in the string. I've tried the various '${}' operators, but either I'm doing it wrong or they only seem to work on variables created within the shell.

Any help would be most appreciated.
 
Old 11-21-2001, 10:06 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The $HOME variables that the shell reads as a string is treated as any other string to the shell. What you need to do is read in the $HOME and use sed to substitute the $HOME read with the shell $HOME variable. It gets kind of tricky since sed uses the '/' character as its delimeter and the paths also contain '/'s. What I had to do is create a new home variable that has the '/'s escaped out, this I can pass to sed.

Code:
#!/bin/sh

ESCHOME=`echo $HOME | sed 's/\//\\\\\//g'`
echo $ESCHOME

read VAR
echo $VAR | sed "s/\$HOME/${ESCHOME}/"
 
Old 11-21-2001, 10:40 PM   #3
Barbarian
Member
 
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Original Poster
Rep: Reputation: 15
Thanks for that, it's an interesting idea and I'll have to add 'sed' to my many things to learn about Linux!

Unfortunately, it doesn't quite give me the result I wanted and after re-reading my initial post (and cursing at my English errors) I decided I wasn't clear enough.

Your solution has a hardcoded $HOME in the string (perhaps $HOME was a bad example), however, the string I've just extracted and stored in a variable contains (say):-

/dir1/dir2/$DIRVAR1/foo

where DIRVAR has been setup globally. Basically, the DIRVAR would contain another directory path which would be used as some sort of system release i.e "beta_release/1a" or "/issue_release/2a" etc...

I've passed the read in string to a subroutine. If I then 'echo' $1 from within my bash script, that is exactly what I get (without $DIRVAR expanded). However, if I build my string manually from the command line it is displayed correctly.
It is easy to parse out the directory path and detect the '$' to denote a variable for expansion. However, I'm not sure whether I'd end up still being in the same boat. Any ideas now?
Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 04:49 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM
Bash problem, no executing of string ChoKamir Programming 3 01-31-2005 04:50 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration