MySQL प्रक्रिया शुरू करने के लिए मेरे पास निम्न स्क्रिप्ट है:
if [ "${1:0:1}" = '-' ]; then
set -- mysqld_safe "$@"
fi
if [ "$1" = 'mysqld_safe' ]; then
DATADIR="/var/lib/mysql"
...
इस संदर्भ में 1: 0: 1 का क्या अर्थ है?
MySQL प्रक्रिया शुरू करने के लिए मेरे पास निम्न स्क्रिप्ट है:
if [ "${1:0:1}" = '-' ]; then
set -- mysqld_safe "$@"
fi
if [ "$1" = 'mysqld_safe' ]; then
DATADIR="/var/lib/mysql"
...
इस संदर्भ में 1: 0: 1 का क्या अर्थ है?
जवाबों:
यह एक -धराशायी तर्क विकल्प के लिए एक परीक्षण है, जाहिरा तौर पर। यह वास्तव में थोड़ा अजीब है। यह bashपहले और केवल पहले चरित्र को निकालने के प्रयास में एक गैर-मानक विस्तार का उपयोग करता है $1। 0सिर चरित्र सूचकांक है और 1स्ट्रिंग की लंबाई है। इस [ testतरह से कि यह भी हो सकता है:
[ " -${1#?}" = " $1" ]
testहालांकि, इसकी तुलना विशेष रूप से अनुकूल नहीं है, क्योंकि यह -धराशायी तर्कों की भी व्याख्या करता है - यही कारण है कि मैं वहां अग्रणी स्थान का उपयोग करता हूं।
इस तरह का काम करने का सबसे अच्छा तरीका है - और जिस तरह से यह आमतौर पर किया जाता है - वह है:
case $1 in -*) mysqld_safe "$@"; esac
${1:0:1}एक लंबाई है, सूचकांक नहीं।
[[: [[ $1 == -* ]]।
[[ : [[है
[[सिर्फ वाक्यविन्यास नाम है, और बृहदान्त्र सिर्फ एक विराम चिह्न है।
यह $10th से 1st कैरेक्टर का विकल्प लेने जा रहा है । तो आप पहले चरित्र और केवल स्ट्रिंग के पहले चरित्र को प्राप्त करने जा रहे हैं।
से bash3.2 आदमी पेज:
${parameter:offset} ${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter start- ing at the character specified by offset. length and offset are arithmetic expressions (see ARITHMETIC EVALUATION below). length must evaluate to a number greater than or equal to zero. If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. If parameter is @, the result is length positional parameters beginning at offset. If parameter is an array name indexed by @ or *, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the :- expan- sion. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1.
यह परीक्षण कर रहा है कि पहले तर्क $1का पहला चरित्र एक पानी का छींटा है -।
1: 0: 1 पैरामीटर विस्तार के लिए मान हैं ${parameter:offset:length}:।
इसका मत:
1, यानी:$10(0 से गिने)।संक्षेप में: पहले स्थितीय पैरामीटर का पहला चरित्र $1।
वह पैरामीटर विस्तार ksh, bash, zsh (कम से कम) में उपलब्ध है।
यदि आप परीक्षण लाइन बदलना चाहते हैं:
[ "${1:0:1}" = "-" ]
अन्य सुरक्षित बैश समाधान हो सकते हैं:
[[ $1 =~ ^- ]]
[[ $1 == -* ]]
सुरक्षित है क्योंकि इसके पास कोई समस्या नहीं है (कोई विभाजन अंदर निष्पादित नहीं है [[)
पुराने, कम सक्षम गोले के लिए, इन्हें बदला जा सकता है:
[ "$(echo $1 | cut -c 1)" = "-" ]
[ "${1%%"${1#?}"}" = "-" ]
case $1 in -*) set -- mysqld_safe "$@";; esac
केवल केस कमांड गलत उद्धरण के लिए अधिक प्रतिरोधी है।