अधिक मजबूत समाधान के लिए मैं निम्नलिखित जैसे कुछ का उपयोग करता हूं। इस तरह से स्क्रिप्ट से बाहर निकलने के बाद अस्थायी डायर को हमेशा के लिए हटा दिया जाएगा।
क्लीनअप फ़ंक्शन को निष्पादित किया जाता है EXIT
सिग्नल है। यह गारंटी देता है कि क्लीनअप फ़ंक्शन हमेशा कहा जाता है, भले ही स्क्रिप्ट कहीं गर्भपात करता हो।
#!/bin/bash
# the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# the temp directory used, within $DIR
# omit the -p parameter to create a temporal directory in the default location
WORK_DIR=`mktemp -d -p "$DIR"`
# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
echo "Could not create temp dir"
exit 1
fi
# deletes the temp directory
function cleanup {
rm -rf "$WORK_DIR"
echo "Deleted temp working directory $WORK_DIR"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
# implementation of script starts here
...
बैश स्क्रिप्ट की निर्देशिका यहाँ से ।
बैश जाल ।
mktemp -d -t <prefix>