File Test – Shell Script

File Test – Shell Script

File test is the process of testing different file permissions such as read, write and execute. test command is used file attributes testing. The below table shows file-related tests with a test command:

A file exists and is a regular file

Test

True if file

-f filename

Test if the file exists and the file is a regular file

-r filename

Test if the file exists and the file is readable

-w filename

Test if the file exists and the file is writable

-x filename

Test if the file exists and the file is executable

-d filename

Test if the file exists and the file is a directory

-s filename

A file exists and has a size greater than zero

 

1. Following Shell script demonstrates the file attributes testing using the test command

#!/bin/sh
# Test file attributes- filetests.sh

if [ -f $1 ]
then
   echo "File exist and file is regular file"
else
   echo "File Does not exists"
fi

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

if [ -w $1 ]
then
   echo "File exist and file writeble"
else
   echo "File is not writeble"
fi

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

if [ -s $1 ]
then
   echo "File exist and size is more than zero"
else
   echo "File size is less than zero"
fi

The output of the above program:

File exist and file regular file
File exist and file readable
File exist and file writeble
File exist and file executable
File exist and size is more than zero

2. Write a Shell script which accepts two file names as arguments through the command line, checks if the permissions for these two files are identical (same) and if permissions are identical (same) outputs the common permission, otherwise outputs each file name followed by its permissions.

READ  Basics of Shell Script

Solution:

First, we check whether two filenames are passed through the command line and if not we print the message as no arguments. Then we check whether both the files exists using test command (-e option is used for this purpose). Using ls -l command we read the file attributes of both the files and we extract the file permission using the cut command (from column 2 to 10), store in p1 and p2 variables. Using string comparison we check the equivalence of file attributes p1 and p2. If both the permission are same then we print the permissions are same and permissions are printed. If the permissions are different then permissions of both the files are printed along with file names.

#!/bin/sh/
if [ $# -ne 2 ]
then
   echo "No arguments"
elif [ ! -e $1 -o ! -e $2 ]
then
   echo "FIle does not exist"
else
   p1=`ls -l $1|cut -c2-10`
   p2=`ls -l $2|cut -c2-10`
   if [ $p1 = $p2 ]
   then
     echo "File permissions are equal & is $p1"
   else
     echo "File permissions are not equal"
     echo "1st file $p1"
     echo "2nd file $p2"
   fi
fi

The output of the program is:

$sh file.sh p1.pl p2.pl

File permissions are not equal
1st file rw-rw-r--
2nd file rwxrwxrwx


$sh file.sh p1.pl p3.pl
File permissions are equal & is rw-rw-r--

 

READ  Basics of Shell Script

3. Display only the names of all users who are logged in and also store the result in user.txt

#!/bin/sh/

echo `who | tee user.txt`

The output of the program:

$ sh user.sh
mahesh tty7 2018-11-27 22:01 (:0)

In the above program first, we display the currently logged in users using who command. Next, we use tee command which takes the output of who command and redirects the output to the user.txt file.