Issue regarding dos2unix perl script

Hi All,

I have pearl script which will check and convert the file:

INFO("dos2unix_cmds".$#{$dos2unix_cmds});  
  if ( $#{$dos2unix_cmds} == 0 ) {
    my $convert_cmd =  $$dos2unix_cmds[0][0];
    my $rename_cmd  =  $$dos2unix_cmds[0][1];
    --conversion going here
else 
     INFO ("No need to convert... skipping this step.")

Now, here the value of $dos2unix_cmds is

dos2unix -437 -q <some_file_nm>

However it is going to else section, and my file still has ^M char!

Please help.
Please let me know if more info required.

Thanks, Saptarshi

---------- Post updated at 11:06 AM ---------- Previous update was at 06:09 AM ----------

One more thing, what does "$#" and "$$" mean in perl?

$$ is the PID of the running program, I think. It could also be a double array reference though that seems dodgy. $# means the length of the array.

Could you post your entire script? Or for that matter, what your actual goal is? There's probably a simpler way to do it than a perl script.

I never liked dos2unix. Almost nobody has it, and when they do it's liable to screw up.

Hi Corona688,

Please find the below script:

# Convert file to Dos2UNIX
$sql  = "SELECT compare_1_command, compare_2_command ";
$sql .= "FROM vw_app_file_validation ";
$sql .= "WHERE source_file_id =  $g_source_file_id AND validation_type = 'DOS2UNIX'";
INFO ("Executing SQL =>". $sql);
my $dos2unix_cmds = app_get_data($sql);
INFO("dos2unix_cmds".$#{$dos2unix_cmds});
if ( $#{$dos2unix_cmds} == 0 ) {
   my $convert_cmd = $$dos2unix_cmds[0][0];
   my $rename_cmd  = $$dos2unix_cmds[0][1];
  #Change to tmp Directory
   INFO ("Switch to tmp Directory => $g_tmp_dir");
   chdir($g_tmp_dir);
   INFO ("Converting Dos File to UNIX => $convert_cmd");
   system($convert_cmd);
   INFO ("Renaming File => $rename_cmd");
   system($rename_cmd);
}
else {
   INFO ("No need to convert... Skipping this step");
}

Logfile shows:

validation_type = 'DOS2UNIX'] [app_validate_file.pl[139]]
[2011/06/29 07:11:41] [INFO] [EXECUTING: SELECT compare_1_command, compare_2_command FROM vw_app_file_validation WHERE source_file_id =  21 AND validation_type = 'DOS2UNIX'] [APP_ORA.pm[222]]
[2011/06/29 07:11:41] [INFO] [Execute succeeded] [APP_ORA.pm[245]]
[2011/06/29 07:11:41] [INFO] [dos2unix_cmds-1] [app_validate_file.pl[143]]
[2011/06/29 07:11:41] [INFO] [No need to convert... Skipping this step] [app_validate_file.pl[162]]

The SQL SELECT query returns:

dos2unix -437 -q <some_file_nm>

However, the script is going to 'else' section as the logfile shows.
I still have ^M char in file.

I think your $dos2unix_cmds needs to be a list, like @var, for $#var to return any sensible value. It will be 0 when var has 1 element, 1 when var has 2, etc.

If app_get_data doesn't return a list, you may need to turn it into one with

@dos2unix_cmds=split(/[ ]/, app_get_data($sql));

1 Like

can i use

if /usr/xpg4/bin/grep -qE [[:cntrl:]]

in perl?

You can, if you wrap it in a system() call. like system("grep ...");

And its return value isn't boolean. It will return zero on success and some nonzero number on failure. So check for zero.

I am just trying to learn perl script.. what i posted was already in PRD... could you please give me the area (with code) where i need to modify?

Warm Regards,
Saptarshi.

I have no idea why that script is doing certain things, and no idea how some of those local functions are supposed to be working, so I can't write it for you, best I can do is give you the pieces you need and offer advice, and improve by trial and error. To do this I need replies from you telling me if what I did worked, otherwise I'm no further along.

Have you tried anything else I've suggested yet? Did it work or not?

I'd suggest commenting out the chdir's and system's until you get it at least acknowledging that you have a file to operate on, and the right number of them at that

Sure.. let me try what you suggested and I will update you very soon.