Sponsored Content
Top Forums UNIX for Dummies Questions & Answers BASH: Interactive "cp" (and "mv") in a loop Post 302504070 by SilversleevesX on Sunday 13th of March 2011 01:00:40 PM
Old 03-13-2011
BASH: Interactive "cp" (and "mv") in a loop

Hi.

I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the list to the destination directory. Casually-speaking, it "barreled right through" the list as if the -i wasn't there.

Here's part of the copy-file script.
Code:
#!/bin/bash

while read 'line';
do
n=$(echo $line)
file=${n%,*}
dest=${n#*,}
if [[ "$dest" == "" ]]; then continue; fi
echo -e "Copying $file now."
cp -ipPv $file $dest

done<copylist.txt

I know the 'pPv' options are more or less unnecessary, particularly the capital 'P', but as I write and use many of my scripts both in Cygwin and GNOME, it seems more prudent to me to include them.

In a loop such as this, would I be better off preventing overwrite (and changes of mod date, file-length, etc to the file that ended up at the destination) to write in an if/then/fi that checked for the file in the destination before copying? Something like this?
Code:
if [ -e "$dest/$file" ]; then
echo -e "Do you want to overwrite $file? (y/n)"
read arPLy
if [[ "$arPLy" != "n" ]]; then
cp -pPv $file $dest
else
continue
fi
fi

Or is there a way to introduce (I hesitate to say "force") a pause in that loop at the "cp -i" stage?

Hope someone can help and soon.

BZT
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

6. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

7. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

8. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

9. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies
lp(1)							      General Commands Manual							     lp(1)

Name
       lp - send requests to an LP line printer

Syntax
       lp [-c] [-d dest] [-n number] [-] [files]

Description
       The command arranges for the named files and associated information (collectively called a request) to be printed by a line printer.  If no
       file names are mentioned, the standard input is assumed.  When a file name is designated by a minus sign (-) it	stands	for  the  standard
       input  and may be supplied on the command line in conjunction with named files.	The order in which files appear is the same order in which
       they are printed.

       This command exists for X/OPEN compatibility.

Options
       The following options to may appear in any order and may be intermixed with file names:

       -c	   Makes copies of the files to be printed immediately when is invoked.  Normally, files are not copied, but are  linked  whenever
		   possible.  If the -c option is not given, then the user should be careful not to remove any of the files before the request has
		   been printed in its entirety.  It should also be noted that without the -c option, any changes made to the  named  files  after
		   the request is made but before it is printed are reflected in the printed output.

       -d dest	   Chooses  dest  as  the  printer that is to do the printing.	If dest is a printer, then the request is printed on that specific
		   printer.  By default, dest is taken from the environment variable PRINTER if it is set.  Otherwise, a default destination,  lp,
		   is used.

       -n number   Prints number copies (default of 1) of the output.

Files
       /etc/passwd	      personal identification
       /etc/printcap	      printer capabilities data base
       /usr/lib/lpd*	      line printer daemons
       /usr/spool/*	      directories used for spooling
       /usr/spool/*/cf*       daemon control files
       /usr/spool/*/df*       data files specified in cf files
       /usr/spool/*/tf*       temporary copies of cf files

See Also
       lpq(1), lpr(1), lprm(1), pr(1), symlink(2), printcap(2), lpc(8), lpd(8)

																	     lp(1)
All times are GMT -4. The time now is 02:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy