2012-02-11 27 views
0

如果我試圖打開循環外部的兩個隱藏文件,打開就好,但不在下面第二個代碼塊中的select語句內。bash,無法在選擇內部打開隱藏文件,循環內

#!/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

在你的情況下,你需要引用「〜/ .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 
+0

有引號。我明白你的意思了... – 2012-02-11 02:36:53