2014-08-31 67 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