LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using echo to output a string to a file with forward slashs (https://www.linuxquestions.org/questions/linux-general-1/using-echo-to-output-a-string-to-a-file-with-forward-slashs-65016/)

chrisk5527 06-11-2003 01:59 PM

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?

moses 06-11-2003 02:22 PM

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. . .

chrisk5527 06-11-2003 02:41 PM

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

kev82 06-11-2003 04:55 PM

"\\$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

moses 06-12-2003 01:53 PM

Use the command I gave above.

unSpawn 06-12-2003 02:12 PM

Btw, if you want to "resolve" username using the env vars, it's $USERNAME, or $LOGNAME, AFAIK.

chrisk5527 06-20-2003 07:02 AM

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

moses 06-20-2003 11:33 AM

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.

chrisk5527 06-20-2003 12:32 PM

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

0x4B 06-20-2003 01:14 PM

there is also a space between the single quote and the $USER that you'll need to remove

chrisk5527 06-20-2003 01:19 PM

I did, this is the command I'm using as of right now...
echo '\' $username', "|/usr/bin/vacation' $username'"' >> .forward

0x4B 06-20-2003 01:36 PM

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

chrisk5527 06-20-2003 02:28 PM

I copied and pasted that command moses gave me and the space was there even after I removed it, any ideas?

moses 06-20-2003 03:22 PM

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'"'

chrisk5527 06-21-2003 09:58 AM

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?


All times are GMT -5. The time now is 09:19 PM.