2013-10-07 13 views
8

जब बैश/zsh निम्नलिखित करता है क्या हो रहा है:शैल निष्पादन: समय बनाम/usr/bin/समय

~ » /usr/bin/time -l sleep 1                            
    1.00 real   0.00 user   0.00 sys 
516096 maximum resident set size 
    0 average shared memory size 
    0 average unshared data size 
    0 average unshared stack size 
    145 page reclaims 
    0 page faults 
    0 swaps 
    0 block input operations 
    0 block output operations 
    0 messages sent 
    0 messages received 
    0 signals received 
    0 voluntary context switches 
    2 involuntary context switches 
------------------------------------------------------------ 
~ » time -l sleep 1                              
zsh: command not found: -l 
-l sleep 1 0.00s user 0.00s system 52% cpu 0.001 total 
------------------------------------------------------------ 
~ » /usr/bin/time foo                              
foo: No such file or directory 
     0.00 real   0.00 user   0.00 sys 
------------------------------------------------------------ 
~ » time foo                                
zsh: command not found: foo 
foo 0.00s user 0.00s system 52% cpu 0.001 total 

यह एक फर्क कैसे मैं समय का उपयोग क्यों पड़ता है, और क्यों zsh की कोशिश कर रहा है -l निष्पादित करने के लिए ??

मजे की बात है, zsh कहते

~ » which time                                
time: shell reserved word 

बैश नहीं करता है:

~ » bash                                 
bash-3.2$ which time 
/usr/bin/time 
bash-3.2$ time foo 
bash: foo: command not found 

real 0m0.006s 
user 0m0.000s 
sys 0m0.003s 
bash-3.2$ /usr/bin/time foo 
foo: No such file or directory 
     0.00 real   0.00 user   0.00 sys 
bash-3.2$ time -l sleep 1 
bash: -l: command not found 

real 0m0.001s 
user 0m0.000s 
sys 0m0.001s 
bash-3.2$ /usr/bin/time -l sleep 1 
     1.00 real   0.00 user   0.00 sys 
    516096 maximum resident set size 
     0 average shared memory size 
     0 average unshared data size 
     0 average unshared stack size 
     144 page reclaims 
     0 page faults 
     0 swaps 
     0 block input operations 
     1 block output operations 
     0 messages sent 
     0 messages received 
     0 signals received 
     2 voluntary context switches 
     2 involuntary context switches 

उत्तर

8

time दोनों zsh और bash में बनाया गया है। हालांकि, which केवल zsh के लिए अंतर्निहित है। बैश में, जब आप which का उपयोग करते हैं तो यह /usr/bin/which चलाता है जिसे शैल बिल्ट-इन्स के बारे में कोई जानकारी नहीं है।

तो बैश में, आप का उपयोग करना चाहिए:

$ type time 
time is a shell keyword 

कारण time -l ... काम नहीं करता है कि time वाक्य रचना -l ध्वज शामिल नहीं है।

दोनों मामलों में, यह कहना सही नहीं है कि time एक अंतर्निहित कार्य है। इसे "आरक्षित शब्द" या "खोल कीवर्ड" कहकर अधिक सटीक है, क्योंकि यह पूरी पाइपलाइन पर लागू होता है; इसे फ़ंक्शन या बाहरी कमांड के रूप में कार्यान्वित नहीं किया जा सकता है। उस अर्थ में, यह if और while जैसे अन्य वाक्य रचनात्मक तत्वों के समान है।

+0

'मैन टाइम'/manr/bin/time के लिए मैन पेज को क्यों चालू करता है, और अंतर्निहित समय के लिए हम मैन पेज को कैसे देखते हैं, यदि यह मौजूद है? – emish

+0

@emish: बिल्टिन के लिए कोई अलग मैन पेज नहीं हैं; वे उस शैल के हिस्से के रूप में दस्तावेज किए जाते हैं, जिसमें वे अंतर्निहित हैं। 'बाश' में, आप बिल्टिन के बारे में जानकारी प्राप्त करने के लिए 'सहायता' अंतर्निहित का उपयोग कर सकते हैं। 'Zsh' वितरण में 'मैन zshbuiltins' शामिल है। मुझे लगता है कि 'डैश' बिल्टिन के लिए एकमात्र दस्तावेज 'मैन डैश' मैनपेज में है। – rici

+0

@emish आप बिल्टिन के लिए 'सहायता समय' भी आज़मा सकते हैं। – gerrit

0

समय zsh में अंतर्निहित कार्य है। यह बाश में नहीं है। यदि आप/usr/bin/time संस्करण का उपयोग करना चाहते हैं तो आपको zsh का उपयोग करते समय पूर्ण पथ की आपूर्ति करने की आवश्यकता है।

zsh में "अक्षम -r" कमांड का उपयोग करके इस व्यवहार को अक्षम करना भी संभव है।

+2

'टाइम' बैश में भी एक अंतर्निहित है। –

+0

@WilliamPursell नहीं, यह एक खोल कीवर्ड है। –

2

यह जानने का एक और तरीका है कि कमांड एक बैश बिल्टिन कमांड है help बिल्टिन कमांड का उपयोग करना है। पक्ष प्रभाव के बारे में जानकारी प्राप्त करने के लिए कहा आदेश के बारे में

bash$ help time 
Report time consumed by pipeline's execution. 

Execute PIPELINE and print a summary of the real time, user CPU time, 
and system CPU time spent executing PIPELINE when it terminates. 

Options: 
    -p print the timing summary in the portable Posix format 

The value of the TIMEFORMAT variable is used as the output format. 

Exit Status: 
The return status is the return status of PIPELINE. 

गैर builtin आदेशों के लिए help कहना है कि वह इसके बारे में पता नहीं है (और आदेश पंक्ति तर्क का समर्थन किया।),।

bash$ help ls 
bash: help: no help topics match `ls'. Try `help help' or `man -k ls' or `info ls'. 
संबंधित मुद्दे