How redirect stderr to variable inside if condition? Bash

MartinP

I try get standard error communicate from mkdir -p $FINAL_BACKUP_DIR and send in message by logger.

It make log more complete for example the user will be know he do not have permission or $FINAL_BACKUP_DIR does not exists.

if ! mkdir -p $FINAL_BACKUP_DIR; then
    logger -t $LOGGER_TAG "Cannot create backup directory in $FINAL_BACKUP_DIR. Standard error communicate. Backup canceling." 1>&2
    exit 1;
fi;

I try something like that:

if ! mkdir -p $FINAL_BACKUP_DIR 2>> ${test1}; then
    logger -t $LOGGER_TAG "Cannot create backup directory in $FINAL_BACKUP_DIR. Backup canceling. $test1" 1>&2
    exit 1;
fi;

But this solution does not working for me in two ways. When I create test1 earlier test1=0 or without that.

I work with Ubuntu 14.04.

Stephen Harris

/dev/null is the standard device to "throw things away".

So

some_command 2> /dev/null

will send the errors from some_command to /dev/null - ie throw away the errors.

Thus:

if ! mkdir -p $FINAL_BACKUP_DIR 2> /dev/null
then
  logger -t $LOGGER_TAG "Cannot create backup directory in $FINAL_BACKUP_DIR. Backup canceling."
  exit 1
fi

Note that you also didn't need all those extra ; characters :-)

EDIT:

You can also direct error to output and capture the result in a variable and test if that variable is empty. In this way you can report on the reason to the user

result=$(mkdir -p $FINAL_BACKUP_DIR 2>&1)
if [ -n "$result" ]
then
  logger -t $LOGGER_TAG "Cannot create backup directory in $FINAL_BACKUP_DIR. Backup canceling: $result"
  exit 1
fi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to redirect stderr in a variable but keep stdout in the console

From Java

Redirect stderr and stdout in Bash

From Dev

how to redirect bash variable to an executable?

From Dev

How to declare a variable inside an if condition?

From Dev

How to declare a variable inside an if condition?

From Dev

bash: redirect stderr to file and stdout + stderr to screen

From Java

How to redirect and append both stdout and stderr to a file with Bash?

From Dev

How do I get bash to redirect stderr into a >( command substitution )?

From Dev

How to redirect stdin and stdout and stderr at the same time in bash?

From Dev

bash - redirect STDERR into log function

From Dev

How to redirect stderr in busybox?

From Dev

Redirect stdout and/or stderr to path in variable

From Dev

How to redirect output of python script to bash variable?

From Dev

how to redirect multiline bash command into a variable

From Dev

How to evaluate a variable inside a variable bash scripting?

From Dev

How to redirect stderr for subprocess module

From Dev

Bash - redirect stdout and stderr to files with background process

From Dev

Can you redirect STDERR for an entire Bash session?

From Dev

Redirect bash stdout+stderr to one file and stderr to another file

From Dev

How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

From Dev

How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

From Dev

Put IF condition inside a variable

From Dev

How to compare variable dynamically inside awk in bash?

From Dev

In Bash, how to set stdout to a variable inside tee

From Dev

How to declare a variable inside bash -c

From Dev

How to redirect stdout and stderr to a file and display stderr to console?

From Dev

How to suppress stdout and stderr in bash

From Dev

bash: Use a variable to store stderr|stdout redirection

From Dev

How to redirect the raw_input to stderr and not stdout?

Related Related

  1. 1

    How to redirect stderr in a variable but keep stdout in the console

  2. 2

    Redirect stderr and stdout in Bash

  3. 3

    how to redirect bash variable to an executable?

  4. 4

    How to declare a variable inside an if condition?

  5. 5

    How to declare a variable inside an if condition?

  6. 6

    bash: redirect stderr to file and stdout + stderr to screen

  7. 7

    How to redirect and append both stdout and stderr to a file with Bash?

  8. 8

    How do I get bash to redirect stderr into a >( command substitution )?

  9. 9

    How to redirect stdin and stdout and stderr at the same time in bash?

  10. 10

    bash - redirect STDERR into log function

  11. 11

    How to redirect stderr in busybox?

  12. 12

    Redirect stdout and/or stderr to path in variable

  13. 13

    How to redirect output of python script to bash variable?

  14. 14

    how to redirect multiline bash command into a variable

  15. 15

    How to evaluate a variable inside a variable bash scripting?

  16. 16

    How to redirect stderr for subprocess module

  17. 17

    Bash - redirect stdout and stderr to files with background process

  18. 18

    Can you redirect STDERR for an entire Bash session?

  19. 19

    Redirect bash stdout+stderr to one file and stderr to another file

  20. 20

    How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

  21. 21

    How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

  22. 22

    Put IF condition inside a variable

  23. 23

    How to compare variable dynamically inside awk in bash?

  24. 24

    In Bash, how to set stdout to a variable inside tee

  25. 25

    How to declare a variable inside bash -c

  26. 26

    How to redirect stdout and stderr to a file and display stderr to console?

  27. 27

    How to suppress stdout and stderr in bash

  28. 28

    bash: Use a variable to store stderr|stdout redirection

  29. 29

    How to redirect the raw_input to stderr and not stdout?

HotTag

Archive