ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;
my $file;
foreach $file(@files) {
print "$file";
print "\n";
chomp $file;
sleep 7;
use File::Copy;
move("$file", "/home/casper1/tmp") or die "Move failed";
}
I use this to move file from one directory to a tmp directory. I works for me because it prints out the files before it moves them. If I do not want to move that particular file, I hit control c to stop script. I want to have the ability to skip over certain files in the array(have them remain in the designated folder) have been trying to add some if/else
statements asking for input verification - and then a shift(@files) command to skip over some files, but (see below) - but what the program does is wait for the first "y" then it moves all the files over. I want something that will ask me each time. I have been working on this two days it is time to get some advice.
#!/usr/bin/perl -w
use strict;
my @files = `ls -1`;
my $files;
print "Do you want to move file $files[0]";
my $answer = <>;
if ($answer eq "y") {
foreach $file(@files) {
print "$file";
print "\n";
chomp $file;
sleep 3;
use File::Copy;
move("$file", "/home/casper1/test") or die "Move failed";
}
elsif ($answer eq "n") {
$file = shift(@files);
}
else {
$file = shift(@files);
print " the file has been skipped";
}
}
Last edited by casperdaghost; 06-01-2010 at 03:02 PM.
Perl is a wonderful tool, but for something like this with user I/O on every file it is less than optimal. You are not taking advantage of perl's internal functions, globing, or other features of power to process more efficiently than a shell, so why not use a shell?
Almost any shell that includes 'read' and 'if' can prompt and gulp user response and conditionally call 'mv' to perform as well with greater simplicity.
UNtested and just off the top of my tired old head:
#!/bin/bash
HOME=/home/casper1
TARG=${HOME}/test
for file in `ls` ; do
echo -n "Shall I move ${file}? "
read foo junk
case $foo in
Y|y)
if mv $file $TARG
then
echo " File $foo moved to $TARG "
else
echo " File $foo move FAILED! "
fi
;;
*)
echo " Skipping $foo "
;;
esac
done
In order to one day "take advantage of Perl's internal functions, globing, or other features of power" I am going to have to learn the language first- which means using it, even for the mundane. Thanks for the input though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.