Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a symlink target matches a specific path?

Tags:

I'm creating a bash script to check if a symlink target matches a specific path so, in case it doesn't match, script removes the symlink. I've tried with readlink:

#!/bin/env bash  target_path=$HOME/Code/slate/.slate.js  if [ `readlink $HOME/.slate.js` == "$target_path" ]; then     rm -rf "$HOME/.slate.js" fi 

but it doesn't work:

%source test test:5: = not found 
like image 867
Alex Guerrero Avatar asked Nov 08 '13 13:11

Alex Guerrero


People also ask

How do I find the path of a soft link?

Showing soft link using Find command in Unix When you use the find command with option type and specify the type as small L ( l for the link), it displays all soft links in the specified path.

Which command follows symlinks?

In order to follow symbolic links, you must specify ls -L or provide a trailing slash. For example, ls -L /etc and ls /etc/ both display the files in the directory that the /etc symbolic link points to. Other shell commands that have differences due to symbolic links are du, find, pax, rm and tar.


1 Answers

You should use double quotes as follow when you compare strings (and yes, the output of readlink $HOME/.slate.js is a string):

[ "$(readlink $HOME/.slate.js)" = "$target_path" ] 
like image 177
Radu Rădeanu Avatar answered Nov 11 '22 16:11

Radu Rădeanu