Find the answer to your Linux question:
Results 1 to 6 of 6
I have the following script to count how many files root and another user that I can access. Since I am not running it as root, find outputs errors for ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2005
    Posts
    65

    Suppressing error messages in bash


    I have the following script to count how many files root and another user that I can access. Since I am not running it as root, find outputs errors for directories it cannot open. How can I supress these error messages so only the 2 lines I need will print.

    Code:
    #!/usr/bin/bash
    
    root=`find / -user "root"|wc -l`
    evlschemm=`find / -user "elschemm"|wc -l`
    
    echo "I can access $root files owned by root"
    echo "I can access $evlschemm files owned by elschemm"

  2. #2
    Just Joined!
    Join Date
    Mar 2005
    Location
    Northern Ireland
    Posts
    23
    hi;

    try running the script with:

    Code:
    $ ./myscript 2>/dev/null
    the "2 > /dev/null" will pipe Standard Error (stderr) output to /dev/null (which means i'll not appear on the screen). Or try:

    Code:
    $ ./myscript 2>/tmp/myscript_errs.txt
    to pipe any errors to a file (could be useful to check out for errors )

    Enjoy!

  3. #3
    Just Joined!
    Join Date
    Mar 2005
    Posts
    65
    These dont seem to work, maybe they need to be logged in as root? I cant log in as root, well I will keep researching.
    Thanks

  4. #4
    Just Joined!
    Join Date
    Mar 2005
    Location
    Northern Ireland
    Posts
    23
    You're getting error messages from the output of the find command; try adding the "2>/dev/null" stderr suppression inside the script:

    Code:
    root=`find / -user "root" 2>/dev/null | wc -l `
    evlschemm=`find / -user "elschemm" 2>/dev/null | wc -l`

  5. #5
    Just Joined!
    Join Date
    Mar 2005
    Posts
    65
    I was gonna try that to...Worked like a charm,
    thanks

  6. #6
    Just Joined!
    Join Date
    Mar 2005
    Location
    Northern Ireland
    Posts
    23
    Great; glad to help.

    I forgot that stderr is written too on a "per-command-basis" ; so that's why you need to use the "2>/dev/null" after every command that could produce output you don't want to see.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •