LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-06-2016, 02:26 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
simple script written concisely


I've started reading this: http://www.tldp.org/LDP/abs/abs-guide.pdf

An exercise says that I should write a script that shows the date, uptime and lists all logged-in users and which creates a log with this information.

So I don't know how to write this script, except by writing the commands twice.

Quote:
echo `date` && echo `who`\n && echo `uptime`\n | tee /var/log/sysstatus.log
This is what I did, but when the file is written in under /var/log, only the input of the last command (uptime) is written in the file.

Any suggestions?
 
Old 06-06-2016, 03:21 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Your "&&"s start a new command and so the pipe to tee only receives the output of the last echo. Use one echo or multiple tees.
 
1 members found this post helpful.
Old 06-06-2016, 03:25 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
The "echo" parts are kind of unnecessary. You can run the programs plain without the backticks, but group them inside a set of parenthesis and then pipe the output of the parenthesis to "tee"

Later, if you do need command substitution you can use $( ... ) instead of backticks. It will be a bit easier to read and can even be nested.
 
2 members found this post helpful.
Old 06-07-2016, 07:48 AM   #4
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Hi,

What you need to learn is I/O redirection.

The && only waits for the preceding command to finish and if the return value is 0, then proceeds to the next command.

The pipe ( | ) is a form of I/O redirection.

Code:
program1 ARGS | program2 ARGS
The above example command actually takes the output obtained from program1 and pipes it into program2. Basically, output of program1 becomes input for program2.

Code:
program1 ARGS > textfile.log
For this one, the output of program1 will be redirected to the regular file textfile.log. In other words, if program1 normally outputs messages on the standard output (your terminal screen), then this output will now be placed in the regular file after the ">".

The "| tee" in your command replaces the ">", and the && only look at the return values from the echo's. So, you need to use a single echo or multiple echo's on different lines and each with their respective tee's or >

Last edited by aragorn2101; 06-07-2016 at 07:50 AM.
 
Old 06-07-2016, 08:00 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
{ date; who; uptime } > /tmp/aaa

Last edited by pan64; 06-08-2016 at 12:47 AM. Reason: typo
 
Old 06-07-2016, 02:57 PM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by pan64 View Post
[coded]
{ date; who; uptime } > /tmp/aaa
[/code]
Isn't that supposed to be between brackets? It doesn't work with { } But it does with brackets. I suppose I could also simply redirect it to /var/log/whatever and then simply cat that /var/log/whatever. Thought of it afterwards.

But I guess I'm going back to turbocapitalist's answer. So that's quite logical, indeed.

But how can I append the output using tee? So displaying stdout, but also appending it to a certain file?

Last edited by vincix; 06-07-2016 at 03:00 PM.
 
Old 06-07-2016, 03:04 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by vincix View Post
But how can I append the output using tee? So displaying stdout, but also appending it to a certain file?
You can append with -a, according to the manual page for "tee". There's also "tee --help" but that gives about the same information. If you leave off the -a it will overwrite the file.

If you use redirects, you can append with >>
 
1 members found this post helpful.
Old 06-07-2016, 03:13 PM   #8
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Ok, I simply needed to add -a to tee. Thank you all for your helpful answers!

Sorry, I saw your post afterwards
 
Old 06-07-2016, 08:28 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pan64 View Post
[coded]
{ date; who; uptime } > /tmp/aaa
[/code]
Quote:
Originally Posted by vincix View Post
Isn't that supposed to be between brackets? It doesn't work with { } But it does with brackets.
It's just mising a semicolon:
Code:
{ date; who; uptime; } | tee /var/log/sysstatus.log
                   ^
 
Old 06-12-2016, 01:26 PM   #10
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by rknichols View Post
It's just mising a semicolon:
Code:
{ date; who; uptime; } | tee /var/log/sysstatus.log
                   ^

Weird that between brackets it should work without the last semicolon, whereas with curly brackets it needs the semicolon.
 
  


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
LXer: Fehlstart: a simple, quick application launcher written in C LXer Syndicated Linux News 0 04-05-2013 09:00 AM
Script output for DD not being written to file mhouston100 Linux - Server 2 06-23-2010 12:24 AM
Anyone written a music tagging script? bigalexe General 2 02-19-2008 08:19 AM
Simple mathmatics written in Assembly code using nasm amon Programming 3 01-24-2007 12:31 PM
Has someone already written a tutorial script? kedens Linux - General 1 10-10-2006 08:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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