LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-30-2015, 07:34 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
trying to echo a line that contains echo in a file


I want to add this line to the file named "playing"
Code:
/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "$(date): $i"; done | grep --line-buffered Playing
To do that I use the command echo
Code:
echo "/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "$(date): $i"; done | grep --line-buffered Playing"
The problem is that the word "date" becomes the actual date when echoed into the file "playing". Like this:
Code:
/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo Wed Dec 30 15:31:48 EET 2015: ; done | grep --line-buffered Playing
How can I avoid this, and actually put the word "date" in the file?

Thanks

Last edited by aristosv; 12-30-2015 at 07:54 AM.
 
Old 12-30-2015, 07:45 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
I fear I'm missing something in your question.

$() says to use the returned output of the command within the parentheses. You are doing: $(date) so you are getting the actual output of the command named "date".

If you want the literal word "date" don't put $() around date but instead put quotes around it.

i.e.
DO:
echo "date"
NOT:
echo $(date)
 
Old 12-30-2015, 07:51 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
The file "playing" will become an executable which will later run, to show which song is playing.
The command "date" has to run, to show the date and time the song started to play. That's why I need $(date).
"date" is not working
 
Old 12-30-2015, 07:53 AM   #4
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
To put it simply, I need to put the following command in a file. Using echo, sed, awk or whatever's available.

Code:
/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "$(date): $i"; done | grep --line-buffered Playing
 
Old 12-30-2015, 08:22 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
ah - that makes it clearer.

1) Encapsulate the whole string in double quotes.
2) Escape the dollar signs and parentheses by putting the \ character in front of them

e.g.
echo "/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "\$\(date\): \$i"; done | grep --line-buffered Playing" >test2

That would add your original line to the file named test2.

cat test2 would show:
/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo $(date): $i; done | grep --line-buffered Playing
 
Old 12-30-2015, 08:33 AM   #6
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
That did it. Thanks for your help.
 
Old 12-30-2015, 04:32 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Use of single quotes is less messy; specifically no need for '\'s
Code:
echo '/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "$(date): $i"; done | grep --line-buffered Playing' >t.t

cat t.t

/usr/bin/mpg123 -Z /root/media/* 2>&1 | while read i; do echo "$(date): $i"; done | grep --line-buffered Playing
 
1 members found this post helpful.
Old 01-05-2016, 12:39 PM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by chrism01 View Post
Use of single quotes is less messy; specifically no need for '\'s
Good point and good for what the OP was doing.

I used the single quotes to day in doing a bunch of repetitive output that had things like slashes, semi-colons and brackets that would have been a bit of a pain to escape singly.

One note on it though is that the single quotes can't be used if you do want a variable expanded in one of the echo lines as I did today. The single quotes make it take ${var} literally rather than giving the value of ${var}. Not an issue in what the OP wrote but was an issue for what I was doing so for that one line I did have to do double quotes. Luckily that one line didn't also require me to escape anything else on the line but I could see situations where it would.

Just posting to note there are times when both are appropriate.

Last edited by MensaWater; 01-05-2016 at 03:30 PM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo text into the beginning of a specific line of a existing file RandyH9 Programming 3 04-13-2014 02:36 PM
[SOLVED] echo text to a specific line in an existing file Jammyzx Programming 6 10-12-2011 12:17 AM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:21 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