Perl Scripting

I have create this shell script

#!/bin/sh

if [ -z $1 ]; then                                 
        echo "Usage: ./script <filename>"           
        exit 1
elif [ -f "$1" ]; then
        fname="$1"
fi

output="output.txt"                               
i=0                                                

while read a;                                                        
do                                                

  i=$((i+1))
                                                                                                    
     num=$a                                      
   
    
    echo "$a" | sed 's/$/_'$num'/' >> "$output"    

done < "$fname"

Now,I want to create a perl script
which i want to echo $a from shell script file
any idea?

I am not able to understand this. Could you please elaborate.

i want to create a perl script
iwant this perl script will displays the results of the script.sh

Why do you need a perl script for this? You could display the results from within scripts.sh. Are you looking for anything specifically?

this script is huge (with many characters) so i want the perl script read one line

I am still not able to understand. IMHO, the requirement is still vague.
Why can't you have the shell script to read that line?

i want that way
this is too general
but the problem is that i have in my shell script
something like that
#hub
ssh myserver "${SIIR}input.pl $*"
SIIR has many characters and the file input.pl read them
so i create this solution

In perl, to read lines from a file, you need to open a file handle and read line by line:

open FILEHANDLE, "< file.txt";
while (<FILEHANDLE>) {
    chomp;
    <do something with line>;
}
close FILEHANDLE;
1 Like

thank you!

---------- Post updated at 04:19 PM ---------- Previous update was at 04:33 AM ----------

i want to create a shell script which input parameters(scripts) and then save(in file) one line in time.
then i want to create perl script who can read one line in time.
i want all this in loop
any idea?

@Evelin90: You must understand that you can read files line by line using a shell script too. Why do you want to invoke perl for that? I am, to some extent, able to understand the flowchart that you've attached in your post, but what is the exact purpose of having such a workflow? Why can't you have it this way:

----------
|  cmd   |
----------
    |
    |
    V
----------
|  .sh   |----> Save file
----------
    |
    |
    V
---------------
| Read file   |
| using shell |
| script      |
---------------

---------- Post updated at 12:42 ---------- Previous update was at 09:11 ----------

This is the best I could think of from your question:

In your shell script, write a line of code that will echo all arguments into a file. Then scp that file to myserver. Hope you have enabled password-less access by sharing the public key from host-machine to myserver. I think yes, since you're already ssh-ing to myserver. Once you copy arguments.dat to myserver, open the file in perl and process the arguments as required.

Here's the shell script:

echo $* > arguments.dat
scp arguments.dat user@myserver:/path/to/
ssh myserver "/path/to/some_perl_script.pl"

Here's the perl snippet:

open ARGFILE, "< arguments.dat";
while (<ARGFILE>) {
    chomp;
    <do something with arguments>;
}
close ARGFILE;
1 Like

thank you!
I think in shell script we must check if user entering script..i will find it!