1

The call is simply ud typed in a terminal. ud is defined as the following in my ~/.bash_aliases:

alias ud='sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get clean -y'

After a quick search I found the following method suggested for maintaining log files (using make install from the original post):

$ make install > file.txt 2>&1

Here is the problem; if I adapt this to my needs, I do not get the expected results out from the following syntax:

$ ud > file.txt 2>&1

What I get is that the command executes to on-screen stdout and a 0-byte text file is produced. What I wish to accomplish is create an output text file similar to the following for ls:

$ ls > file.txt 2>&1

May I ask for someone to explain to me what is going on?

Finally, the following produce results I cannot explain:

$ $(ud) > file.txt
La commande « Ign » est introuvable, vouliez-vous dire :
 La commande « tgn » du paquet « tgn » (universe)
Ign : commande introuvable

$ echo $(ud) > file.txt
  • 1
    Run it in a subshell (ud) > file.txt 2>&1 ..also don't use dist-upgrade unless you are sure of it.. – heemayl May 2 '15 at 0:24
  • YES. This works, I guess I missed a subtlety. Thanks! – Tfb9 May 2 '15 at 0:30
2

Run the command in a subshell:

(ud) > file.txt 2>&1

This will have the commands in the alias ud to run in a subshell and the STDOUT and STDERR of all the commands will be directed to the parent shell so that we can redirect the STDOUT and STDERR of all commands to the same file from our parent shell at once.

Or as @steeldriver suggested you could also set the alias as following so that all the commands are grouped together and their STDOUT and STDERR will be redirected at once:

alias ud='{ sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get clean -y ;}'

Now you can run the command as:

ud > file.txt 2>&1

Now, lets get what you were doing wrong:

You have set the alias as:

alias ud='sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get clean -y'

Now as you run :

ud > file.txt 2>&1

Only the last successful or failed command's STDOUT and(or) STDERR gets redirected to file.txt, all previous commands run and depending on that command's exit code (success &&) the next command will run. So, in your case all commands succeeded and as sudo apt-get clean -y removes all the .deb files from /var/cache/apt/archives silently without showing any STDOUT you were getting an empty file.

  • ... or perhaps use command grouping in the alias alias ud='{ sudo apt-get update && ... && sudo apt-get clean -y ;}' – steeldriver May 2 '15 at 0:34
  • @steeldriver yeah..that would do too..thanks..adding.. – heemayl May 2 '15 at 0:36
  • So many thanks for the explanation. I have to read up on .deb files, now... – Tfb9 May 2 '15 at 1:10
  • @Tfb9 Happy to help :) – heemayl May 2 '15 at 1:11

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.