-4

I am trying to create a version of file copy that allows the user to append the target file that already exists. The program takes two arguments: source-file and destination-file.

If the destination-file already exists, it must ask the user to choose between three options: overwrite destination-file, append destination-file or cancel.

It should make the following argument checks:

  • The number of arguments must be 2.
  • The source-file must be a regular file.
  • If the destination-file exists, it must be a regular file.

Can anyone help me how I would go about completing this task?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • If the user wants a copy, you do `cp sourcefile destfile`. If they want to append, you do `cat sourcefile >> destfile`. – Barmar Apr 17 '17 at 18:57
  • I suggest to take a look at `help test` to do (a), (b) and (c). – Cyrus Apr 17 '17 at 19:02

1 Answers1

0

An attempt to answer your 4 or 5 questions: In the future, please ask a single question per post and make an attempt at answering it as all of this is pretty easily answered through google.

How to check how many arguments are passed to the script


Information on "test". You can also use command man test to RTFM

Some examples:

  1. Testing if it's a regular file (from the previous link) returns true/false:

    [ -f myfile ]

  2. Testing if file exists (again from previous link) returns true/false:

    [ -a myfile ]


How to read user input

I think the "inline" bash version here is very nice for your needs:

read -p "Do you want to (C)opy, (A)ppend, or (E)nd? " answer
case ${answer:0:1} in
    c|C )
        cp file1 file2
    ;;
    a|A )
        cat file1 >> file2
    ;;
    * )
        echo "That's not an option, yo"
    ;;
esac

How to copy a file (after user makes selection)

cp file1 file2

How to append a file (after user makes selection)

cat file >> file2
Community
  • 1
  • 1
JNevill
  • 46,980
  • 4
  • 38
  • 63