Shell command: test

16 Nov.,2022

 

valve shell

Search notes:

Shell command: test

#!/bin/sh
#
#  TODO:  -G, -L, -N, -O, -S, -ef, -nt, -ot, -o, -v, -R, -z, -n, ==, =, !=, <, >, -eq, -ne, -lt, -le, -gt, -ge
#
test_file() {

  echo
  echo testing $1

  if [ -a $1 ]; then
     echo "  File exists"
  else
     echo "  File does not exist"
  fi

  if [ -b $1 ]; then
     echo "  Block special file"
  else
     echo "  No block special file"
  fi

  if [ -c $1 ]; then
     echo "  Character special file"
  else
     echo "  No character special file"
  fi

  if [ -d $1 ]; then
     echo "  Is a directory"
  else
     echo "  Is not a directory"
  fi

  if [ -e $1 ]; then
     echo "  File exists"
  else
     echo "  File does not exist"
  fi

  if [ -f $1 ]; then
     echo "  Regular file"
  else
     echo "  No regular file"
  fi

  if [ -g $1 ]; then
     echo "  set-group-id bit is set"
  else
     echo "  set-group-id bit is not set"
  fi

  if [ -h $1 ]; then
     echo "  File is symbolic link"
  else
     echo "  File is not a symbolic link"
  fi

  if [ -k $1 ]; then
     echo "  Sticky bit is set"
  else
     echo "  Sticky bit not set"
  fi

  if [ -p $1 ]; then
     echo "  Named pipe (FIFO)"
  else
     echo "  No named pipe (FIFO)"
  fi

  if [ -r $1 ]; then
     echo "  File is readable"
  else
     echo "  File is not readable"
  fi

  if [ -s $1 ]; then
     echo "  File size > 0"
  else
     echo "  File size = 0 or file does not exist" 
  fi

  if [ -t $1 ]; then
     echo "  File descriptor open and refers to terminal"
  else
     echo "  File descriptor not open or not referring to terminal" 
  fi

  if [ -u $1 ]; then
     echo "  set-user-id bit is set"
  else
     echo "  set-user-id bit is not set"
  fi

  if [ -w $1 ]; then
     echo "  File is writable"
  else
     echo "  File is not writeable"
  fi

  if [ -x $1 ]; then
     echo "  File is executable"
  else
     echo "  File is not executable"
  fi

}

test_file Conditional-Expressions
test_file $1