2017-06-13 41 views
0

我正在學習Bash。我對Bash腳本有疑問。如何接收字符串列表,創建與這些字符串作爲名稱的目錄,並在此格式打印出一張「報告」:在Bash中創建目錄使用給定字符串名稱的腳本

success_files='' 
failed_failes='' 
while read file_name 
do 
    mkdir $file_name 
    if [ $? -eq 0 ] 
    then 
    success_files="$success_files $file_name" 
    else 
    failed_files="$failed_files $file_name" 
    fi 
done < ./file_names.txt 
+0

請格式化你的問題,以使其更具可讀性:) – RaphaMex

+0

向我們展示你的企圖,否則它讀起來就像你問我們做你的作業。 –

回答

0

你可以像這樣的東西開始沒有殼提示與我,但我希望下面的代碼段將工作。

#!/usr/bin/bash 
    filename="$1" 
    while read -r line 
    do 
    mkdir $line 
    if [ $? -ne 0 ] ; then 
     createdDir="$createdDir"$'\n'"$line" 
    else 
     failedDir="$failedDir"$'\n'"$line" 
    fi 
     done < "$filename" 
    echo "created directories: \n" 
    echo $createdDir 
    echo " failed to create: \n" 
    echo $failedDir 

如何使用它?

您提示符> ./ script_name.sh file_name_with_dir_names

0

我:

created directories: 
========= 
…. (names of directories that were created) 
failed to create: 
============ 
…. (names of directories you failed creating)?