You are not logged in.
When you run cp -i in an interactive script and the destination file already exists, is it possible to get the user's reply to the "cp: overwrite" prompt?
Offline
well u can do something as
# it checks for the file
if [ -e "${destination_file}" ] ;
then
printf "are you sure you want to overwrite (y/n)?\n";
# i guess its scanf or scan or even read
scanf x;
[[ "${x}" == "y" ]] && cp -f "${source_file}" "${destination_file}" || echo "skipped" :
fi
and notice this is a "bash-pseudocode"
If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.
Simplicity is the ultimate sophistication.
Offline
Thanks quarkup, but you don't know of a way to get the user's reply directly from a "cp -i fromfile tofile" command? I doubt it's possible, but I thought I'd check.
Offline
Never tried it, but you might look into seeing what
$?
returns when cp -i is answered with y or n and compare the results.
Display exit status of the command:
$ echo $?
I'm not at my Linux machine at the moment, or I would test it for you.
Offline
Thanks, I've tried checking that -- $? == 0 either way.
Offline
It IS possible........ (i'm at home now ) ...... I wrote a silly little script to test .....
#!/bin/bash
# Script to determine the answer to the interactive cp -i command.
# This example copies the "test.sh" program to "test3".
# Use the command "script" to log what your program is doing to "test.txt"
script -qc 'cp -i test.sh test3' test.txt
# Now, we grep for the line in the script file, and determine if it was y or n, and push it to a variable.
# We also remove that test.txt file so it doesn't clutter stuff up.
yesorno=`grep "cp: overwrite" test.txt | cut -d'?' -f2 | sed 's/^ *//;s/ *$//'` ; rm test.txt
# Ok...... lets test it and see if it works ! :)
echo "You answered : ${yesorno}"
exit 0
The output of the test script looks like this :
[crouse@Archie] ~
> ./cp.sh
cp: overwrite `test3'? n
You answered : n
[crouse@Archie] ~
[crouse@Archie] ~
> ./cp.sh
cp: overwrite `test3'? y
You answered : y
[crouse@Archie] ~
So, it's a roundabout way of doing things....but it DOES give you the exact answer you reponded to the question with.
Last edited by crouse (2009-11-08 03:40:04)
Offline
@crouse: That's very cool! As you say, it's a roundabout solution and not good for serious use, but it's great as a proof of concept!
Offline
.
Last edited by fumbles (2020-09-26 11:32:16)
Offline