如何使用命令将绝对路径转换为相对于当前工作目录的路径?
答案1
答案2
只是为了“自己动手”的乐趣
here=/dir1
there=/dir1/dir4/dir5/my.file
root=""
if [ ! -z $(grep "^$here" <<<$there) ]; then
root="./"
else while [ -z $(grep "^$here" <<<$there) ]; do
here=${here%/*}
root=${root}../
done
fi
echo $root$(sed "s|^$here/||g" <<<$there)
./dir4/dir5/my.file
并为
here=/dir1/dir2/dir3
../../dir4/dir5/my.file