Previous post deleted because of improvement/editing of post.
What I need to do: The script will copy and paste a folder/file to another location, and also check & verify to make sure all data was copied successfully and no corruption or errors, and if successful and no corruption, i want it to give me the option to just copy the data(no deleting), or actually cut/paste and delete the original source. And also create an error log inside the folder that the bash script is in, if any errors.
Once I run the bash script I need it to ask me the source file/folder.
Then ask me the destination file path.
Then it will estimate the time it will take and tell me what that will be, before initiating.
Then it will ask me y or n to proceed.
It also has a progress bar that shows the progress of the transfer in the terminal.
It will also have an alarm when the transfer is complete, and use an audio file that is in the same folder as the bash script. And the alarm sound can be stopped by pressing the space bar.
I asked both Gemini and Bing A.I. to create this script, it chose to use bash rather than python, and they both said it will work. It doesnt work. Just wanted to run this by the group to see if anyone wanted to improve on it/fix it.
#!/bin/bash
# Function to play sound and allow stopping with space key
play_sound_with_stop() {
local script_dir=$(dirname "$0")
local sound_file=$(find "$script_dir" -name "*.mp3" | head -n 1)
if [ -f "$sound_file" ]; then
sudo ffplay -nodisp -autoexit "$sound_file" &
sound_pid=$!
echo "Press the space key to stop the alarm."
while kill -0 $sound_pid > /dev/null 2>&1; do
read -n 1 -s key
if [[ $key == " " ]]; then
sudo kill $sound_pid
echo "Alarm stopped."
break
fi
done
else
echo "No MP3 file found in the script directory."
fi
}
# Function to get a valid folder path from the user
get_valid_folder() {
local folder
while true; do
read -p "Enter $1 folder: " folder
if [ -d "$folder" ]; then
echo "$folder"
break
else
echo "No such file or directory. Please try again."
fi
done
}
# Get source and destination folders from user
source_folder=$(get_valid_folder "source")
destination_folder=$(get_valid_folder "destination")
# Ask user if they want to copy or cut (move) the data
read -p "Do you want to copy or cut (move) the data? (copy/cut): " transfer_type
# Estimate transfer speed (assuming a fixed transfer speed for simplicity)
total_size=$(du -sb "$source_folder" | awk '{print $1}') # Get total size of source folder in bytes
transfer_speed=100000000 # 100 MB/s in bytes (adjust as needed)
estimated_time=$(echo "scale=2; $total_size / $transfer_speed" | bc -l) # Estimate time in seconds
echo "Estimated transfer time: $estimated_time seconds (This is a rough estimate)"
# Define the log file path
log_file="$(dirname "$0")/error_log.txt"
# rsync command with options
read -p "Proceed with transfer? (y/N) " -r response
if [[ $response =~ ^([Yy])$ ]]; then
sudo rsync -av --checksum --log-file="$log_file" --protect-args "$source_folder/" "$destination_folder/" | pv -W . &
transfer_pid=$!
# Print continuously updated estimated time remaining
while kill -0 $transfer_pid > /dev/null 2>&1; do
elapsed_time=$(ps -p $transfer_pid -o etimes=)
remaining_time=$(echo "scale=2; $estimated_time - $elapsed_time" | bc -l)
echo "Estimated time remaining: $remaining_time seconds"
sleep 2 # Update time every 2 seconds
done
# Check rsync exit status and provide specific error messages
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Transfer completed successfully."
play_sound_with_stop # Play sound notification with stop functionality
if [[ $transfer_type == "cut" ]]; then
sudo rm -rf "$source_folder" # Remove source folder only if transfer was successful and user chose to cut
fi
else
echo "Transfer failed with exit code $exit_code. Please refer to rsync documentation for details."
fi
fi