split function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nivet
    New Member
    • Nov 2008
    • 8

    split function

    Hi,

    I am new to perl programming.

    I have a requirement to validate the input received from the user with the file and send it to the printer.

    For Eg:
    script -o option -p printername -f filename

    script - is the name of my script
    -o option- is the option that has to be validated with another file
    -p printername - is the name of my printer
    -f filename - is the filename which has to be printed.

    how to use this split function in this case. I want to take" -o option" and compare it with the file.
    I already have a print queue ready. what is teh command to send the filename to the printer.

    Please help me.

    Thanks.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    To process command line options, you can make use of GetOpt::Std module that comes with default Perl installation.

    Comment

    • nivet
      New Member
      • Nov 2008
      • 8

      #3
      i had used the below command to check whether the file exists or not.
      eventhough the file exists in proper path, it is going to else part.

      or if the path is wrong, it is not dying....
      Please help...
      Code:
      #!/usr/bin/perl
      
      use strict;
      
      my $file = <STDIN>;
      print ( $file );
      $ENV{'PPD'} = $file;
      
      # file
      
      open (FILE, $file) || die "cannot open";
      
      if (-e $file) {
      print "exists \n";
      }
      else
      {
      print ( $file );
      print "not \n";
      }
      close(FILE);
      Last edited by numberwhun; Nov 11 '08, 08:08 PM. Reason: Please use code tags!

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by nivet
        i had used the below command to check whether the file exists or not.
        eventhough the file exists in proper path, it is going to else part.

        or if the path is wrong, it is not dying....
        Please help...
        Code:
        #!/usr/bin/perl
        
        use strict;
        
        my $file = <STDIN>;
        print ( $file );
        $ENV{'PPD'} = $file;
        
        # file
        
        open (FILE, $file) || die "cannot open";
        
        if (-e $file) {
        print "exists \n";
        }
        else
        {
        print ( $file );
        print "not \n";
        }
        close(FILE);
        Do remember to chop the newline while taking input from command line/terminal. That will solve the issue.
        Code:
        chomp(my $file = <STDIN>);

        Comment

        • nivet
          New Member
          • Nov 2008
          • 8

          #5
          Hi,

          Thanks for the reply..

          i have got the option from the user Eg: -o xxx=yyy

          @options=@ARGV[0..$#ARGV];

          I want to ignore -o and take only xxx in one variable and yyy in another variable..

          Please help me with the commands..
          Thank You.

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            If you are calling the script like,
            perl script.pl -o xxx=yyy

            use:
            Code:
            use strict;
            my $option=$ARGV[1];     # get second argument
            my ($first, $second) = split /=/,$option;   # split across '='
            print "$first\n$second\n";

            However, if you are handling multiple options, it is better to use Getopt::Std as I said before.

            - Nithin

            Comment

            • nivet
              New Member
              • Nov 2008
              • 8

              #7
              Hi Nithin,

              Thank you for the reply.

              this is what i exactly wanted.

              perl script.pl -o xxx=yyy -o aaa=bbb -o asd=asd ........

              i want to execute my script in this way...

              so using getopt, returns only the last value.
              and also i want to ignore -o and use only xxx=yyy, aaa=bbb, asd=asd values.
              xxx,aaa,asd separately in a array and yyy,bbb,asd separately in a array......

              please help me if you have any idea on this.

              Thanks in advance...

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                Originally posted by nivet
                Hi Nithin,

                Thank you for the reply.

                this is what i exactly wanted.

                perl script.pl -o xxx=yyy -o aaa=bbb -o asd=asd ........

                i want to execute my script in this way...

                so using getopt, returns only the last value.
                and also i want to ignore -o and use only xxx=yyy, aaa=bbb, asd=asd values.
                xxx,aaa,asd separately in a array and yyy,bbb,asd separately in a array......

                please help me if you have any idea on this.

                Thanks in advance...
                Try:
                Code:
                use strict;
                
                my @options=grep(!/-o/,@ARGV); # get all args other than -o
                my (@first,@second);
                foreach(@options) {
                my ($first, $second) = split /=/;
                push @first,$first;
                push @second,$second;
                }

                Comment

                • nivet
                  New Member
                  • Nov 2008
                  • 8

                  #9
                  Thanks for this valuable information.
                  If there is any rating for the replies I would like to give you 10.
                  Thanks a lot.

                  Comment

                  • nivet
                    New Member
                    • Nov 2008
                    • 8

                    #10
                    I am executing the below script in the terminal.

                    /usr/lib/filter/rip "<some options>" /anyfilename > /outputfile

                    I am executing a script named rip with some options and giving the input file and redirecting it to a file.

                    I want to use perl script to execute this command.

                    I used system command as below.

                    system("/usr/lib/filter/rip "<some options>" /anyfilename > /outputfile");

                    It is not working. How to execute.
                    Please help.

                    Comment

                    • nivet
                      New Member
                      • Nov 2008
                      • 8

                      #11
                      I have executed the below command and it is partially working.

                      @pr = ("/usr/lib/rip","option", "/test",">","/del");
                      print ( "@pr" );
                      system("@pr");

                      Bash#:/usr/lib/rip option /test > /del

                      This <option> has to be displayed in double quotes ("option").
                      But this double quotes are taken as space.

                      The command I want to execute is as below.

                      Bash#:/usr/lib/rip "option" /test > /del

                      Please help.

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        Originally posted by nivet
                        I have executed the below command and it is partially working.

                        @pr = ("/usr/lib/rip","option", "/test",">","/del");
                        print ( "@pr" );
                        system("@pr");

                        Bash#:/usr/lib/rip option /test > /del

                        This <option> has to be displayed in double quotes ("option").
                        But this double quotes are taken as space.

                        The command I want to execute is as below.

                        Bash#:/usr/lib/rip "option" /test > /del

                        Please help.
                        The double quotes around options should be escaped. For ex., consider the command you need is
                        Code:
                        #path/rip "Hello man" input.txt  > output.txt
                        Then, the system command should be as below:
                        Code:
                        system("path/rip \"Hello man\" input.txt > output.txt");

                        Comment

                        • nivet
                          New Member
                          • Nov 2008
                          • 8

                          #13
                          Thanks for the reply.

                          I want to store the output file. If i store it in an $var or @array, the file is not saved properly.

                          my script:
                          my $file = "/tmp/input";
                          my @pr = "/tmp/file.log";

                          @command = ("/usr/lib/rip \"options\" $file > @pr");
                          system ( "@command" );

                          file.log shows some file size. When i try to open it, it is not responding.
                          please let me know how to solve this issue.
                          This issue is not present wen a txt file is given as input. other than text file all other files are giving problem.

                          Thanks,
                          Last edited by nivet; Nov 19 '08, 11:41 AM. Reason: forgot to include a command

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            Code:
                            my $file = '/tmp/input';
                            my $out = '/tmp/file.log';
                            
                            my $command = ("/usr/lib/rip \"options\" $file > $out");
                            system ($command);

                            Comment

                            Working...