LinuxQuestions.org
Have you heard the LinuxQuestions.org Podcast?
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Thread Tools
Old 11-04-2009, 07:23 PM   #1
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5
Thanked: 0
passing a path from one script to another without expansion


[Log in to get rid of this advertisement]
I have a script that generates a new template script. I have no problems passing variables from the generating script to the template script by prefixing said variables with \, i.e. AA1="\$LINENO" However, when I attempt to pass the following variable it is expanded regardless of any restriction that I use to restrict the expansion:
message=`cat /error_messages/test_msg.txt`

The template script is expanded to:
message=This is a test of an error message.

The result I am looking for is:
message=`cat /error_messages/test_msg.txt`

Thank you for your assistance.
linuxcentos jbowers is offline     Reply With Quote
Old 11-05-2009, 12:10 AM   #2
neonsignal
Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Lenny (2.6.30-bpo kernel, Fluxbox) on Toshiba Portege 3500
Posts: 423
Thanked: 92
I'm not sure I understand the problem? Why can't you just quote it:

Code:
message='`cat /error_messages/test_msg.txt`'
Can you provide more detail?
linux neonsignal is offline     Reply With Quote
Old 11-05-2009, 01:12 PM   #3
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5
Thanked: 0

Original Poster
I have tried message='`cat /error_messages/test_msg.txt`' and get the following result:
message='This is a test of an email error message. ', the variable "message" is an expansion of the path.

Since the code is within a function I tested it outside of the function and the results are the same.

The following code does not result in expansion: message=/error_messages/test_msg.txt. So the problem as I see it, is when the code is back quoted and cat is inserted as a command the script that generating the template code executes cat. I believe what is required is to isolate `cat ......` so that the generating script does not execute cat. I use kate as an editor. I can can see that cat is being treated as a command in the generating script.

Thank you for your assistance.
linuxcentos jbowers is offline     Reply With Quote
Old 11-05-2009, 03:34 PM   #4
Disillusionist
Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 825
Thanked: 48
have you tried:
Code:
message=\`cat /error_messages/test_msg.txt\`
Alternatively, please can you provide a larger piece of sample code so that we can try things out.

Last edited by Disillusionist; 11-05-2009 at 03:35 PM..
windows_vista Disillusionist is offline     Reply With Quote
Thanked by:
Old 11-05-2009, 05:01 PM   #5
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5
Thanked: 0

Original Poster
Disillusionist,

Your code resolved the problems, I knew that there was a solution just could not figure it out.

Thank you!!!
linuxcentos jbowers is offline     Reply With Quote
Old 11-05-2009, 05:27 PM   #6
zhjim
Member
 
Registered: Oct 2004
Distribution: Debian lenny & etch, Red Hat 4.0, (used slackware 11.0)
Posts: 445
Blog Entries: 2
Thanked: 29
Just to have that sorted. everythign between ` and ` is run in a subshell. ` IS NO QOTATION in bash. Use ' or " to quote things.
' takes everything between two of them as it is. " allows for interpretation like $variable.

Cheers Zhjim

P.S
to differ between ` and ' use $(your command to run). This does the same like `your command to run`
linuxdebian zhjim is offline     Reply With Quote
Thanked by:
Old 11-05-2009, 08:18 PM   #7
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5
Thanked: 0

Original Poster
Thumbs up

Quote:
Originally Posted by zhjim View Post
Just to have that sorted. everythign between ` and ` is run in a subshell. ` IS NO QOTATION in bash. Use ' or " to quote things.
' takes everything between two of them as it is. " allows for interpretation like $variable.

Cheers Zhjim

P.S
to differ between ` and ' use $(your command to run). This does the same like `your command to run`
This explanation is the best that I have encountered. An example or two might help others better understand the concept.

Thank you,
jbowers
linuxcentos jbowers is offline     Reply With Quote
Old 11-06-2009, 04:27 AM   #8
zhjim
Member
 
Registered: Oct 2004
Distribution: Debian lenny & etch, Red Hat 4.0, (used slackware 11.0)
Posts: 445
Blog Entries: 2
Thanked: 29
Quote:
Originally Posted by jbowers View Post
This explanation is the best that I have encountered. An example or two might help others better understand the concept.
Okay but just some quick ones I'am having migraine headache For further explanations look at :
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://tldp.org/guides.html#abs

Quotation in shell (BASH)
There are two kinds of quotation marks one is ' and the other is " They should always come in pairs say start of quotation and end of quotation else the shell will not be happy and provide a < as a prompt (after hitting enter).
examples:
'example'
"example"

Diffrence between the two is how they esacpe special shell characters like $ * ?. ' escapes everything. So no evaluation is done. With " everything will get expanded
example:
Code:
MYVAR='example'
MYVAR="example"
Code:
echo "MYVAR is: $MYVAR"
prints
Code:
MYVAR is: example
Code:
echo 'MYVAR is: $MYVAR'
prints
Code:
MYVAR is: $MYVAR
Prepare to not mix up ` and '!
` is the start of shell expansion. Everything between `some command` will be run in a subshell. If you want that better use $(somecommand) this is easier to distinguish.

Last one is \ this tells the shell to escape the following character so that it's not interpreted by the shell.
example
Code:
echo \$\(date\)
will print
Code:
$(date)
Now of to bed or to

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://tldp.org/guides.html#abs
windows_xp_2003 zhjim is offline     Reply With Quote
Old 11-06-2009, 11:47 AM   #9
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5
Thanked: 0

Original Poster
Thank you
windows_xp_2003 jbowers is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
passing variable (a path) with spaces aolong Linux - General 2 06-28-2009 06:50 PM
Avoiding Shell Script Brace Expansion Woodsman Slackware 4 05-31-2008 10:36 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 10:09 AM
Bash Script Expansion Problem meadensi Linux - Newbie 1 03-04-2005 06:17 PM
bash script $n variable expansion cortez Programming 6 12-08-2003 05:03 PM


All times are GMT -5. The time now is 12:22 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration