को देखते हुए इस सरणी:
MY_ARR[0]="./path/path2/name.exe 'word1 word2' 'name1,name2'"
MY_ARR[1]="./path/path2/name.exe 'word1 word2' 'name3,name4,name5'"
MY_ARR[2]=".name.exe 'word1 word2'"
MY_ARR[3]="name.exe"
MY_ARR[4]="./path/path2/name.exe 'word1 word2' 'name1'"
MY_ARR[5]="./path/path2/name.exe 'word1 word2' 'name.exe, name4.exe, name5.exe'"
चलें 2 नए सरणियों MY_FILES और MY_PARAMETERS
for MY_ARR_INDEX in ${!MY_ARR[*]} ; do
######
# Set the current file in new array.
MY_FILES[ ${MY_ARR_INDEX} ]=${MY_ARR[ ${MY_ARR_INDEX} ]// *}
######
# Set the current parameters in new array
MY_PARAMETERS[ ${MY_ARR_INDEX} ]=${MY_ARR[ ${MY_ARR_INDEX} ]#* }
######
# Show the user whats happening
# (from here until done is just printing info.)
printf "MY_FILES[ ${MY_ARR_INDEX} ]=\"%s\" ; MY_PARAMETERS[ ${MY_ARR_INDEX} ]=\"%s\"\n" \
\
"${MY_ARR[ ${MY_ARR_INDEX} ]// *}" "${MY_ARR[ ${MY_ARR_INDEX} ]#* }"
done
MY_FILES[ 0 ]="./path/path2/name.exe" ; MY_PARAMETERS[ 0 ]="'word1 word2' 'name1,name2'"
MY_FILES[ 1 ]="./path/path2/name.exe" ; MY_PARAMETERS[ 1 ]="'word1 word2' 'name3,name4,name5'"
MY_FILES[ 2 ]=".name.exe" ; MY_PARAMETERS[ 2 ]=" 'word1 word2'"
MY_FILES[ 3 ]="name.exe" ; MY_PARAMETERS[ 3 ]="name.exe"
MY_FILES[ 4 ]="./path/path2/name.exe" ; MY_PARAMETERS[ 4 ]="'word1 word2' 'name1'"
MY_FILES[ 5 ]="./path/path2/name.exe" ; MY_PARAMETERS[ 5 ]="'word1 word2' 'name.exe, name4.exe, name5.exe'"
प्रत्येक फ़ाइल का उपयोग कैसे करें:
for MY_ARR_INDEX in ${!MY_FILES[*]} ; do
CUR_FILE=${MY_FILES[ ${MY_ARR_INDEX} ] }
echo "# Do something with this file: ${CUR_FILE}"
done
आउटपुट:
Do something with this file: ./path/path2/name.exe
Do something with this file: ./path/path2/name.exe
Do something with this file: .name.exe
Do something with this file: name.exe
Do something with this file: ./path/path2/name.exe
Do something with this file: ./path/path2/name.exe
प्रत्येक पैरामीटर का उपयोग कैसे करें:
for MY_ARR_INDEX in ${!MY_PARAMETERS[*]} ; do
CUR_FILE=${MY_FILES[ ${MY_ARR_INDEX} ]}
echo "# Do something with this parameter: ${CUR_FILE}"
done
आउटपुट: {! MY_FILES [[*]}
Do something with this parameter: ./path/path2/name.exe
Do something with this parameter: ./path/path2/name.exe
Do something with this parameter: .name.exe
Do something with this parameter: name.exe
Do something with this parameter: ./path/path2/name.exe
Do something with this parameter: ./path/path2/name.exe
के बाद से $ में परिणाम सरणी MY_FILES के अनुक्रमणिका NUMBERS आप अन्य सरणी तक पहुंचने के लिए एक ही अनुक्रमणिका संख्या का उपयोग भी कर सकते हैं। टी में उसका तरीका, आप उसी लूप में डेटा के कई कॉलम तक पहुंच सकते हैं। इसलिए जैसा:
################
#
# Print each file and matching parameter(s)
#
################
# Set a printf format string so we can print all things nicely.
MY_PRINTF_FORMAT="# %25s %s\n"
################
#
# Print the column headings and use index numbers
#
# to print adjacent array elements.
#
################
(
printf "${MY_PRINTF_FORMAT}" "FILE" "PARAMETERS" "----" "----------"
for MY_ARR_INDEX in ${!MY_FILES[*]} ; do
printf "${MY_PRINTF_FORMAT}" "${MY_FILES[ ${MY_ARR_INDEX} ]}" "${MY_PARAMETERS[ ${MY_ARR_INDEX} ]}"
done
)
आउटपुट:
FILE PARAMETERS
---- ----------
./path/path2/name.exe 'word1 word2' 'name1,name2'
./path/path2/name.exe 'word1 word2' 'name3,name4,name5'
.name.exe 'word1 word2'
name.exe name.exe
./path/path2/name.exe 'word1 word2' 'name1'
./path/path2/name.exe 'word1 word2' 'name.exe, name4.exe, name5.exe'
हाँ, मुझे कटौती के बारे में पता हैअनुभाग। मैंने इसे अपनी पहली कोशिश के साथ इस्तेमाल किया। मैंने इसका इस्तेमाल करने से इंकार कर दिया क्योंकि मुझे रिक्त स्थान होने पर तीसरे और मुंह से चूकने से डर था ... जांचने की ज़रूरत है! – idobr