-3

So if my user types in bash copy.sh copysource.txt copydest.txt - and the files are valid I want copysource to append to copydest and then show the file via cat

-if the files names are NOT correct or found...I want the user to be prompted to code below

#!/bin/bash
#Date: July 14 2016

if [ "$1" == "copysource.txt" ] && [ "$2" == "copydest.txt ]; then

cp $1 $2
echo "copies $1 to $2"
echo "contents of $2 are: "
cat $2

fi
#if the above was correct I want to script to stop
#otherwise if the info above was incorrect re: file names I want to user to be
#prompted to below code


clear
echo -n "Enter the source file name: "
read sourc


if [ "$sourc" == "copysource.txt" ]; then

    echo "The file name you entered was found!";

else
    echo "The file name you entered was invalid - please try again";
    echo -n "Enter the source file name: "
    read sourc
fi

echo -n "Enter the destination file name: "
read dest


if [ "$dest" == "copydest.txt" ]; then

    echo "The destination file name you entered was found!";

else
    echo "The destination file name you entered was invalid - please try         again";
    echo -n "Enter the destination file name: "
    read dest
fi


cp $sourc $dest
echo "copies $sourc to $dest"
echo "contents of $dest are: "
cat $dest

closed as unclear what you're asking by dhag, Stephen Harris, Wildcard, sam, Gilles Jul 14 '16 at 23:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I am having hard time to find question in your post – Rahul Jul 14 '16 at 19:32
  • @Rahul if user (in command line) types bash copy.sh copysource.txt copydest.txt (these are the correct files) I want them to merge and show as cat copydest.txt IF NOT...the user has to enter the correct info...my script does not work – Conor Martin Jul 14 '16 at 19:44
  • Would it not be logical to permit $2 to be a non-existing file, so as to be able to create new files, and then continue appending to them with the same command? – Alex Stragies Jul 14 '16 at 20:05
  • as a general rule, scripts that do what you're asking (repeatedly prompt the user if args are missing) are really annoying to use. it's far better to just print an error message and let the user use their shell's history recall and edit features to correct their mistake. – cas Jul 15 '16 at 2:43
0

The following checks that both files (passed in as $1 and $2) exist and concatenates $1 to $2 (I think that's what you're trying to do). If either of the files don't already exist the user will be prompted to enter the sourc and dest filenames in turn until they enter existing files - ctrl-c to exit otherwise!

#!/bin/bash
#Date: July 14 2016

# Use -f to test if files exist
if [[ -f "$1" ]] && [[ -f "$2" ]]; then
    echo "Appending contents of $1 to $2"
    # Use cat to take the contents of one file and append it to another
    cat "$1" >> "$2"
    echo "Contents of $2 are: "
    cat "$2"
    # Don't want to go any further so exit.
    exit
fi


# You could consider merging the logic above with that below
# so that you don't re-test both files if one of them exists, but the
# following will work.

clear
sourc=''
dest=''

while [[ ! -f "$sourc" ]]
do  
    echo -n "Enter the source file name: "
    read sourc

    if [ -f "$sourc" ]; then
        echo "The file name you entered was found!";
    else
        echo "The file name you entered was invalid - please try again";
    fi
done

while [[ ! -f "$dest" ]]
do
    echo -n "Enter the destination file name: "
    read dest

    if [ -f "$dest" ]; then
        echo "The file name you entered was found!";
    else
        echo "The file name you entered was invalid - please try again";
    fi
done

cat "$sourc" >> "$dest"
echo "Appending $sourc to $dest"
echo "Contents of $dest are: "
cat "$dest"
  • thanks a lot - I'm new to this so this helped me greatly in advancing my logic. Cheers mate! – Conor Martin Jul 14 '16 at 20:14
0

Here is a shell function, that should do the trick

appendFile(){
  local iFile oFile;
  [ ! -f "$1" ] || [ -z "$2" ] && {
    while [ ! -f "$iFile" ]; do read -p "Input file: " -i "$1" iFile; done;
    read -i "$2" -p "Output file: " oFile;
  };
  cat "${iFile-$1}" >> "${oFile-$2}";
  cat "${oFile-$2}";
}

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