In: Computer Science
Create files with the following names : test1, test2, test3. Create a symbolic link to test2 and name it test4 Create a directory and name it test5.
Write a shell script to perform the following tasks:
• check if a file named test6 exist. If not, it should create it.
• Display a text to indicate whether test4 is symbolic link or not
• Display a text to indicate whether test2 is directory or not
• Display a text to indicate whether test1 is older than test4
• Display a text to indicate whether tes3 is a character device file or not.
To create the 3 files, we use the touch command as:
touch test1
Then we create the symbolic link using ln command:
ln -s test2 test4
Finally we create directory by:
mkdir test5
Now, we have the following structure before our script:
We have written the following script in script.sh :
SCRIPT:
FILE=test6
#To check if file exists, we use the -f operator
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist, creating it."
touch $FILE
fi
#To check if file is a symbolic link & it exists, we use the -L
operator
FILE=test4
if [ -L "$FILE" ]; then
echo "$FILE is a symbolic link."
else
echo "$FILE is not a symbolic link."
fi
FILE=test2
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
else
echo "$FILE is not a directory."
fi
# The -ot operator compares if first file is older than the second
file specified
if [ "test1" -ot "test4" ]
then
echo "test1 is older than test4."
else
echo "test1 is not older than test4."
fi
FILE=test3
if [ -c "$FILE" ]; then
echo "$FILE is a character device file."
else
echo "$FILE is not a character device file."
fi
Script Output:
(*Note: Please up-vote. If any doubt, please let me know in the comments)