How To Suppress Error In Shell Script
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 Business Learn more about hiring developers or posting ads 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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up suppress shell script error messages up vote 12 down vote favorite 1 In my shell script I got these lines: rm tempfl.txt rm tempfl2.txt If these do not exist I get the error messages rm: tempfl2.txt: No such file or directory rm: tempfl.txt: No such file or directory is there a way to only suppress these messages even though they do not always appear, as the files might exist? shell suppress-warnings error-suppression share|improve this question asked Mar 28 '13 at 9:57 John Andreas Westman 84129 Have a look here: stackoverflow.com/questions/12785533/… –ConcurrentHashMap Mar 28 '13 at 10:00 add a comment| 4 Answers 4 active oldest votes up vote 25 down vote accepted You have two options: Suppress rm warnings $ rm tempfl.txt 2> /dev/null Redirect script output to /dev/null $ ./myscript.sh 2> /dev/null The latter has a drawback of missing all other warning messages produced by your script. share|improve this answer answered Mar 28 '13 at 9:59 kamituel 18.2k23763 1 rm -f should be used carefully. –hetepeperfan Mar 28 '13 at 10:01 good point, thanks –kamituel Mar 28 '13 at 10:02 add a comment| up vote 2 down vote you should redirect all error message to /dev/null like rm tempfl2.txt 2> /dev/null share|improve this answer answered Mar 28 '13 at 9:59 developer 1,84521736 add a comment| up vote 2 down vote try this command: rm -f tempfl.txt the -f option acts like this: -f, --force ignore nonexistent files, never prompt The command also doesn't report a non-zero error code in case the file doesn't exist. share|improve
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 Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux 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 http://stackoverflow.com/questions/15678796/suppress-shell-script-error-messages works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How do I suppress a warning message? up vote 1 down vote favorite touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2> /dev/null where foo is a directory It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' and http://unix.stackexchange.com/questions/148761/how-do-i-suppress-a-warning-message putting a directory_ in front so the directory /home/test/foo would create a file directory_\home\test\foo It does exactly what I want except a warning keeps printing tr: warning: an unescaped backslash at end of string is not portable a. I don't know what that means b. I would like it to not show I tried to redirect stderr to /dev/null but it doesn't work. How do I suppress it? bash tr stderr share|improve this question edited Aug 6 '14 at 14:41 asked Aug 6 '14 at 14:32 lonewarrior556 3632511 Please edit and clarify how you use this. Are you calling it as a function? Where is $1 defined? You should also be aware that while \ are allowed in file names, it is a Very Bad Idea® to include them and it will lead to all sorts of trouble later on. –terdon♦ Aug 6 '14 at 14:37 It's in a script. to run it I "sh script nameOfDirectory" –lonewarrior556 Aug 6 '14 at 14:38 2 Please edit your question to clarify accordingly. I also strongly suggest you consider using any other character but \ , backslashes are special as you're finding
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here. Search Forums Show Threads Show Posts http://www.unix.com/shell-programming-and-scripting/34502-how-suppress-error-messages-script.html Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... http://www.linuxquestions.org/questions/programming-9/bash-scripting-suppress-error-messages-66031/ unix and linux commands - unix shell scripting How to suppress error messages in script Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 01-16-2007 helper2007 Registered User Join Date: Jan 2007 Last Activity: 16 January 2007, 10:43 how to PM EST Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts How to suppress error messages in script I am getting the following upon cat a file which is not present in directory. "cat: cannot open test1.txt" I need to process files and I want that this message should be suppressed. thx Remove advertisements Sponsored Links helper2007 View Public Profile Find all posts by how to suppress helper2007 #2 01-16-2007 blowtorch AFK Join Date: Dec 2004 Last Activity: 1 July 2016, 6:18 AM EDT Location: UK Posts: 2,351 Thanks: 0 Thanked 6 Times in 6 Posts When you run your script, run it as '/path/to/script 2>/dev/null'. This means that all the stuff that would be written to standard error is to be redirected to /dev/null. Remove advertisements Sponsored Links blowtorch View Public Profile Find all posts by blowtorch #3 01-16-2007 helper2007 Registered User Join Date: Jan 2007 Last Activity: 16 January 2007, 10:43 PM EST Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Could please explain more, I am new to scripting so when you say "run it as '/path/to/script 2>/dev/null' " this means I should add this line in my script? helper2007 View Public Profile Find all posts by helper2007 #4 01-16-2007 nathan Supporter Join Date: Jul 2006 Last Activity: 15 February 2012, 11:03 AM EST Posts: 168 Thanks: 0 Thanked 0 Times in 0 Posts No, it means when you run your script, add the exact text below right after the name of the script, be
HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming bash scripting - suppress error messages? User Name Remember Me? Password Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 06-16-2003, 06:30 PM #1 brian0918 Member Registered: Apr 2003 Posts: 87 Rep: bash scripting - suppress error messages? I've got a bash script file that runs fine, but since I haven't taken into account all possibilites, error messages like: ls: BLEH*: No such file or directory appear in the output sometimes. Is there any way to suppress these error messages? brian0918 View Public Profile View LQ Blog View Review Entr