Reputation: 2187
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.
Upvotes: 1
Views: 1710
Reputation: 13344
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.
Upvotes: 1