1

In linux, to concatenate all files under a folder, you can do file=FOLDER/*; cat $file > ONEFILE, I also want to use this in my perl script so I coded like system("file=$folder/*"); system("cat \$file > $out");

But it won't work when I run the perl program, the $out was assigned a file name as my $out = "outfile";. The outfile always keeps at 0 bit. What's wrong here.

1
  • I think I already know the problem. Commented Jun 26, 2012 at 17:21

2 Answers 2

1

The first line sets the $file environment variable in a new shell process:

system "file=$folder/*";

The second line starts a new shell process with a new environment:

system "cat \$file > $out";

Since it's a new process, with a new environment, your previous $file variable is no longer set, so you are really running the following shell command:

cat  > $out

Do this instead:

system "cat '$folder/'* > '$out';

Note - I also added quotes, which will help if your paths may contain spaces. However, it's still not safe against all forms of input, so don't pass any user input to that command without validating it first.

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

Comments

0

What's about exec in Perl?

perl -e 'exec "cat *.txt"'

Comments

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.