LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-11-2003, 01:59 PM   #1
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Rep: Reputation: 30
Using echo to output a string to a file with forward slashs


I'm trying to issue this command:

echo "\jsmith, "|/usr/bin/vacation jsmith"" >> /users/jsmith/.forward

But when I do, it doesnt output the string to the .forward file. Does anyone have a clue as to why this is happening?
 
Old 06-11-2003, 02:22 PM   #2
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
What, exactly, do you want in the file? Your quoting isn't correct, and it's unclear to me what you want in the file. If you want this:
Code:
\jsmith, "|/usr/bin/vacation jsmith"
in your file, then you need to learn about escaping characters. There are three types of escapes, ", ', and \. They each do things a little differently and it's very important to use them in the correct manner. For this application, you need to use the ' escape:
Code:
echo '\jsmith, "|/usr/bin/vacation jsmith"' >> file
I'm not trying to be rude, just stating that escaping characters from the shell is something that causes a lot of grief when it doesn't need to. . .
 
Old 06-11-2003, 02:41 PM   #3
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
What I'm trying to do, is interpret this line:

echo '\$username, "|/usr/bin/vacation $username"' >> .forward

to:
--------------------------------------------------
\jsmith, "|/usr/bin/vacation jsmith"
--------------------------------------------------

within a script I am writing. The output should go be exactly what is shown.

Thanks alot
 
Old 06-11-2003, 04:55 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
"\\$username, \"|/usr/bin/vacation $username\"" i think, basically in " quotes everything is interpreted by the shell, to put a shell-character in literally prefix it with a \ eg \ is produced by \\ " is produced by \" etc
 
Old 06-12-2003, 01:53 PM   #5
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Use the command I gave above.
 
Old 06-12-2003, 02:12 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Btw, if you want to "resolve" username using the env vars, it's $USERNAME, or $LOGNAME, AFAIK.
 
Old 06-20-2003, 07:02 AM   #7
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
In my script, when its time for this line to be interpreted:
echo '\$username, "|/usr/bin/vacation $username"' >> .forward

I want the line in the .forward file to look like this:
\jsmith, "|/usr/bin/vacation jsmith"

Right now, its only outputting this:
\$username, "|/usr/bin/vacation $username
 
Old 06-20-2003, 11:33 AM   #8
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Use this:
echo '\'$USER', "|/usr/bin/vacation ' $USER'"'


Whatever is processing the forms is interpreting the above text in such a way that it removes the backslash.
Should be:
echo ' \ ' $USER', "|/usr/bin/vacation ' $USER'"'
without the space between the \ and the single quotes.

Last edited by moses; 06-20-2003 at 11:39 AM.
 
Old 06-20-2003, 12:32 PM   #9
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
That almost works to perfection, but what I get from that is this:

\ ckoniecny, "|/usr/bin/vacation ckoniecny"

instead of this:

\ckoniecny, "|/usr/bin/vacation ckoniecny"

Notice the that there is no space on the one that I would like to have output to a file.

Thanks
 
Old 06-20-2003, 01:14 PM   #10
0x4B
Member
 
Registered: May 2003
Location: Nashville TN, USA
Distribution: Debian (I'm unstable)
Posts: 117

Rep: Reputation: 15
there is also a space between the single quote and the $USER that you'll need to remove
 
Old 06-20-2003, 01:19 PM   #11
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
I did, this is the command I'm using as of right now...
echo '\' $username', "|/usr/bin/vacation' $username'"' >> .forward
 
Old 06-20-2003, 01:36 PM   #12
0x4B
Member
 
Registered: May 2003
Location: Nashville TN, USA
Distribution: Debian (I'm unstable)
Posts: 117

Rep: Reputation: 15
yeah, it still looks like there is a space between the second single quote (after the backslacsh) and the $. maybe its just the way the forums are formatted
 
Old 06-20-2003, 02:28 PM   #13
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
I copied and pasted that command moses gave me and the space was there even after I removed it, any ideas?
 
Old 06-20-2003, 03:22 PM   #14
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
The only single quote that should have a space near it is the one that comes after vacation. echo takes everything literally unless it's escaped out (by single or double quotes or backslashes). echo does not strip whitespace, you need to do that yourself:

echo '\'$USER', "|/usr/bin/vacation '$USER'"'

Last edited by moses; 06-20-2003 at 03:24 PM.
 
Old 06-21-2003, 09:58 AM   #15
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Original Poster
Rep: Reputation: 30
This is the command am using right now:

echo '\' $username', "|/usr/bin/vacation' $username'"' >> .forward

And this is the output it gives me (notice the space after the backslash, it shouldnt be there):
\ jsmith, "|/usr/bin/vacation jsmith"

It should be:
\jsmith, "|/usr/bin/vacation jsmith"

How do I fix that extra space after that backslash?
 
  


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
How do I forward standard and error output into same file? ivj Linux - Software 5 07-19-2005 08:30 PM
Send string to std output without interpreting (script) ta0kira Programming 6 06-29-2005 08:50 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM
cant see .forward file in home directory >> mail forward/copy steve_babbage Linux - Newbie 0 03-02-2004 06:25 AM
Exercise 1-9. Write a program to copy its input to its output, replacing each string zombi3 Programming 3 12-21-2003 02:28 AM

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

All times are GMT -5. The time now is 07:41 PM.

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