Roel
Roel

Reputation: 97

reading from /dev/rfcomm0 with bash not noticing file exists

I am trying to see if the /dev/rfcomm0 file exists but in the way as described below by using the normal [ -f $file ] way I constantly get the message that the file does not exist. But I am able to read the file manually with cat. looking for /etc/hosts is just for controling that I'm not getting insane.

file="/dev/rfcomm0"
file2="/etc/hosts"
while :
do
        [ -f $file2 ] && echo "Found host" || echo "Not found host"
        [ -f $file ] && echo "Found rfcomm" || echo "not found rfcomm"

the manual view of /dev/rfcomm

root@raspberrypi:~# cat /dev/rfcomm0

[null,"0,0,0","255,255,255",5,50,"Nikske"]

Probably I'm missing something very important but I've been looking for a long time after a solution.

Upvotes: 0

Views: 568

Answers (1)

paxdiablo
paxdiablo

Reputation: 882226

The -f flag specifically states in the bash documentation (my emphasis):

True if file exists and is a regular file

So I'd warrant that the file you're looking at is not a regular file, especially since it's in the /dev directory where all sorts of non-regular files tend to exist.

Case in point (on my system with a character-special file):

pax$ ls -al /dev/rfkill 
crw-rw-r--+ 1 root netdev 10, 62 Feb 13 17:49 /dev/rfkill

pax$ [[ -f /dev/rfkill ]] && echo regular || echo not regular
not regular

pax$ [[ -e /dev/rfkill ]] && echo exists || echo does not exist
exists

pax$ [[ -c /dev/rfkill ]] && echo char special || echo not char special
char special

I'd suggest checking what sort of file it is with ls -al and using the appropriate [[ flag for that (see, for example, CONDITIONAL EXPRESSIONS in the output of man bash).

Upvotes: 1

Related Questions