Bash - Reasons for stderr to fill up? -
i wrote script works should, reason stderr being filled something. can go in there , how can prevent happening?
here's scripts, gets directory first argument , names of people next x arguments.
#!/bin/bash authors=$# args=("$@") ((i = 1; < authors; i++)); echo ${args[${i}]} d in `find $1 -type d`; appearances=`grep -c ${args[${i}]} $d/*.comp` if [ "$appearances" -gt 0 ]; filename=`grep -l ${args[${i}]} $d/*.comp | rev | cut -d '/' -f 1 | rev | cut -d '.' -f 1` results=`get_test_stats $d | grep -w ${args[${i}]} | cut -d" " -f2-3` echo $filename: $results fi done done
thank you!
commands find
, grep
may encounter variety of conditions may unexpected or not reflected in output written stdout
. inform user of such condition, warning written stderr
, example:
grep: binary file [...] matches find: `[...]': permission denied
in interactive session, messages written stderr
displayed in terminal, if stdout
redirected, e.g:
mj@ap:/tmp$ find > results find: `./cron-apt.ex7jhf': permission denied
or captured in variable, e.g:
mj@ap:/tmp$ results=$( find ) find: `./cron-apt.ex7jhf': permission denied
you can redirect stderr
well:
find >results 2>errorfile # redirect stderr errorfile find >results 2>/dev/null # discard stderr find >results 2>&1 # include stderr in results
respectively:
results=$( find 2>errorfile ) # redirect stderr errorfile results=$( find 2>/dev/null ) # discard stderr results=$( find 2>&1 ) # include stderr in results
see manpage bash(1)
more information on redirection (search redirection
).