bash,无法在select,inside循环内打开隐藏文件

时间:2012-02-11 02:22:38

标签: bash loops scripting

如果我尝试在循环外打开两个隐藏文件,则打开就好了,但不在下面第二个代码块的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

1 个答案:

答案 0 :(得分:1)

你需要在“〜/ .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