2017-01-13 2 views
0

現在のフォルダを同じディレクトリにリストし、正しいフォルダを選択するための番号を入力するようにユーザに表示したい。フォルダのセットから必要なフォルダを選択するようにユーザに指示する

Please select a Folder eg, 1,2,3. 
1. Folder Name 1 
2. Folder 2 
3. Folder 3. 

私はまた、実際のフォルダ名に戻る入力例1に変換できるようにしたいと思いますので、私はでき

cd ./$foldername/ 

私がしようとしたが、それが成功しなかった

#!/bin/bash 
printf "Please select folder:\n" 
select d in */; do test -n "$d" && break; done 
cd "$d" && pwd 

エラーが発生しました。「選択:見つからない」 どうしてですか?どこが間違っていますか?

+0

あなたは試しましたか? – Inian

+0

はい...どこかで失敗しました – Sneha

+1

printf "フォルダを選択してください:\ n" * /を選択してください。テストする-n "$ d" && break; done cd "$ d" && pwd – Sneha

答えて

0

これはあなたの後ろのようなものですか?

#put directories into an array 
folder_list=($(find /$foldername/* -maxdepth 0 -type d -exec basename {} \;)) 
#loop the output to prevent errors in the user's input 
while true; do 
    #clear the screen for each loop iteration 
    tput clear 
    #print the contents of the array and number them 
    printf '%s\n' "${folder_list[@]}" | cat -n 
    #read user input and store response as variable 
    read -p $'Please select a Folder eg, 1,2,3.\n>' REPLY 
    #check if variable is zero or not a number, if so restart the loop 
    #check if variable is within the range of array content 
    if ((REPLY)) && ((REPLY<=${#folder_list[@]})); then 
     #if it is then print it out and break the loop 
     echo ${folder_list[$((--REPLY))]} 
     break 
    fi 
    echo "Invalid response" 
done 

このメソッドは、二重括弧のために移植性がないことを指摘しておきましょう。

すべての編集について申し訳ありませんが、コードは効率的ではありませんでした。

+0

私は実際には、異なるフォルダから読み込まれているファイルのサイズ、変更された時間と名前を印刷するスクリプトを実行したいと思います。そのためには、現在のディレクトリからフォルダを選択するようにスクリプトを要求します。 – Sneha

+0

それでいいですね、ちょうど私がしたように配列の結果を取り出し、 'du'や' cd'のようなものでそれを行います。私が答えていた元の質問は、もう少し具体的であったかもしれません。 – Alek

関連する問題