2016-03-23 79 views
0

我想在一個unix腳本里面運行perl腳本。但是在執行它時,我得到一個錯誤。它沒有timegm()perl函數正常工作。這是代碼和錯誤消息。perl timegm()在unix shell腳本里面的錯誤

#!/ bin中/ KSH

echo "I M IN KSH" 
    i=10 
    echo "value of i in ksh is $i" 
    perl << _MYCODE_ 

    print "I M IN PERL\n"; 
    use Time::Local; 

    my \$i = $i;  
    print "Value of i in perl $i\n";  
    $time_epoo = timegm(13,06,22,29,2,2016-1900);  

    print "$time_epoo" 

    _MYCODE_ 

    echo "BACK IN KSH" 

------------------------OUTPUT WITH ERROR-------------- 
I M IN KSH 
value of i in ksh is 10 
syntax error at - line 7, near "=" 
Execution of - aborted due to compilation errors. 
BACK IN KSH 
------------------------------------------------------- 
+1

我真的不知道ksh的插值規則處理的perl裏面的價值,但我猜在第一$「我的\ $ I = $ i」被轉義,以防止從KSH插值它。如果是這種情況,爲什麼「$ time_epo = ...」中的$也不會逃脫?我認爲mkHun的答案將消除可變插值中的任何可能的歧義並解決您的問題。 –

回答

2

使用-e標誌Perl來執行。然後通過使用參數方法將shell變量傳遞給perl腳本。然後使用$ARGV[0]

echo "I M IN KSH" 
i=10 
echo "value of i in ksh is $i" 
perl -e ' 

print "I M IN PERL\n"; 
use Time::Local; 

my $i = $ARGV[0]; 
print "Value of i in perl $i\n"; 
$time_epoo = timegm(13,06,22,29,2,2016-1900); 

print "$time_epoo" 

' $i 

echo "BACK IN KSH"