You are not logged in.

#1 2009-11-07 16:54:53

nh
Member
From: England
Registered: 2008-07-09
Posts: 45

Bash cp -i : how to get the user's reply?

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

#2 2009-11-07 18:58:02

quarkup
Member
From: Portugal
Registered: 2008-09-07
Posts: 497
Website

Re: Bash cp -i : how to get the user's reply?

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

#3 2009-11-07 19:42:15

nh
Member
From: England
Registered: 2008-07-09
Posts: 45

Re: Bash cp -i : how to get the user's reply?

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

#4 2009-11-07 20:23:41

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Bash cp -i : how to get the user's reply?

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. wink

Offline

#5 2009-11-07 20:27:04

nh
Member
From: England
Registered: 2008-07-09
Posts: 45

Re: Bash cp -i : how to get the user's reply?

Thanks, I've tried checking that -- $? == 0 either way.

Offline

#6 2009-11-08 02:23:01

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Bash cp -i : how to get the user's reply?

It IS possible........ (i'm at home now wink  ) ...... 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

#7 2009-11-08 17:04:17

nh
Member
From: England
Registered: 2008-07-09
Posts: 45

Re: Bash cp -i : how to get the user's reply?

@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

#8 2009-11-14 05:48:09

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: Bash cp -i : how to get the user's reply?

.

Last edited by fumbles (2020-09-26 11:32:16)

Offline

Board footer

Powered by FluxBB