2

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.

1
  • 3
    Why are you putting perl in a .sh file? seems a very roundabout way to do it, why not just use a .pl file? Commented Apr 9, 2010 at 16:38

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

$_="$str\n"

Comments

0
print OUT $_ . "\n";

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.