Linux Bash Get Error Message
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Bash Error Message Variable
Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags shell script error handling Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and
How To Capture Error Message In Shell Script
other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to bash catch error message catch an error in a linux bash script? up vote 5 down vote favorite 1 I made the following script: # !/bin/bash # OUTPUT-COLORING red='\e[0;31m' green='\e[0;32m' NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists { cd $1 if [ $? = 0 ] then echo -e "${green}$1${NC}" else echo -e "${red}$1${NC}" fi } # EXE directoryExists "~/foobar" directoryExists "/www/html/drupal" The script works, but beside my echoes, there is also bash capture error message the output when cd $1 fails on execution. testscripts//test_labo3: line 11: cd: ~/foobar: No such file or directory Is it possible to catch this? bash shell shell-script error-handling share|improve this question edited Oct 22 '13 at 22:58 Gilles 372k696761127 asked Oct 22 '13 at 10:29 Thomas De Wilde 28114 Just an FYI, you can also do this a lot simpler; test -d /path/to/directory ( or [[ -d /path/to/directory ]] in bash ) will tell you whether a given target is a directory or not, and it will do it quietly. –Patrick Oct 22 '13 at 12:36 @Patrick, that just tests if it's a directory, not if you can cd into it. –Stéphane Chazelas Oct 22 '13 at 12:54 @StephaneChazelas yes. The function name is directoryExists. –Patrick Oct 22 '13 at 13:57 add a comment| 5 Answers 5 active oldest votes up vote 4 down vote accepted Your script changes directories as it runs, which means it won't work with a series of relative pathnames. You then commented later that you only wanted to check for directory existence, not the ability to use cd, so answers don't need to use cd at all. Revised. Using tput and colours from man terminfo: #!/bin/bash -u # OUTPUT-COLORING red=$( tput setaf 1 ) green=$( tput setaf 2 ) NC=$( tput setaf 0 ) # or pe
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Bash Print Error Message
About Us Learn more about Stack Overflow the company Business Learn more about shell script exit on error hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux
Bash Script Exit On Error
Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script ask a question Anybody can answer The best answers are voted up and rise to the top Is it possible to get the error message from previous command which failed when the conditional command runs using || up vote 5 down vote favorite 2 I have a helper function: function error_exit { /opt/aws/bin/cfn-signal ... exit 1 } This helper function is used to signal an error. Here http://unix.stackexchange.com/questions/27218/is-it-possible-to-get-the-error-message-from-previous-command-which-failed-when is an example of usage: /opt/aws/bin/cfn-init -s .. || error_exit 'Failed to run cfn-init' The cfn-init command takes a lot of parameters which isn't relevant for the question. When the command returns a non-null value and possibly an error message to the error output, I would like to get the error message and include it to the error_exit method as a parameter. Is this possible? If not, how would you implement a helper method in bash which makes it possible to get the source error message? bash shell error-handling share|improve this question edited Dec 20 '11 at 23:31 Gilles 372k696761127 asked Dec 20 '11 at 11:00 tronda 182111 In the context of CloudFormation's stock templates, from which this looks to derive, also consider setting 'DisableRollback' with --disable-rollback in the CLI tools. Also use cfn-init's -v flag to get verbose output. –Christopher Jul 23 '12 at 13:22 add a comment| 2 Answers 2 active oldest votes up vote 6 down vote accepted You can redirect the error output to a file and then retrieve that output: trap "rm -f /tmp/cfn-error.txt" 0 1 2 3 15 /opt/aws/bin/cfn-init -s ... 2>/tmp/cfn-error.txt || error_exit $(
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more http://stackoverflow.com/questions/25303996/how-to-capture-command-error-message-in-variable-for-if-block about Stack Overflow the company Business Learn more about hiring developers or posting ads https://ubuntuforums.org/showthread.php?t=689289 with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up how to capture command error message in variable for if block up error message vote 1 down vote favorite 1 Hi below is my code for bash shell script, in this I want to capture error message for if clause, when it says either job is already running or unable to start the job to a variable, how it is possible in below script, or any other way for the below functionality if initctl start $i ; then echo "service $i started by capture error message script" else echo "not able to start service $i" fi linux bash shell share|improve this question asked Aug 14 '14 at 8:56 agarwal_achhnera 85322248 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted You can for example use the syntax msg=$(command 2>&1 1>/dev/null) to redirect stderr to stdout after redirecting stdout to /dev/null. This way, it will just store stderr: error=$(initctl start $i 2>&1 1>/dev/null) if [ $? -eq 0 ]; then echo "service $i started by script" else echo "service $i could not be started. Error: $error" fi This uses How to pipe stderr, and not stdout?, so that it catches stderr from initctl start $i and stores in $error variable. Then, $? contains the return code of the command, as seen in How to check if a command succeeded?. If 0, it succeeded; otherwise, some errors happened. share|improve this answer edited Aug 17 at 8:23 answered Aug 14 '14 at 9:00 fedorqui 110k30167217 Wouldn't this solution be affected by this bug ? As it is dated from 2010, I'm not sure wether it's been fixed –Aserre Aug 14 '14 at 9:05 Interesting... I wasn't aware of that. I don't have Ubuntu here w
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] Bash error message handling Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 6 of 6 Thread: [SOLVED] Bash error message handling Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode February 6th, 2008 #1 roggo View Profile View Forum Posts Private Message Spilled the Beans Join Date Oct 2007 Beans 14 DistroUbuntu 7.10 Gutsy Gibbon [SOLVED] Bash error message handling Hello all! I have a simple question, and my searches have not found an answer so I want your help! How do I find the string of the error message of a command? In example... ls "this file does not exist.txt" echo THE_ERROR_FROM_LS # (no such file or dir) Thanks! Adv Reply February 6th, 2008 #2 ghostdog74 View Profile View Forum Posts Private Message I Ubuntu, Therefore, I Am Join Date Sep 2006 Beans 2,914 Re: Bash error message handling Originally Posted by roggo Hello all! I have a simple question, and my searches have not found an answer so I want your help! How do I find the string of the error message of a command? In example... ls "this file does not exist.txt" echo THE_ERROR_FROM_LS # (no such file or dir) Thanks! echo $? that's the return status. is that what you want. Also, pls try to read the bash manual next time. Python tutorial |Unix power tools|Effective AWK|A handful of Awk|Perl|File Renamer|Bash ref Adv Reply February 6th, 2008 #3 roggo View Profile View Forum Posts Private Message Spilled the Beans Join Date Oct 2007 Beans 14 DistroUbuntu 7.10 Gutsy Gibbon Re: Bash error message handling