Write a function that lists the number of ordinary files, directories, block special files, character special files, FIFOs, and symbolic links in the working directory. Do this in two different ways:

a. Use the first letter of the output of ls –l to determine a file’s type.
b. Use the file type condition tests of the [[ expression ]] syntax to determine
a file’s type.

a.
$ function ft () {
> declare -i ord=0 dir=0 blk=0 char=0 fifo=0 symlnk=0 other=0
>
> for fn in *
> do
> case $(ls -ld "$fn" | cut -b1) in
> d)
> ((dir=$dir+1))
> ;;
> b)
> ((blk=$blk+1))
> ;;
> c)
> ((char=$char+1))
> ;;
> p)
> ((fifo=$fifo+1))
> ;;
> l)
> ((symlnk=$symlnk+1))
> ;;
> a-z)
> ((other=other+1))
> ;;
> *)
> ((ord=ord+1))
> ;;
> esac
> done
>
> echo $ord ordinary
> echo $dir directory
> echo $blk block
> echo $char character
> echo $fifo FIFO
> echo $symlnk symbolic link
> echo $other other
> }
b.
$ function ft2 () {
> declare -i ord=0 dir=0 blk=0 char=0 fifo=0 symlnk=0 other=0
>
> for fn in *
> do
> if [[ -h $fn ]]
> then ((symlnk=$symlnk+1))
> elif [[ -f $fn ]]
> then ((ord=ord+1))
> elif [[ -d $fn ]]
> then ((dir=$dir+1))
> elif [[ -b $fn ]]
> then ((blk=$blk+1))
> elif [[ -c $fn ]]
> then ((char=$char+1))
> elif [[ -p $fn ]]
> then ((fifo=$fifo+1))
> else
> ((other=other+1))
> fi
> done
>
> echo $ord ordinary
> echo $dir directory
> echo $blk block
> echo $char character
> echo $fifo FIFO
> echo $symlnk symbolic link
> echo $other other
> }

Computer Science & Information Technology

You might also like to view...

Which of the following calculations on the target cell is NOT supported by Solver?

A) Minimize B) Maximize C) Range D) Exact value

Computer Science & Information Technology

A logical file object is a stream that connects a file of logically related data, such as a data file, to a hardware device.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology