tags:

views:

78

answers:

3

I have a requirement described in the following.

This just a sample script:

$ cat test.sh

#!/bin/bash

perl -e '
open(IN,"addrss");
open(out,">>addrss");
@newval;

while (<IN>)
{
    @col_val=split(/:/);
    if ($.==1)
    {
        for($i=0;$i<=$#col_val;$i++)
        {
           print("Enter value for $col_val[$i] : ");
           chop($newval[$i]=<STDIN>);
        }
        $str=join(":");
        $_="$str"
        print OUT;
    }
    else
    {
        exit 0;
    }
}
close(IN);
close(OUT);

Running this scipt:

    $ ./test.sh
Enter value for NAME : abc
Enter value for ADDRESS : asff35
Enter value for STATE : XYZ
Enter value for CITY : EIDHFF
Enter value for CONTACT
 : 234656758
$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
abc:asff35:XYZ:EIDHFF:234656758

When I ran it the second time:

$ cat addrss
NAME:ADDRESS:STATE:CITY:CONTACT
abc:asff35:XYZ:EIDHFF:234656758ioret:56fgdh:ghdgh:afdfg:987643221  ## it is appended in the same line...

I want it to be added to the next line.

NOTE: I want to do this by explitly using the filehandles in Perl and not with redirection operators in shell.

A: 

I think if you add a newline to the end of the string you print, this will work:

$_="$str\n"
WhirlWind
A: 
print OUT $_ . "\n";

or

instead of print, use printf and add a line feed to the front or back.

MJB
+1  A: 

I think you just need to add a newline to your string, before you print it out:

$_="$str\n";

Also, is there a reason why your script executes as bash, and you do a "perl -e" within the script? Why not just make the script file execute as perl?

Andy White