我正在创建一个bash脚本,以检查符号链接(symbolic link)目标是否与特定路径匹配,因此,如果不匹配,脚本会删除符号链接(symbolic link)。我已经尝试过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

但它不起作用:
%source test
test:5: = not found

最佳答案

比较字符串时,应使用双引号,如下所示(是的,readlink $HOME/.slate.js的输出是字符串):

[ "$(readlink $HOME/.slate.js)" = "$target_path" ]

10-07 13:38