这是我第一次用bash编程做些什么。作为第一个例子,我试图从我的.bashrc中获取.bash_profile --即使~/.bashrc是一个符号链接。
.bash_profile
if [ -f ~/.bashrc ] && ! [ -L ~/.bashrc ]
then
# ~/.bashrc is a regular file. Source it!
source ~/.bashrc
echo "~/.bashrc found."
elif [ -L ~/.bashrc ]
then
# ~/.bashrc is a symbolic link.
# Recursivly follow symbolic links.
location="~/.bashrc"
while [ -L $location ]
do
# QUESTION: Control-Flow never reaches this point.
# Follow link on macOS.
location="$(readlink $path)"
done
# Check if final target is regular file. Source it!
if [ -f $location ]
then
source $location
echo "Symlink to .bashrc found."
fi
else
echo "No valid .bashrc found."
fi这就是我期望我的代码所做的:
~/.bashrc不是一个符号链接,而是一个常规文件:源文件。~/.bashrc是一个符号链接:
作为测试,我创建了一个指向原始文件~/.bashrc的符号链接.dotfiles/.bashrc。我的代码按预期进入elif,但遗憾的是从未进入while-loop的主体(正如我所预期的,因为~/.bashrc是一个符号链接)。
这里发生什么事情?我认为location的变量赋值在某种程度上是错误的。
发布于 2016-09-17 21:25:47
取代:
location="$(readlink $path)"通过以下方式:
location="$(readlink $location)"备注:
path从未定义过。我相信你打算将readlink应用于locationreadlink (在Linux、自制软件等上可用),那么可以使用-f选项来消除循环的需要。~/需要在引号之外:
位置=~/..bashrc
一旦完成,脚本中引用location的部分应该是双引号。例如:
Location=“$”(重新链接“$location”)
这变得非常重要,例如,如果文件或路径名称包含空格。发布于 2016-09-17 21:28:48
这导致了以下问题:
location="~/.bashrc"倾斜扩展不发生在双引号中,也不发生在
[ -L $location ]都不是。
所以,不要在赋值中使用双引号,或者使用
location="$HOME/.bashrc"或者类似的。
https://stackoverflow.com/questions/39551754
复制相似问题