Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help fine

Status
Not open for further replies.

sauser

Programmer
Aug 22, 2001
49
US
hey guys thanks for answering my last post you helped me a lot. Well I have one more question. I was writing a real simple script just to copy folders.

so the command i was issuing was liek that

cp -R /home/$username /home/sauser/$username


I was just doing backup of all users folders.

So anyways when i issue this command from the command prompt. It works just fine. When it comes to the perl script It gets all messed up with error messages.

The line i wrotein perl was like that
` cd -R "/home/$username" "/home/sauser/$username"`

The error it was giving was no such file or directory.
 
Help us out here... what are the errror messages?


keep the rudder amid ship and beware the odd typo
 
the error meesage was:

/home/$username no such file or directory and /home/sauser/$username no such or directory

what puzzles me is that when i run that same command from command line it works just fine!


 
sauser,

Some things to consider

Did you mean cp -R, instead of cd -R (hence you would be trying to cd to the -R directory ;-) )

Also, if $username a shell variable? If so, you will need to export this variable to the environment, and then call using the %ENV hash in perl:

Code:
ksh:       export username=$(whoami)
csh:       setenv username `whoami`
then perl: my $username = $ENV{'username'};

Also, the following code may be a bit cleaner:

Code:
my $source = qq</home/$ENV{'username'}>;
my $target = qq</home/sauser/$ENV{'username'}>;
my $cmd    = &quot;cp&quot;;
my $opts   = &quot;-R&quot;;

system $cmd, $opts, $source, $target;

I think this should work. Unfortunately not in front of my UNIX box at the moment. Happy coding, cheers NEIL :)
 
yeah i mean cp sorry

oh i frogot to tellyou soemthing all those usernames are coming from text file
I have a list of users in a text file

Alex
 
OK. So your code looks something like:

Code:
$cmd  = &quot;cp&quot;;
$opts = &quot;-R&quot;;
open USERS, $userfile or die;
while( $user = <USERS> ) {
    chomp $user;
    $src  = &quot;/home/$user&quot;;
    $dest = &quot;/home/sauser/$user&quot;;
    system $cmd, $opts, $src, $dest;
}

Note the very important chomp command. Otherwiser, the $user variable would contain an end of line character ('\n' on UNIX), so that the command seen by the system call would actually be:

Code:
cp -R /home/fred
/home/sauser/fred

For debugging purposes, you should print out what is being passed to the system command, just to make sure all is as expected, eg:

Code:
print &quot;cmd: $cmd, opts: $opts, src: $src, dest: $dest\n&quot;

Hope some of this helps s-) NEIL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top