Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

called file1 and a subdirectory dir1 under the current directory. Assume
that file1 has a permission of r-x (read and execute permission) and
dir1 has a permission of rwx (read, write, and execute permission). The
code for the shell program would look like this:


Click here to view code image
#!/bin/sh
if [ -d $dir1 ]; then
echo ""dir1 is a directory"
else
echo ""dir1 is not a directory"
fi


if  [   -f  $dir1   ];  then
echo ""dir1 is a regular file"
else
echo ""dir1 is not a regular file"
fi

if  [   -r  $file1  ];  then
echo ""file1 has read permission"
else
echo ""file1 does not have read permission"
fi

if  [   -w  $file1  ];  then
echo ""file1 has write permission"
else
echo ""file1 does not have write permission"
fi

if  [   -x  $dir1   ];  then
echo ""dir1 has execute permission"
else
echo ""dir1 does not have execute permission"
fi

If you execute the shell program, you get the following results:


Click here to view code image
dir1 is a directory
file1 is a regular file
file1 has read permission
file1 does not have write permission
dir1 has execute permission


Logical Operators

You use logical operators to compare expressions using Boolean logic—that

Free download pdf