Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
Well I run a command in my bashscript using $(COMMAND) Is there a way to make the program not give ANY output. For example I give tar a file that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429

    Run Command Silent in Bash?


    Well I run a command in my bashscript using $(COMMAND)
    Is there a way to make the program not give ANY output.
    For example I give tar a file that isnt a tar file.
    Then it give the error. Its not a tar file. And Aborts
    Its fine with me that he aborts. but that output is anoying since I dont want to see that.
    So I look in the man page of tar and there isnt a Quite Function.
    So, I thought can I maybe do it in another way like:
    $(COMMAND) >> /dev/null
    or silent($(COMMAND))

    I tryed the first one, but not the second one (since thats just rubbish)
    Anyhow, is there a way to do something like this?

    Thanks,
    Cheers,
    Robin
    New Users, please read this..
    Google first, then ask..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I don't think you want the command to give no output, because the
    Code:
    $(COMMAND)
    construct is used to collect standard output from the command. For example, when I run this script:
    Code:
    #!/bin/bash
    
    aaa=$(date | rev)
    echo $aaa $aaa
    I get something like:
    Code:
    8002 TSP 20:20:30 51 naJ euT 8002 TSP 20:20:30 51 naJ euT
    I think what you want (correct me if I'm wrong) is that standard output should go to the variable, but standard error should be thrown away.

    In that case, do something like this:
    Code:
    aaa=$(date | rev 2> /dev/null)
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    Umm, sorry I think I need to do more explaination.
    When I run my script now with the wrong password I get:
    Code:
    bad decrypt
    1349:error:<number>:error explain
    tar: This does not look like a tar archive
    tar: Skipping to next header
    tar: Error Exit delayed from previous errors
    Wrong Passphrase
    And this part of my script does that:
    Code:
     echo "Enter Passphrase"
     read -s PASSWORD
     $(openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD)
     $(tar -xf container.tar)
     shred -zu container.tar
     if [[ -d container ]]; then
               rm -rf .container
               echo "Succesfully Decrypted"
    else
                echo "   Wrong Passphrase"
    fi
    The Error is a openSSL error that is Hardcoded in the OpenSSL Package to give. (I guess using printf but I guess thats not the point)

    Anyhow, Id there a way to make openssl shutup?
    and tar as well.
    New Users, please read this..
    Google first, then ask..

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    41
    As wje_lf suggested, if you redirect the output to a file then it will not appear on the command line. If you redirect to /dev/null then the output will be thrown away. There are two sets of output for any program, output 1 gives the standard output and output 2 gives the error output.

    So

    Code:
    openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD 1>/dev/null
    or just
    Code:
    openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD >/dev/null
    Will give you no standard output

    Code:
    openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD 2>/dev/null
    Will give you no error output.

    To get neither is a bit different you need to redirect standard output to dev/null then redirect error output to standard output (not the other way around):
    Code:
    openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD >/dev/null 2>&1
    Incidentally are you intending to run the output of the commands?
    Code:
    $(tar -xf container.tar)
    Will untar your file and then try to run the output of the command as a new command. To untar the file you would just need:
    Code:
    tar -xf container.tar

  6. #5
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    Well, ">/dev/null 2>&1" worked for the OpenSSL one.
    Now I run the command aswell on the tar -xf but now my script just crashes.
    Anyhow I tryed to Nck everything to Oblivion before with > /dev/null but it just doesnt work.

    Anyhow, why does my script crash when I did use the >/dev/null 2&>1
    twice?

    Cheers,
    Thanks
    New Users, please read this..
    Google first, then ask..

  7. #6
    Just Joined!
    Join Date
    Jul 2007
    Posts
    41
    Are you still using this?
    Code:
    $(tar -xf container.tar >/dev/null 2>&1)
    What this will do will be to run the command:
    Code:
    tar -xf container.tar >/dev/null 2>&1
    And then attempt to run the output because of the $(). The original command won't produce any output which will probably cause an error when trying to execute the empty second command.

    Using the redirects twice should not cause your script to crash, could you post your code again?
    Last edited by thedondj; 01-15-2008 at 11:29 AM. Reason: Clarification

  8. #7
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    I use the second one.
    Ill try to add $() and then ill report it.

    Cheers,
    Robin

    Edit:

    Code:
    $(openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD >/dev/null 2>&1)
    $(tar -xf container.tar >/dev/null 2>&1)
    New Users, please read this..
    Google first, then ask..

  9. #8
    Just Joined!
    Join Date
    Jul 2007
    Posts
    41
    What's the error message?

    Again I don't understand why you're using the $(...) syntax, is there a good reason for this? Why are you using:
    Code:
    $(openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD >/dev/null 2>&1)
    $(tar -xf container.tar >/dev/null 2>&1)
    instead of

    Code:
    openssl enc -d -aes-256-cbc -salt -in $1 -out container.tar -pass pass:$PASSWORD >/dev/null 2>&1
    tar -xf container.tar >/dev/null 2>&1

  10. #9
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    Since I tryed both
    It doesnt matter
    New Users, please read this..
    Google first, then ask..

  11. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Actually, it does matter. It matters because you're going to try variations until you find something that works. If you try those same variations on the $(...) syntax, it's guaranteed to fail, so why do the extra (possibly misleading) work?

    It also matters because it's important for future work that you understand what $(...) is supposed to do.

    So, once again: Leaving out the $(...) stuff, can you show us your exact script and what output you get?
    --
    Bill

    Old age and treachery will overcome youth and skill.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •