2014-08-31 62 views
-1

shell腳本是否可以提示用戶在哪裏創建目錄?Bash腳本提示輸入文件夾的位置

echo -n "Enter folder path location::::" 
+0

可能重複shell腳本?](http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script) – 2014-09-01 19:46:34

回答

1

試試這個:

while read -ep "Enter folder path location: " file_dir; do 
    if [ -d "${file_dir}" ]; then 
     echo "${file_dir} already exists - please enter a valid directory path." 
    else 
     mkdir -p "${file_dir}" 
     break 
    fi 
done 
的[我如何在Linux提示輸入
+0

感謝這很好,我正在尋找。 – jquerynoob 2014-09-05 21:41:57

1
echo -n 'Enter folder path location: ' 
read folder_path 
echo "You entered $folder_path" 
1
echo -n "Enter folder path location::::" 
read folderName 
echo "$folderName is the directory name inserted" 

您可以添加許多控件。 我認爲你最好開始閱讀一些bash手冊和書籍。像Advanced BASH Scripting Guide.

+0

我不同意閱讀高級BASH的建議腳本指南,但我沒有看到這個答案中有什麼值得的downvote。 – chepner 2014-08-31 18:55:43

1

東西,你可以使用讀取的提示功能,如:

err() { 
    echo "Error: [email protected]" >&2 ; return 1 
} 
while : 
do 
    read -r -p "Enter directory name > " newdir 
    #check already exists 
    [[ ! -e "$newdir" ]] || err "$newdir exists" || continue 

    #if want to find real parent directory 
    #real="$(cd "$(dirname "$newdir")" && /bin/pwd -P)" || continue 

    #some other safety tests here if need 

    #create a directory, if success - break the cycle 
    mkdir "$newdir" && break 

    #otherwire repeat the question... 
done 
#ok