ali_kerem has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I try to execute a command on Windows by sending that command from Linux. I did it in bash code as test but when I tried it in perl, I could not achieve what I wanted. What can I not see in these codes, can you help me ?

(Update#1 win2 of bash code)

(Update#2 with newline (\n), I solved the mistery, )

bash code;

#!/bin/bash # to test it continuosly I keep 1copy.txt untouched # there are 4 lines in it, the first line is # ac wavSound\test.wav adpSound\test01.adp -norm cat 1copy.txt > 1.txt i=`wc -l 1.txt | awk '{print $1}'` # get line count while [[ $i -gt 0 ]] do (( i-- )) head -n1 1.txt > bat.bat # get first line into bat file sed 1d 1.txt > 2.txt # remove first line and save it new file mv 2.txt 1.txt # move it back to original file #put bat file onto Windows machine sftp adp@10.10.110.222 <<EOF put bat.bat EOF #execute bat file in cmd ( win2 has one line; start "" "bat.bat" )#Upd +ate #1 ssh adp@10.10.110.222 cmd.exe < win2 sleep 12 #without this sleep,it ends so fast, it can only #produce one adp file randomly, sometimes its test01.adp #sometimes its test04.adp done

perl code;

sub winUse { my $wavF =shift; my $adpC =shift; # Channel info my $adpT =shift; # Title info my $adpF = "channel$adpC-title$adpT.adp"; my $batF = "adp2.bat"; #bat file my $ordF = "cmdWin"; #windows command to exec my $wavD = "wavSound"; #wav dir my $adpD = "adpSound"; #adp dir my $winS = "10.10.110.222"; #win server my $winU = "adp"; #win user my $fh; # FileHandle my @cnvrtArray = ( "ac"," ", #adpcm_converter.exe $wavD,"\\", #wav directory on windows $wavF," ", #wav source file $adpD,"\\", #adp directory on windows $adpF," ", #adp goal file "-norm" #parameters ); open($fh,'>',$batF) or die "Can't write file '$batF' [$!]\n"; print $fh @cnvrtArray; close($fh); open($fh,'>',$ordF) or die "Can't write file '$ordF' [$!]\n"; #print $fh $batF; # First post print $fh 'start "" "'.$batF.'"'."\n"; # Update #2 close($fh); my $sftpString1 = "sftp $winU\@$winS<<EOF\nput $batF\ncd $wavD +\nput $wavF\nEOF"; my $sshString = "ssh $winU\@$winS cmd.exe < $ordF\n"; my $sftpString2 = "sftp $winU\@$winS<<EOF\ncd $wavD\nrm $wavF\ +ncd ..\/$adpD\nget $adpF\nEOF"; system($sftpString1); sleep 5; system($sshString); sleep 10; system($sftpString2); sleep 5; } winUse("test.wav",52,24); winUse("test.wav",52,25); winUse("test.wav",52,26);

Replies are listed 'Best First'.
Re: Bash vs Perl. Why doesn't work...?
by vinoth.ree (Monsignor) on Jan 24, 2013 at 10:36 UTC

    Are you getting any error message ? check the exist status of the system command

    If you'd like to manually inspect system's failure, you can check all possible failure modes by inspecting $? like this:

    if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; }

      I dont get any error message normally but after your code, I got this output

      Connecting to 10.10.110.222... sftp> put adp2.bat Uploading adp2.bat to /adp2.bat adp2.bat + 100% + 57 0.1KB/s 00:00 sftp> put wavSound/test.wav wavSound/test.wav Uploading wavSound/test.wav to /wavSound/test.wav wavSound/test.wav + 100% + 771KB 771.1KB/s 00:00 child exited with value 0 child exited with value 255 Connecting to 10.10.110.222... sftp> cd wavSound sftp> rm test.wav Removing /wavSound/test.wav sftp> cd ../adpSound sftp> get channel52-title24.adp Couldn't stat remote file: No such file or directory File "/adpSound/channel52-title24.adp" not found. child exited with value 0
Re: Bash vs Perl. Why doesn't work...?
by ali_kerem (Acolyte) on Jan 24, 2013 at 11:18 UTC

    Thank you ree for your help. I found my answer in newline :)

    I updated the code, you can check Update #2 for the updated part. After that line, perl code works too. Thanks again.