1

I need to do the following which I've already started:

  • Prompt user to enter a folder and see if it is readable
  • If file is not readable it needs to display a message and end
  • Then copy that folder into a backup folder
  • Read each line of the backup folder and display each line

Here's what I have but I keep getting a missing destination operand error.

echo -n "Please enter a file name:" read file if [ -r "$file" ] then echo [ $file ] >> /home/shawn/backupfile.txt while IFS= read -r line do printf '%s\n' "$line" done < "$file" else echo "File is not readable or does not exist" fi

0
cp $file >> backupfile.txt

Should be:

cp "$file" backupfile.txt

>> is used to append output, which is not what you need to do.

Other things:

  • Quote your variables: Use "$file". What will happen if user enters File Name With Spaces?
  • file=$file is a tautology. And unnecessary.
  • How do I display all the lines in the backup file? – DSH72 Nov 16 '15 at 23:03
  • @DSH72 mywiki.wooledge.org/BashFAQ/001 – muru Nov 16 '15 at 23:04
  • Where does the line display go in the script? Sorry very green with shell scripting – DSH72 Nov 16 '15 at 23:35
  • @DSH72 After the cp command? You're supposed to read the backup file, right? – muru Nov 16 '15 at 23:36
  • Yes that's part of the requirments – DSH72 Nov 16 '15 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.