If -z is used, I want the source-file to be compressed/decompressed(if it's already compressed) and copied over to the target directory.
I think the problem is with the mv command in the functions.
I've ran it through shell checker and it doesn't show any errors.
Is there a better condition for the if statements relating to -z being pressed to test if the file is compressed or not?
#!/bin/bash
# Script name: cpc.sh
# Written by: Rayhaan J
option=$1
source=$2
dir=$3
function_compress() {
gzip -k $source
mv $source".gz" $dir
echo "Source file '$source' has been compressed to directory '$dir'"
}
function_decompress() {
gzip -d $source
mv $source $dir
echo "Source file '$source' has been decompressed to directory '$dir'"
}
if [[ $# -ne 3 ]]; then
echo "Error: Incorrect number of arguments"
echo "Usage: cpc [-cz][source-file][target-directory]"
elif [[ ! -f $source ]]; then
echo "Error: The source file '$source' does not exist."
elif [[ ! -d $dir ]]; then
echo "The target is not a directory!"
read -p "Would you like to create the directory?" answer
if [[ $answer = y ]]; then
mkdir "$dir"
else
echo "Error: Target Directory does not exist"
echo "Usage: cpc [-cz][source-file][target-directory]"
fi
fi
while [[ $# -eq 3 ]] && [[ -f $source ]] && [[ -d $dir ]]; do
if [[ $option = "-c" ]]; then
cp $source $dir
echo "Source file '$source' has been copied to directory '$dir'."
elif [[ $option = "-z" ]] && [[ "${source##*.}" = "gz" ]]; then
function_decompress "$@"
elif [[ $option = "-z" ]] && [[ "${source##*.}" != "gz" ]]; then
function_compress "$@"
else
echo "Error: The first argument has to be '-c' or '-z'"
echo "Usage: cpc [-cz][source-file][target-directory]"
fi
break
done
Aucun commentaire:
Enregistrer un commentaire