bash,无法在选择、循环内打开隐藏文件

发布于 2025-01-04 03:56:15 字数 1521 浏览 1 评论 0原文

如果我尝试在循环之外打开两个隐藏文件,则打开效果很好,但不能在下面第二个代码块中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2025-01-11 03:56:15

在您的情况下,您需要在“~/.bashrc”和“~/.bash_profile”周围加上引号。

test.sh 中的示例代码:

#!/bin/bash
select item in "~/.bashrc" "hosts"; do
    case $item in 
        hosts )
            echo hosts
            break;;
        ~/.bashrc )
            echo no quotes
            break;;
        "~/.bashrc" )
            echo quotes
            break;;
    esac
done

运行该代码:

$ ./test.sh 
1) ~/.bashrc
2) hosts
#? 1
quotes

You need quotes around "~/.bashrc" and "~/.bash_profile" in your case.

Example code in test.sh:

#!/bin/bash
select item in "~/.bashrc" "hosts"; do
    case $item in 
        hosts )
            echo hosts
            break;;
        ~/.bashrc )
            echo no quotes
            break;;
        "~/.bashrc" )
            echo quotes
            break;;
    esac
done

Running that code:

$ ./test.sh 
1) ~/.bashrc
2) hosts
#? 1
quotes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文