Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [11].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

replaced http://superuser.com/ with https://superuser.com/
Source Link

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [33]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].

Source Link
Hastur
  • 19.6k
  • 9
  • 56
  • 101

As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:

#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### '   # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ###  '    

$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR

Short notes

  • If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND, &&, and OR, || [1].
    The logic: cmd1 && cmd2, cmd2 will executed only if cmd1 exits without errors (with exit code 0). Instead with cmd1 || cmd3, cmd3 will be executed only if cmd1 will exits with an error (error code different from 0).

  • If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation

      mycommand               # you execute your command
      ExitCodeToUseLater=$?   # So I can use it later in the script
      # ... other stuffs ...
      [ $ExitCodeToUseLater = 0  ]  && echo "It was gone ok " 
      # ... again other stuffs ...
      [ $ExitCodeToUseLater = 0  ] || echo "# Never a time that it goes as I want"
    
  • In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the TAR_COMMAND variable). Writing in a shell the variable $MyVar it will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more from man bash.

  • /dev/null is the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].