bash,无法在选择、循环内打开隐藏文件
如果我尝试在循环之外打开两个隐藏文件,则打开效果很好,但不能在下面第二个代码块中的 select 语句内打开。
#!/bin/bash
bbedit "./.bashrc"; # works fine here
bbedit "./.bash_profile"; # works fine here
但是,两者都在 select 语句中失败。我尝试使用 shopt,但这没有帮助。
#!/bin/bash
divider="-----------------------------------------------------------------"
echo -n "Admin "
sudo echo
echo
echo $divider
echo "| Enter an item number to open the following? |"
echo "| When done opening the files, enter the choice for ALL DONE |"
echo $divider
echo
shopt -s dotglob
done_flag="begin"
while [ "$done_flag" != "end" ];do
select item in "apache" "hosts" "php.ini" "~/.bash_profile" "~/.bashrc" "ALL DONE"; do
case $item in
apache )
sudo bbedit "/etc/apache2/httpd.conf";
break;;
hosts )
sudo bbedit "/etc/hosts";
break;;
php.ini )
sudo bbedit "/etc/php.ini";
break;;
~/.bash_profile ) # quotes here will fix the case statement
bbedit "./.bash_profile"; # hidden file will not open inside loop
break;;
~/.bashrc ) # quotes here will fix the case statement
bbedit "./.bashrc"; # hidden file will not open inside loop
break;;
"ALL DONE" )
done_flag="end";
break;;
esac
done
done
shopt -u dotglob
exit 0
If I try to open the two hidden files outside the loop, the open just fine, but not within the select statement in the second code block below.
#!/bin/bash
bbedit "./.bashrc"; # works fine here
bbedit "./.bash_profile"; # works fine here
However, both fail within the select statement. I tried using shopt, but that didn't help.
#!/bin/bash
divider="-----------------------------------------------------------------"
echo -n "Admin "
sudo echo
echo
echo $divider
echo "| Enter an item number to open the following? |"
echo "| When done opening the files, enter the choice for ALL DONE |"
echo $divider
echo
shopt -s dotglob
done_flag="begin"
while [ "$done_flag" != "end" ];do
select item in "apache" "hosts" "php.ini" "~/.bash_profile" "~/.bashrc" "ALL DONE"; do
case $item in
apache )
sudo bbedit "/etc/apache2/httpd.conf";
break;;
hosts )
sudo bbedit "/etc/hosts";
break;;
php.ini )
sudo bbedit "/etc/php.ini";
break;;
~/.bash_profile ) # quotes here will fix the case statement
bbedit "./.bash_profile"; # hidden file will not open inside loop
break;;
~/.bashrc ) # quotes here will fix the case statement
bbedit "./.bashrc"; # hidden file will not open inside loop
break;;
"ALL DONE" )
done_flag="end";
break;;
esac
done
done
shopt -u dotglob
exit 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,您需要在“~/.bashrc”和“~/.bash_profile”周围加上引号。
test.sh
中的示例代码:运行该代码:
You need quotes around "~/.bashrc" and "~/.bash_profile" in your case.
Example code in
test.sh
:Running that code: