如何使用指令將絕對路徑轉換為相對於目前工作目錄的路徑?
答案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