I am learning Bash. I have a question on Bash Scripting. How to receive a list of strings, creates directories with those strings as names, and print out a “report” in this format:
created directories:
=========
…. (names of directories that were created)
failed to create:
============
…. (names of directories you failed creating)?
You can start with something like this:
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
I dont have shell prompt with me , but i hope below snippet would work .
#!/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
How to use it ?
your-prompt>./script_name.sh file_name_with_dir_names
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.