-1

I want to be able to log result of running bash script to file. Script itself is pretty simple, its used to install basic packages on fresh install machines and it scheme looks like this

sudo apt-get install -y nano

The thing im trying to achieve is the bash script to report whether installation went smoothly or in case it didnt success - to point what went wrong. So I would like it to create file like "log" which would very briefly report status, for example

Package "nano" successfully installed!

or

Package "nano" failed to install. // Here comes the line of apt-get install that caused fail

Thanks in advance!

5
  • 3
    What do you have so far?
    – user143703
    Jul 10 '14 at 13:24
  • To be honest nothing. Came up with this idea this morning, did some googling around but couldnt find an answer. I could use a tee like command but I dont want all the output of apt-get install to be logged. I just want a summary.
    – Prop
    Jul 10 '14 at 13:26
  • 2
    use the exit code of the apt-get command , and redirect stderr to your file Jul 10 '14 at 13:34
  • @DennisNolte Could you please write a simple example ?
    – Prop
    Jul 10 '14 at 13:46
  • -1 this is extremely basic shell programming, and you're asking an extremely specific question without having tried anything yet. If you need to know about redirection, please read man bash and these links.
    – l0b0
    Jul 10 '14 at 13:46
4

First off, you mustn't use sudo in a bash script, it's non-interactive and so if you're required to use a password you won't be able to elevate.

Below would be a basic example to install several packages, to install more you would add a package to the "for package in" line. The output is set to append to the existing file.

#!/bin/bash

apt-get update

for package in nano httpd vim mysql-server
do

apt-get install -qq --print-uris $package >> script.log 2>>script_error.log

done

You should save this file however you like, then you need to set the permissions to allow execution of the script. If you called the file script.sh you would run:

chmod +x script.sh

Then to run the script you would do:

sudo ./script.sh

This is a very basic example, it doesn't log as well as is possible and there are many more complex and better ways to accomplish it, but it will do the job if you're happy to look through the log post-installation. The first log contains minimal logging on normal installation (even if you're not looking at it you may as well keep it for checking) and the second log file contains only the errors from apt-get.

4
  • wont work exactly, sadly "is already installed" f.e. is not stderr, so instead one could tail only the last few lines. Jul 10 '14 at 14:21
  • True, although that's not necessarily a bad thing, if the package is already installed then the script has done it's job. I agree this could be done a lot better but this script is sufficient for bulk install of a few packages.
    – Alex Berry
    Jul 10 '14 at 14:26
  • 1
    Thank you very much, your post gave me an idea of where to look for more answers. Yet, running it its producing an error of "E: Command line option --print-urls is not understood"
    – Prop
    Jul 10 '14 at 14:44
  • Apologies, that was my typo, it should have been --print-uris. It just adds extra info to script.log for further diagnosis. Either copy the script above again (I've corrected the error, I also moved apt-get update out of the loop so it doesn't run for each package, saves a little time and bandwidth) or replace with with i.
    – Alex Berry
    Jul 10 '14 at 14:53

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