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

Dear Monks,

I am writting a perl script in which i create a bash file containing some commands based on some rules. At the end of my perl script i close this bash file and chmod it and run it.
The problem is that i am not able to run this bash file. If i create another perl script just to run this bash file, it runs fine. But i am not able to run it from the perl script in which i created this bash file. Any ideas???

Regards,
Gaurav

"Wisdom begins in wonder" - Socrates, philosopher

Replies are listed 'Best First'.
Re: perl - bash script problem
by tachyon-II (Chaplain) on Nov 29, 2007 at 06:06 UTC
    Any ideas???

    Sure, check your error messages. Post sample code. Are you running your bash file with system, exec or backtics?

    close FILE; chmod(0755,$file) or die "Can't chmod $file $!\n"; $ret = `$file 2>&1`; print $ret;
Re: perl - bash script problem
by chromatic (Archbishop) on Nov 29, 2007 at 05:50 UTC

    We'd have to see the code you're using to try to run the bash file to give you good advice.

      Here is what i am doing:
      my $diffFile = "$reportsPath/DiffReport.bash"; open (DIFFBASH, ">$diffFile"); print DIFFBASH "#!/usr/local/bin/bash\n#\n"; print DIFFBASH "cd $wrPath\n"; print DIFFBASH "cvs diff >> $reportsPath/diffReport.txt"; close (DIFFBASH); chmod 0775, $diffFile; # Run the bash file system "./$diffFile";

      Thanks, Gaurav
      "Wisdom begins in wonder" - Socrates, philosopher

        You are not checking for any errors. If you had checked for errors, you would not need to ask this question, perl would have already given you the answer. The problem will almost certainly be the ./ before $diffFile and the error will be "file does not exist" as you write the file to the path $diffFile not ./$diffFile.

        $! Check your return values regularly and avoid frustration $!

        Now that you have it working, I wonder why you bother writing a bash script file to be executed in a separate system call, rather than just doing the "cvs" command itself via a perl system call. Seems like your script could just be:
        chdir $wrPath; my $failed = system( "cvs diff >> $reportsPath/diffReport.txt" ); if ( $failed ) { warn "cvs diff returned non-zero exit status $failed\n"; }
        If the rest of the script does stuff that depends crucially on what your current working directory is (before and/or after running "cvs diff"), you just need to add:
        use Cwd; my $origpath = getcwd; # before doing 'chdir $wrPath' ... chdir $origpath; # after doing 'system( "cvs diff ..." )'