Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Embed Unix command inside unix command in perl

by aibharath (Initiate)
on May 20, 2010 at 14:52 UTC ( #840953=perlquestion: print w/replies, xml ) Need Help??

aibharath has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I need a small help. It would be great if you can provide an answer.

I need to embed one unix system command inside another system command in a single line. I cant use "system" command, but i can only use the charaters (`) and i cant use variables for assigning. Here is how i want :

`echo /tmp/filename.date +%Y%m%d`

I want to print the file name which has today's date suffixed to it. I'm not able to expand the inside data command

Hi again, Just found the answer after posting this thread.  `echo /tmp/filename.\`date +%Y%m%d\`` Please ignore this thread.

Replies are listed 'Best First'.
Re: Embed Unix command inside unix command in perl
by Corion (Pope) on May 20, 2010 at 14:58 UTC

    See POSIX::strftime. Also, I'm not sure why you want to use backticks when you can do the whole thing in Perl. But then again, it's not even clear to me what you want to do exactly.

Re: Embed Unix command inside unix command in perl
by cdarke (Prior) on May 20, 2010 at 16:51 UTC
    In Korn shell and Bash using back-ticks is considered deprecated, partly because nesting them makes things difficult to read. You might be better off using:
    `echo /tmp/filename.\$(date +%Y%m%d)`
    Although spawning two child process and using another language (the shell) to do a bit of string handling with a date is rather over the top. Wouldn't it be better done in Perl?

      Although spawning two child process and using another language (the shell) to do a bit of string handling with a date is rather over the top. Wouldn't it be better done in Perl?

      I understood the OP's code to be a simple example which shows the requirement, not as a complete specification of his end product. If the shell command was something distinctly non-trivial, or perhaps not the same command on each run of the Perl script, it may be much harder to implement the complete program purely in Perl.

      Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://840953]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-07-04 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?