1

I'm not going to lie. This is for an assignment. I'm stuck and its kind a frustrating so i came here as my last resort so please help me out.

So, I need to make a script to find and print, that if the path is a relative or absolute path. I'm stuck in the last part where the prof want's me to do a command line substitute,which I have no idea how to do. This is what I have so far.

if [ "$#" -ne 1 ]; then
    echo 1>&2 "$0: please insert one valid file name;found $# ($*) "
    echo 1>&2 "Usage: $0 [Filename..]"
    exit 2
fi

if [ -z "$1" ] ; then
        echo 1>&2 "$0: file name cannot be empty; found $# ($*) "
        echo 1>&2 "Usage: $0 [filename...] "
        exit 2
fi

if [ ! -L "$1" ] ; then
        echo 1>&2 "$0: The pathname '$1' is not a symlink"
        echo 1>&2 "Usage: '$0' [symlink] "
        exit 2
fi

a=ls "$1" | awk '{ print $NF }'

if [ -z "$a" ] ; then
        echo 1>&2 "$0: Pathname is empty "
        exit 3
fi


    type=$(a)
    case "$b" in
    /* )    type='an Absolute Pathname' ;;
    * )     type='a Relative Pathname in the current directory' ;;  # the "default" match
    echo "pathname'$a' is $type"

    esac

This is screen shot of what he want us to do. enter image description here

please ask any questions if the question isn't clear enough. Thank you

  • Don't post screenshots of text if possible. – Tomasz Dec 3 '16 at 21:37
  • As your professor what to do if there are files called e.g. A -> B in the directory. Pro tip: Use readlink instead of parsing ls -l. – Kusalananda Aug 26 '18 at 7:11
1

The script is far from ready, but you're on the right track now.

if [ "$#" -ne 1 ]; then
    echo 1>&2 "$0: please insert one valid file name;found $# ($*) "
    echo 1>&2 "Usage: $0 [Filename..]"
    exit 2
fi

if [ -z "$1" ] ; then
        echo 1>&2 "$0: file name cannot be empty; found $# ($*) "
        echo 1>&2 "Usage: $0 [filename...] "
        exit 2
fi

if [ ! -L "$1" ] ; then
        echo 1>&2 "$0: The pathname '$1' is not a symlink"
        echo 1>&2 "Usage: '$0' [symlink] "
        exit 2
fi

a=$(ls -l "$1" | awk '{ print $NF }')

if [ -z "$a" ] ; then
        echo 1>&2 "$0: A Really Good Error Message."
        exit 3
fi

#    type=$a
    case "$a" in
    /*)    type='an Absolute Pathname' ;;
    *)     type='a Relative Pathname in the current directory' ;;  # the "default" match
    esac
    echo "pathname'$a' is $type"
  • Thanks for the answer. but do i have to remove the that if statement in order for this to work? I'm completely lost, I don't understand what he's asking us to do. could you please check out the new picture that I updated right now. – Saja Peiris Dec 3 '16 at 21:10
  • Well, no. As explained in the instruction this is to make sure the listing (the ls command) worked. And to exit if it failed. But I have no idea of what a good error message might be here. I can't think of any situation in which it would be applicable to print "Pathname is empty". If the link is dead, it would show the target anyhow. – Tomasz Dec 3 '16 at 21:33
  • nvm its working now. thank you so much. error was in the symbolink link part and u fixed it :) – Saja Peiris Dec 3 '16 at 21:45
  • Don't forget your last line. echo "$type symlink: '$1' -> '$a'" – Tomasz Dec 3 '16 at 21:52
  • @tomas ther is no need for an extra variable a. Also it's bad to store ls output in scripts. ls produces human readable output thus not suited in scripts. Either a="$1" or case "$1" in .. – Valentin Bajrami Dec 3 '16 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.