Check if a file is a device file

1

The exercise consists of evaluating the files contained in a directory, depending on whether they are:

  • Directories
  • Executable
  • Device
  • Regular files

My moment code is this:

    # Clasificamos el contenido del directorio
    # Contadores:
    d=0
    e=0
    t=0
    dv=0

    # Recorremos el directorio actual
    for entry in *
    do
    echo "$entry"

            # Comprobamos si es un directorio
            if [ -d "$entry" ]
            then
                ((d = d + 1))
            fi

            # Comprobamos si es ejecutable
            elif [ -x "$entry" ]
            then
                ((e = e + 1))
            fi

            # Comprobamos si es un fichero de texto (regular)
            elif [ -f "$entry" ]
            then
                ((t = t + 1))
            fi

    done

I've been testing, and for now it's fine, but I can not find a way to check if a file is a device file (/ dev)

    
asked by MigherdCas 14.05.2017 в 01:23
source

1 answer

2

There are different types of device files.

From the bash you can use:

  • -b to check if it is a block device
  • -c to check if it is a character device

as both are device files, you have to use the logical operator and [& amp;]

elif [ -b "$entry" ] && [-c "$entry" ];
then
   ((dv = dv + 1))
fi

About Device Files

    
answered by 14.05.2017 / 01:39
source