2009-06-29 110 views
4

我想提示用戶輸入目錄名稱,並讓它們能夠製表完成到一個有效的目錄。在bash腳本中讀取和製表目錄名稱

到目前爲止,我已經使用「read -e」工具爲文件和目錄執行了tab-completion。我只希望當用戶按下標籤時自動完成目錄。

這是我有:

echo "Enter a directory" 
read -e -p "> " DEST 

我怎樣才能讓慶典只有當用戶按下標籤,而不是文件目錄列表返回目錄列表?

回答

3

爲您提供很多靈活性的替代方法是使用compgen;詳情請參閱我的回答here

+3

聽起來不錯。任何想法如何將它與Tab鍵集成? – nfm 2009-10-03 00:36:28

0

這是我對問題的快速回應。出於某種原因,我必須實際使用bash,而不是在我的電腦上使用pushd和popd。我覺得這個評論已經足夠讓我不要再進一步解釋了。

#!/bin/sh 
tempdir=`mktemp -d` 

# save the current directory 
pushd . 

# make a new folder, then make a bunch of new directories 
# mirroring those in our current directory 
for i in $(find . -type d); do mkdir "$tempdir/$i" ; done 

# change to the temporary directory 
cd "$tempdir" 

echo "Enter a directory" 
read -e -p ">" DEST 

echo "You told me $DEST" 

# return to our original directory 
popd 

# clear out that temporary directory we made 
rm -rf "$tempdir" 

雅各的反應可能更有效,比我乾淨。

+1

read -e -p自動完成目錄_and_文件。您也可以使用`cd -`而不是推送和彈出原始目錄。 – nfm 2009-10-03 00:30:46