0

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
4
  • how is this connected to java? Commented Dec 16, 2017 at 18:45
  • cp SOURCE DEST Commented Dec 16, 2017 at 18:49
  • you are missing a space between source and destination files Commented Dec 16, 2017 at 19:02
  • 1) You need to put the case handling for different $REPLY values inside the case...esac statement. 2) You need to read the filename in from the user; currently, neither $filename nor $file has any value (and are they supposed to be different variables?). 3) You need a space between the arguments to cp. Commented Dec 16, 2017 at 19:06

1 Answer 1

2
  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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your explanation I now understand where I went wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.