Skip to content
Advertisement

having an issue with my bash script

im writing a script that allows the user to create a backup of a file they choose by allowing them to input the file name. the file will then have backup followed by being date stamped at the end of it’s name and saved on the home drive. but whenever i try to run it i get an error: cp: missing destination file operand after ‘_backup_2017_12_16’

here’s my code:

title="my script 3" 
prompt="Enter:"        
options=("create a backup of a file")  
echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do 

    case "$REPLY" in
esac
    cp "$filename""${file}_backup_$(date +%Y_%m_%d)"
done

Advertisement

Answer

  1. Your case statement is currently empty. You need it to handle your chosen option
  2. There needs to be a space between arguments: cp source dest
  3. If you are using array for the options, you can also put Quit in there
  4. If you choose the option to create a backup, you need to prompt the user to enter a filename. read command is used to get user input

Putting it all together, your script could look like this:

#!/usr/bin/env bash

options=("Backup" "Quit")
prompt="Enter: "        
title="My script 3" 

echo "$title"
PS3=$prompt

select opt in "${options[@]}"; do
   case $opt in
      "Backup")
          IFS= read -r -p "Enter filename: " filename 
          cp -- "$filename" "${filename}_backup_$(date +%Y_%m_%d)" && echo "Backup created..."
          ;;
        "Quit") break ;;
             *) echo "Wrong option..." ;;
   esac
done
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement