This question already has an answer here:
- Redirect grep error output to file 2 answers
#!/bin/sh if [ $(ls sample01.log | wc -l) = 1 ] then echo "File Found" > lsOutput.log else echo "File Not Found" > lsOutput.log fi
But if sample01.log is not already existing my code already returns the error:
ls: cannot access sample01.log: No such file or directory
And the code will not run anymore. The File Not Found will not be displayed anymore. I wanted to capture that error to file (No such file or directory) so each time such error occurs, it is written and documented on the log file.