redirection

Hi,

The code below works, it's a part of a bash shell script that serve to search a pattern $pattern_da_cercare in the files contained in a directory $directory_iniziale.

Now the proble is:
How can I redirect stderr to a file?
PS: so I want to redirect ALL the errors to a file.

I tryed to learn how to do it, but I haven't understood

find $directory_iniziale -type f | while read file 
do
  
  awk -v min=$MIN -v max=$MAX -v patt=$pattern_da_cercare '
  BEGIN{RS=" |\n"; patt = "(^|[^A-Za-z0-9])" patt "([^A-Za-z0-9]|$)" }
  $0 ~ patt {n++}
  END {
		if(n >= min && n <= max && n != 0) {print FILENAME " : " n}
  }
  ' "$file"

done

try this.....

yourscript 2>&1 > somefile

says take STDERR (2) and redirect to same place as STDOUT (&1)...

I inserted it in my script, but find still procude output errors on the screen and the error-file is empty

find $directory_iniziale -type f | while read file 2>&1 > conta_occorrenze_log.txt
do
  
  awk -v min=$MIN -v max=$MAX -v patt=$pattern_da_cercare '
  BEGIN{RS=" |\n"; patt = "(^|[^A-Za-z0-9])" patt "([^A-Za-z0-9]|$)" }
  $0 ~ patt {n++}
  END {
		if(n >= min && n <= max && n != 0) {print FILENAME " : " n}
  }
  ' "$file"

done

you want to put that on your command line, not within the script!

I only don't want that appear any errors on the screen.

find $directory_iniziale -type f 2>/dev/null | ....

So I have to put 2> after every command?

Can I redirect all the errors to a file in all the script?

hmmm, what part are you not understanding?

quine has told you how to do that with command line, then when you posted it didn't work when you placed it in the script, I posted "not in the script but on the command line".
The only other alternative, is the 2nd option, "within the script", and yes, you'd then need to put on every line there is output..... so why not put it on the command line?? it sounds like want you're wanting to do.

Suggestion: try it! if it doesn't work, post exactly what you typed, and the resulting output.

Re-direct stderr of the entire shell script with an exec placed at the top right after the shebang line.

#!/usr/bin/ksh

exec 2> file.err

.
.
.

YES!
It was that i mean, I didn't know how to correctly write it.

Thank you shamrock