0

I need to run sequential shell commands using perl, but I need the shell environment to keep its variables.

for example:

$result = `cd /`;
$result = `touch test.txt`;

In this example, I need test.txt to be created on /.

Also, I don't want to run them in one line code like $result=touch /test.txt; I need seperate calls to shell while environment variables remain the same.

3
  • Would you expect running bash -c 'cd /' to do anything useful? Commented Apr 3, 2013 at 14:30
  • Yes, I would. It's just an example to describe what I need. This example may not look to be useful, but in the case that I need, it's extremely useful. Commented Apr 3, 2013 at 18:33
  • That was a rhetorical question. bash -c 'cd /' does not do anything useful. It creates a sub-shell that sets it's working directory, that then returns an errorlevel of 0. When it's done you have the same working directory that you started out with. The only thing it can do, is find out if the directory exists. Commented Apr 3, 2013 at 18:46

1 Answer 1

3
chdir '/';
$result = `touch test.txt`;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I need 2 separate shell calls using the same function. "chdir" is not a shell call and is not as same as backticks.
you could create a shell script file (example.sh) with the commands needed, and call this script from Perl
Yes, but then, I can't get separate results from each command. I may need to manipulate the results of each command and decide what to do next.
In that case, use the perl equivalents and you have the whole control

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.