Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

How to use shell variables in perl command call in a bash shell script?

How to use shell variables in perl command call in a bash shell script?

I have a perl command in my shell script to evaluate date -1.

How can i use $myDate in perl command call?

This is the section in my script:

myDate='10/10/2012'

Dt=$(perl -e 'use POSIX;print strftime '%m/%d/%y', localtime time-86400;")

I want use $myDate in place of %m/%d/%y.

Any help will be appreciated.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Variables from the shell are available in Perl's %ENV hash. With bash (and some other shells) you need to take the extra step of "exporting" your shell variable so it is visible to subprocesses.

mydate=10/10/2012
export mydate
perl -e 'print "my date is $ENV{mydate}
"'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...