2010-11-07 13 views
5

मेरे पास एक बैश स्क्रिप्ट है जो मूल रूप से ड्राइवर के रूप में कार्य करती है। किसी कारण से उबंटू ब्लूटूथ सीरियल पोर्ट को अपने आप पर असाइन नहीं कर सकता है। स्क्रिप्ट का फ़ंक्शन ब्लूटूथ डिवाइस को कनेक्ट करना है, फिर इसे/dev/ब्लूटूथ सीरियल में एक्सेस करने के लिए एक स्थान असाइन करें। अंत में, जब डिवाइस डिस्कनेक्ट हो जाता है, या "क्यू" दबाकर समाप्त हो जाता है, तो यह बंदरगाह को मार देता है।बाश स्क्रिप्ट समाप्ति पर कमांड कैसे निष्पादित करें?

मैं अगर वहाँ किसी तरह एक पार्टी लिपि में एक कमांड निष्पादित करने के लिए इतना है कि यह मेरी/dev फ़ोल्डर में जगह में व्यर्थ डिवाइस नहीं छोड़ता जब Ctrl-C क्रियान्वित किया जाता है जानना चाहूंगा

उत्तर

8

हाँ, आप 'जाल' कमांड का उपयोग कर सकते हैं। साधते CTRL-C एक SIGINT भेजता है, तो हम को पकड़ने के लिए है कि जाल का उपयोग कर सकते हैं:

#!/bin/bash 

trap "echo hello world" INT 

sleep 10 

आप जब इस चलाता CTRL-C मारा, तो यह आदेश (echo hello world) :-)

पर अमल करेंगे
0
$ help trap 

trap: trap [-lp] [arg signal_spec ...] 
    The command ARG is to be read and executed when the shell receives 
    signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC 
    is supplied) or `-', each specified signal is reset to its original 
    value. If ARG is the null string each SIGNAL_SPEC is ignored by the 
    shell and by the commands it invokes. If a SIGNAL_SPEC is EXIT (0) 
    the command ARG is executed on exit from the shell. If a SIGNAL_SPEC 
    is DEBUG, ARG is executed after every simple command. If the`-p' option 
    is supplied then the trap commands associated with each SIGNAL_SPEC are 
    displayed. If no arguments are supplied or if only `-p' is given, trap 
    prints the list of commands associated with each signal. Each SIGNAL_SPEC 
    is either a signal name in <signal.h> or a signal number. Signal names 
    are case insensitive and the SIG prefix is optional. `trap -l' prints 
    a list of signal names and their corresponding numbers. Note that a 
    signal can be sent to the shell with "kill -signal $$". 
0

एक जाल का प्रयोग करें।

trap "do_something" SIGINT 

जहां "do_something" एक कमांड या फ़ंक्शन नाम है।

संबंधित मुद्दे