[Bash] How to print error msgs on stderr stream?

General support questions
Post Reply
sunb3
Posts: 1
Joined: 2017/12/11 07:03:06

[Bash] How to print error msgs on stderr stream?

Post by sunb3 » 2017/12/13 03:03:12

Hi,
I want to output error message on stderr stream like C++, Python whatever can do
how to do it with bash?

eg.

Code: Select all

#!/bin/bash

for f in `ls -R`
do
   ls $f > /dev/null
   if [ $? -eq 0 ];then
      printf "%s: this is not a regular file." $f   #how to put this message on stderr stream?
   fi
done
Hi :)

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: [Bash] How to print error msgs on stderr stream?

Post by pjsr2 » 2017/12/13 12:25:39

Redirect stdout (1) to stderr (2)

Code: Select all

printf "this is not a regular file."  1>&2
You can find a thorough explanation of all redirects here: http://www.catonmat.net/blog/bash-one-l ... art-three/

Your code snippet is not testing whether $f is a regular file. It only tests if something is the name of a file in the current directory or the name of a subdirectory. How about:

Code: Select all

find . ! -type f -exec echo {} is not a regular file \;

hunter86_bg
Posts: 2019
Joined: 2015/02/17 15:14:33
Location: Bulgaria
Contact:

Re: [Bash] How to print error msgs on stderr stream?

Post by hunter86_bg » 2017/12/13 17:56:02

Here is what I use:

Code: Select all

echo "Error: 123 " >>&2

Post Reply