Results 1 to 6 of 6

Thread: Running unix commands within Perl Scripts

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Running unix commands within Perl Scripts

    Hi ,

    I am using the Linux operating system and as well as using perl scripting for specific task.I am new for perl programming but much familiar with linux.I want to execute the Linux commands in in the perl script.How it would be done.i will be highly obliged to you.

    Thank you very much
    Last edited by Ash maker; 09-03-2010 at 02:48 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Running unix commands within Perl Scripts

    You can execute the Linux commands within the perl script file and can be more beneficial for you to execute the commands within the program.

    This is one of the program which is embedded with the Linux commands and will execute successful.


    Code:
    {
        my $dir = './logs';
        opendir my $dh, $dir or die "Can't opendir '$dir': $!\n";
        my @files = grep { ! -d "$dir/$_" and ! /file1/ } readdir $dh;
        print "$_\n" for @files;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    exec() and system() function within Perl Scripts

    exec() and system() function:

    There are two functions are available in Perl named exec() function and system() that are being used to execute a system shell command.

    The difference between these two functions are - system() creates a fork process and waits to see if the command succeeds or fails - returning a value.
    On other hand,exec() doesn't evaluate anything after execution , it only executes the command.

    Using backtick operator,you can capture output of the system call and it can't be used directly to get the capture output.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    exec and system function with Perl Scripts

    The system and exec function can be used during creation of script in perl.You can use these functions as below-

    A simple example can show you the functionality and implementation of the functions which would be as follows -

    Code:
    $result = system(PROGRAM);
    exec(PROGRAM);
    The system function will manipulate the variable assigned in the parameter and the result variable is defined to get the output from this function.

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    Command-Line Programs in perl

    Command-Line Programs in perl :

    You need not use another editor and all to execute the programs in perl.You can easily run the small bit Perl programs on the command line.

    You need to use some option which makes able you to execute the script.The -e option allows you to define Perl code to be executed by the compiler. For example, You can execute the "Hello William..." on the command line argument and it would print without any option.I am using it as a simple program to understanding for you.

    Code:
    $ perl -e 'print "Hello William...\n"'
    You can use another -e option to print the output of the program.

  6. #6
    Join Date
    May 2008
    Posts
    2,302

    Information about Perl components

    You can get easily and readable information about the programming components of the perl.There are some commands with appropriate options will facilitate you to access the function and documentation about the perl programming -

    To know more about the perl system function,just type the commands suggested below -

    perldoc -f system

    You can use the same method to access different components of the perl documentation.

Similar Threads

  1. Unix commands in iPhone
    By Valliy in forum Portable Devices
    Replies: 6
    Last Post: 08-03-2012, 02:51 AM
  2. Macbook Pro is freezing on ‘Running Package Scripts’
    By ChittiBabu in forum Portable Devices
    Replies: 2
    Last Post: 15-01-2012, 07:59 PM
  3. Running PHP Scripts with Cron
    By klite in forum Software Development
    Replies: 5
    Last Post: 04-11-2009, 07:26 AM
  4. An error has occured running scripts on this page
    By Swine Flu in forum Technology & Internet
    Replies: 3
    Last Post: 17-08-2009, 01:21 PM
  5. Scripts not running in crontab
    By Frasier in forum Operating Systems
    Replies: 6
    Last Post: 22-10-2008, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •