0

I am writing a code in perl with embedded shell script in it:

#!/usr/bin/perl

use strict;
use warnings;

our sub main {

    my $n;
    my $n2=0;


    $n=chdir("/home/directory/");

    if($n){
        print "change directory successful $n \n";

        $n2 = system("cd", "section");
        system("ls");
        print "$n2 \n";
    }
    else {
        print "no success $n \n";
    }
    print "$n\n";
}

main();

But it doesn't work. When I do the ls. The ls doesn't show new files. Anyone knows another way of doing it. I know I can use chdir(), but that is not the only problem, as I have other commands which I have created, which are simply shell commands put together. So does anyone know how to exactly use cli in perl, so that my compiler will keep the shell script attached to the same process rather than making a new process for each system ... I really don't know what to do.

The edits have been used to improve the question. Please don't mind the edits if the question is clear. edits: good point made by mob that the system is a single process so it dies everytime. But, What I am trying to do is create a perl script which follows an algorithm which decides the flow of control of the shell script. So how do I make all these shell commands to follow the same process?

6
  • It's not clear to me what you are trying to do. What compiler do you want to use and what compiler does your script use if you do not change it? Commented Jul 9, 2013 at 18:02
  • You going to be more precise about what you are trying to do. Why can't you use perl's native chdir? Commented Jul 9, 2013 at 18:07
  • I edited it...Can you please read again...thanks :) Commented Jul 9, 2013 at 18:08
  • Its because I have to use many other commands and the program here is just a sample...and I don't have a C function for all of them, some are in perl, some in shell script... Commented Jul 9, 2013 at 18:09
  • Please read the edited question...it is much more clear Commented Jul 9, 2013 at 18:10

2 Answers 2

2

system spawns a new process, and any changes made to the environment of the new process are lost when the process exits. So calling system("cd foo") will change the directory to foo inside of a very short-lived process, but won't have any effect on the current process or any future subprocesses.

To do what you want to do (*), combine your commands into a single system call.

$n2 = system("cd section; ls");

You can use Perl's rich quoting features to pass longer sequences of commands, if necessary.

$n2 = system q{cd section
if ls foo ; then
  echo we found foo in section
  ./process foo
else
  echo we did not find foo\!
  ./create_foo > foo
fi};

$n2 = system << "EOSH"; cd section ./process bar > /tmp/log cd ../sekshun ./process foo >> /tmp/log if grep -q Error /tmp/log ; then echo there were errors ... fi EOSH

(*) of course there are easy ways to do this entirely in Perl, but let's assume that the OP eventually will need some function only available in an external program

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

2 Comments

Thanks, but it won't help. What I am trying to do is create a perl script which follows an algorithm which decides the flow of control of the shell script. So how do I make all these shell commands to follow the same process?
Yes but then I could write the whole script in shell, but I need the algorithm in perl and all shell commands need to follow the same process...is there no way to do it? I can read something extensive and do it, if you have a resource which describes the flow of control of system in perl
1

system("cd", "section"); attempts to execute the program cd, but there is no such program on your machine.

There is no such program because each process has its own current work directory, and one process cannot change another process's current work directory. (Programs would malfunction left and right if it was possible.)

It looks like you are attempting to have a Perl program execute a shell script. That requires recreating the shell in Perl. (More specific goals might have simpler solutions.)


What I am trying to do is create a perl script which follows an algorithm which decides the flow of control of the shell script.

Minimal change:

Create a shell script that prompts for instructions/commands. Have your Perl script launch the shell script using Expect and feed it answers/commands.

1 Comment

thats what i went for after the wait...but still thank you so much, gives me some confidence that it will work.

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.