LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux 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
  Search this Thread
Old 11-04-2009, 06:23 PM   #1
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5

Rep: Reputation: 0
passing a path from one script to another without expansion


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.
 
Old 11-04-2009, 11:10 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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?
 
Old 11-05-2009, 12:12 PM   #3
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 11-05-2009, 02:34 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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 02:35 PM.
 
Old 11-05-2009, 04:01 PM   #5
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Disillusionist,

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

Thank you!!!
 
Old 11-05-2009, 04:27 PM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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`
 
Old 11-05-2009, 07:18 PM   #7
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 11-06-2009, 03:27 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 11-06-2009, 10:47 AM   #9
jbowers
LQ Newbie
 
Registered: Dec 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable (a path) with spaces aolong Linux - General 2 06-28-2009 05:50 PM
Avoiding Shell Script Brace Expansion Woodsman Slackware 4 05-31-2008 09:36 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
Bash Script Expansion Problem meadensi Linux - Newbie 1 03-04-2005 05:17 PM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration