How do I best pass arguments to a Perl one-liner?

前端 未结 7 1722
误落风尘
误落风尘 2020-12-05 05:27

I have a file, someFile, like this:

$cat someFile
hdisk1 active
hdisk2 active

I use this shell script to check:



        
相关标签:
7条回答
  • 2020-12-05 05:49

    My solution is a little different. I came to your question with a Google search the title of your question, but I'm trying to execute something different. Here it is in case it helps someone:

    FYI, I was using tcsh on Solaris.

    I had the following one-liner:

    perl -e 'use POSIX qw(strftime); print strftime("%Y-%m-%d", localtime(time()-3600*24*2));'
    

    which outputs the value:

    2013-05-06
    

    I was trying to place this into a shell script so I could create a file with a date in the filename, of X numbers of days in the past. I tried:

    set dateVariable=`perl -e 'use POSIX qw(strftime); print strftime("%Y-%m-%d", localtime(time()-3600*24*$numberOfDaysPrior));'`
    

    But this didn't work due to variable substitution. I had to mess around with the quoting, to get it to interpret it properly. I tried enclosing the whole lot in double quotes, but this made the Perl command not syntactically correct, as it messed with the double quotes around date format. I finished up with:

    set dateVariable=`perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time()-3600*24*$numberOfDaysPrior));"`
    

    Which worked great for me, without having to resort to any fancy variable exporting.

    I realise this doesn't exactly answer your specific question, but it answered the title and might help someone else!

    0 讨论(0)
提交回复
热议问题