How to get error output and store it in a variable or file

后端 未结 3 1756
耶瑟儿~
耶瑟儿~ 2021-01-14 11:31

I\'m having a little trouble figuring out how to get error output and store it in a variable or file in ksh. So in my script I have cp -p source.file destination

3条回答
  •  一个人的身影
    2021-01-14 11:57

    You can redirect the error output of the command like so:

    cp -p source.file destination 2>> my_log.txt
    

    It will append the error message to the my_log.txt file.

    In case you want a variable you can redirect stderr to stdout and assign the command output to a variable:

    my_error_var=$(cp -p source.file destination 2>&1)
    

提交回复
热议问题