Write a BASH SCRIPT: All of the paths used must be relative. You must use variab
ID: 3844528 • Letter: W
Question
Write a BASH SCRIPT:
All of the paths used must be relative.
You must use variable names for the directories in all paths. This script must: Be able to take in 2 directories as positional parameters. The first one will be the source directory and needs to get stored in variable $source_dir. The second is the destination directory and needs to get stored in $dest_dir. Test to see if those variables are valid directories and if they are not send the user into a while loop to enter the directory and only allow exit when a valid directory is entered. Use touch to create files in the source directory to test with. Using curly brace syntax to expand a pattern create files in the pattern “a .error.log” “b .error.log” all the way through “z .error.log” and make sure they have the space in the file name. Use touch to create more files in the source directory to test with. Using curly brace syntax to expand a pattern create files in the pattern “1 _file.txt” “2 _file.txt” all the way through “9 _file.txt” and make sure they have the space in the file name. Test to see if the directory $dest_dir/logs exists and if it does not create it. Test to see if the directory $dest_dir/text exists and if it does not create it. Use a for in loop to iterate through all of the files in the source directory. Inside the for in loop test the files. Put the files ending in “.log” in $dest_dir/logs. Echo out a message indicating the file being moved and to what directory it is being moved. Put the files ending in “.txt” inside of $dest_dir/text. Echo out a message indicating the file being moved and to what directory it is being moved. For files not matching either condition echo out a message indicating you are leaving the file alone.
Explanation / Answer
Note: While passing the parameters to the shell script, please include the '/' at the end. For example, for directory home->user->Documents, pass the parameter as /home/user/Documents/
Script:
#Assigning passed parameters to variables
source_dir=$1
dest_dir=$2
#If passed parameter is not a valid directory
if ! [ -d "${source_dir}" ];then
echo "Enter a valid source directory: "
read source_dir
#Entering into loop to get valid directory name
while ! [ -d "${source_dir}" ]
do
echo "Enter a valid source directory: "
read source_dir
done
fi
#Similarly for destination directory
if ! [ -d "${dest_dir}" ];then
echo "Enter a valid destination directory: "
read dest_dir
while ! [ -d "${dest_dir}" ]
do
echo "Enter a valid destination directory: "
read dest_dir
done
fi
#Creating log files
for i in {a..z}
do
#Creating the file name with the path
filename="${source_dir}"${i} .error.log"" #Keeping the file name in double quotes because of space in name
eval "touch ${filename}" #Evaluating touch command to create file
done
#Similarly for text files
for i in {1..9}
do
filename="${source_dir}"${i} _file.txt""
eval "touch ${filename}"
done
#Create logs and text directory if not found
if ! [ -d "${dest_dir}logs" ];then
eval "mkdir ${dest_dir}logs"
fi
if ! [ -d "${dest_dir}text" ];then
eval "mkdir ${dest_dir}text"
fi
#Looping through all files in the source directory
files="${source_dir}*"
for file in ${files}
do
len=`echo ${#file}` #Finding length of the file name with path
if [ ${file: -4} == ".log" ];then #If it is a log file
len=`expr $len - 12` #Calculating length of the path (length of log file names are 12)
# In the move statement below, the file name is being reconstructed by using substrings of
# variable $file because to execute the move statement, we need to put the file names in
# double quotes because there are spaces in the file names.
# ${file: 0: ${len}} will give the substring from position 0 upto $len characters i.e., the path
# ${file: ${len}} will give the substring from position $len upto the end of the string i.e., the
# file name which is included into double quotes. To include " in string we're using (backslash).
eval "mv ${file: 0: ${len}}"${file: ${len}}" ${dest_dir}logs" #Moving file
echo "File ${file} moved to directory ${dest_dir}logs" #Appropriate message
#Similarly for text files
elif [ ${file: -4} == ".txt" ];then
len=`expr $len - 11` #The length of file names are 11.
eval "mv ${file: 0: ${len}}"${file: ${len}}" ${dest_dir}text"
echo "File ${file} moved to directory ${dest_dir}text"
else #For other files
echo "Leaving file ${file} alone."
fi
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.