Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

FOR UNIX Preparation: Create a file and type some junk in it. Make at least five

ID: 3864986 • Letter: F

Question

FOR UNIX

Preparation:

Create a file and type some junk in it.

Make at least five soft links to this file using completely arbitrary names.

Script

Script Name: softLinkFinder.txt

Arguments: A filename.  The files for which we want to find soft links.

Validation: The minimum validation requirements are:

Ensure that exactly one argument is entered.

Check that the only argument is the name of a file that exists.

Body Section: Use the ls –l and grep command to find all the soft links attached to $argv[1] positional parameter.  Note that a file of type softlink is distinguished by lowercase l.  Be sure to find the soft links to the file defined in $argv[1] and not other files.

Explanation / Answer

#!/bin/sh


if [ "$#" -eq 0 ];then
   echo "No arguments supplied"
   exit 0
elif [ "$#" -ne 1 ]; then
   echo "Too many arguments provided."
   exit 0
elif [ ! -f "$1" ]; then
   echo "File name does not exists."
   exit 0
fi

links=$(ls -l | grep $1 | awk '{print $9}'|sed '$d')

if [ "$links" = "" ];then
   echo "No softlinks exists for given file."
   exit 0
fi

echo "The softlink[s] for the given file is[are] : "
echo $links


exit 1

--------------------------------------------------------------------

Explanation of the above script:

First we are checking whether the arguments are supplied or not.

If no argument suppied, we will exit.

Next we provide a check if user suppiled more than one argument i.e argumnet apart from file name

Next we check wether the file exists or not.

If file exist, then we find the links for file using below command metioned above

ls -l | grep $1 | awk '{print $9}'|sed '$d'

the awk command extract the 9 column in the result and sed '$d' command delete a last row which is filebname itself.At last if there is any softlinks for the given file, we will print.