Abrar Sayeed
Abrar Sayeed

Reputation: 73

how to test a string contained in a variable with a readlink output?

A user enters a file name --> $1. i compare that with path2. If both strings match, it echoes "Same file". I have included the code below.

path="$(readlink -f $1)"
path2="path/to/$USER/file"

if [[ $path == $path2 ]] ; then
    echo "Same file"

However, when i run this with the input file same as path2, it still shows a mismatch as it is comparing path/to/username/file with path/to/username\file. How do i get the path2 to output in the same format as path? i.e. path2 should output as path/to/username/file instead of path/to/username\file

Thank you!

Upvotes: 0

Views: 278

Answers (1)

Léa Gris
Léa Gris

Reputation: 19625

Use -ef that also works with ksh93, zsh, bash… rather than readlink that is GNU coreutils specific :

if [ "$1" -ef "path/to/$USER/file" ] ; then
    echo "Same file"

help test

FILE1 -ef FILE2 True if file1 is a hard link to file2.

Upvotes: 5

Related Questions