0

I have written a scipt(example.sh) to echo the date and system uptime on stdout. At the same time I want this script to log this output into a log file. Here's my code

echo `date`
echo `uptime`

And at last

cat /home/rpeb/example.sh 1> /home/rpeb/example.log 

to redirect the output into a log file.

I don't want the last line to be logged. What changes should I make to this code?

1
  • What do you mean by I don't want the last line to be logged ? which last line?
    – matsib.dev
    Jun 24 '19 at 2:41
3
#!/bin/sh
{ date; uptime; } | tee "$HOME/example.log"

This would run date and uptime and pipe the output of both to tee which would write the data to the file example.log in the current user's home directory, and to the standard output of the script. The { ...; } is used to combine a set of commands into a "compound command" that can be redirected as a whole.

Without { ...; }, you would need to do two redirections in the script:

#!/bin/sh
date   | tee    "$HOME/example.log"
uptime | tee -a "$HOME/example.log"

Note that the second invocation of tee needs the -a option to append to the logfile, or it would truncate it and you would lose the output of the date command.


You generally don't want to use echo just to output the result of a command substitution. A command substitution (your back-ticked commands, or $(...)) may be useful for inserting the output of a command into a string, used by some other command, but to just output the result of a command you don't need the command substitution or echo (you don't do echo $(ls) on the command line, and you don't do that in a script, for example).

1
  • thanks for clearing my doubt. This is the answer i was looking for. Jun 26 '19 at 4:04
0

this works:

echo `date`
echo `uptime`

but is better this way:

echo $(date)
echo $(uptime)

(read about why here)


And, in this particular case, you'll probably be fine just with:

date
uptime

this:

cat /home/rpeb/example.sh 1> /home/rpeb/example.log

will not execute your script.

cat /home/rpeb/example.sh is just printing the content of /home/rpeb/example.sh

and then, you are redirecting stdout from that command, to the file /home/rpeb/example.log. So, what your are really doing here, is making a copy of /home/rpeb/example.sh into /home/rpeb/example.log. Seems that this is not what you want.

But in case you do, this is more succinct:

cat /home/rpeb/example.sh > /home/rpeb/example.log

when you just use >, the 1 before it is implied.


If you want to run the script /home/rpeb/example.sh, and then redirect its output to the file /home/rpeb/example.log, first, give execute permissions to /home/rpeb/example.sh, this way:

chmod u+x /home/rpeb/example.sh

then, you run the script, simply writing its path, then redirecting its output, like this:

/home/rpeb/example.sh > /home/rpeb/example.log

and, btw, if both files (your script, and your log to be) are in the same dir, and you are inside that dir, you con simply run:

./example.sh > example.log

And if you want the output from example.sh printed on your terminal, and logged into example.log, you can do that this way:

./example.sh | tee example.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.