我在/ etc/bashrc中的pathmunge()函數中映射。這非常有用,因爲它可以使PATH中的重複條目保持不變。在bash中的嵌套變量替換
但是這也應該是其他變量的模式:CDPATH,LD_LIBRARY_PATH不應該有重複的條目。所以我試圖用相同的方式編寫一個varmunge函數。這是它:
# varmunge() allows a one and only one addition of entries into the
# a PATH like environment variable. Good use cases for this function
# are: PATH, CDPATH, LD_LIBRARY_PATH
# example: varmunge PATH $HOME/bin [after | before]
#!/bin/sh
varmunge() {
case ":$1:" in
*:"$2":*)
;;
*)
if [ "$3" = "after" ] ; then
export $1=\$$1:$2 # These both fail
# ${1}=${$1}:$2
else
export $1=$2:\$$1 # blech.
# ${1}=$2:${$1}
fi
esac
}
但唉,它不起作用。任何人都可以修理我可憐的巴斯特斯?
謝謝...
發佈在其他情況下,正確的bash腳本可能需要它。
#!/bin/sh
varmunge() {
case ":${!1}:" in
*:"$2":*)
;;
*)
if [ "$3" = "after" ] ; then
eval ${1}=${!1}:$2
else
eval ${1}=$2:${!1}
fi
esac
}
varprint() {
echo "${1}=${!1}"
}
# test varprint
VARTEST="path1:path2"
varprint VARTEST
# test varmunge
varmunge VARTEST path1
varmunge VARTEST path1 after
varmunge VARTEST path2
varmunge VARTEST path2 after
varmunge VARTEST path3
varmunge VARTEST path3 after
varmunge VARTEST path3
varmunge VARTEST path4 after
varmunge VARTEST path4
varprint VARTEST
謝謝!這很好。 – natersoz