2

I'm looking to remove the '/' from the end of a variable so I can check if a file is a symlink to a directory. I've tried just about every method I could think of or find online, is there something I'm missing?

If I check if a file is a symlink with the '/' on the end, it treats it as a directory. This can be checked by running:

if [ -L symtest/ ] ; then echo "symlink"; fi

where symtest is a symlink to a directory. The above outputs nothing.

When I remove the '/', it works fine and outputs "symlink":

if [ -L symtest ] ; then echo "symlink"; fi

My question is, is there a way to remove the '/' from the name when it's passed as a variable to a function?

The function would look something like:

function is_it_a_symlink() {
    if [ -L $1 ] ; then
        echo "This file is a symlink!"
    else
        echo "This file is not a symlink!"
    fi
}

Thanks in advance.

7
  • 1
    Note that POSIX's Pathname Resolution rules require the behaviour you observe. Commented Mar 10, 2017 at 1:40
  • 1
    @Alexander, please avoid linking the ABS as a resource -- it has a longstanding reputation of showcasing bad practices. (Using the gratuitous function keyword -- which breaks compatibility with POSIX sh but adds no value over the standardized function declaration syntax -- is onesuch). Commented Mar 10, 2017 at 1:46
  • 1
    @Alexander, ...actually, there are more serious bad practices in that particular page you linked as well. directorys=$@ loses boundary information -- one can't distinguish between ./yourscript "foo bar" "baz qux" and ./yourscript foo bar baz qux after using it; the linkchk function is missing quotes (needs to be "$1"/* and linkchk "$element"), and more. Commented Mar 10, 2017 at 1:48
  • 2
    @Alexander, ...a better link for test operators is wiki.bash-hackers.org/commands/classictest; another reliable resource is the Wooledge wiki, ie. mywiki.wooledge.org/BashGuide/TestsAndConditionals. There's also the POSIX specification for test at pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html Commented Mar 10, 2017 at 1:49
  • @CharlesDuffy Thanks, I will look into those links. Commented Mar 10, 2017 at 1:53

1 Answer 1

3

POSIX-ly, using parameter expansion to get rid of the last /:

${parameter%/}

So, your case:

[ -L "${1%/}" ]

External tool, sed:

sed 's_/$__' <<<"$1"

So:

[ -L "$(sed 's_/$__' <<<"$1")" ]

Similarly, awk:

awk '{sub("/$", "")} 1' <<<"$1"

So:

[ -L "$(awk '{sub("/$", "")} 1' <<<"$1")" ]
Sign up to request clarification or add additional context in comments.

3 Comments

That works perfectly! I had actually tried that earlier just without the quotation marks. Thanks!
To be safe, you should probably remove all trailing /; don't assume there is just one. ${1%%/}
@chepner, ...that works? I thought one had to use an extglob to match a sequence there. Actually, it looks to me like one does; thus, ${1%%+(/)}, after shopt -s extglob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.