[英]explain the stages of this script
誰能解釋這個shell腳本?
#!/bin/bash read a STR=" $a " ARR=($STR) LEN=${#ARR[*]} LAST=$(($LEN-1)) ARR2=() I=$LAST while [[ $I -ge 0 ]]; do ARR2[ ${#ARR2[*]} ]=${ARR[$I]} I=$(($I-1)) done echo ${ARR2[*]}
謝謝。
評論到exaplain每行。
#!/bin/bash
read a # Reads from stdin
STR=" $a " # concat the input with 1 space after and three before
ARR=($STR) # convert to array?
LEN=${#ARR[*]} # get the length of the array
LAST=$(($LEN-1)) # get the index of the last element of the array
ARR2=() # new array
I=$LAST # set var to make the first, the last
while [[ $I -ge 0 ]]; do # reverse iteration
ARR2[ ${#ARR2[*]} ]=${ARR[$I]} #add the array item into new array
I=$(($I-1)) # decrement variable
done
echo ${ARR2[*]} # echo the new array (reverse of the first)
格式確實弄亂了您的帖子。 這是重新格式化的腳本:
#!/bin/bash
read a
STR=" $a "
ARR=($STR)
LEN=${#ARR[*]}
LAST=$(($LEN-1))
ARR2=()
I=$LAST
while [[ $I -ge 0 ]];
do ARR2[ ${#ARR2[*]} ]=${ARR[$I]}
I=$(($I-1))
done
echo ${ARR2[*]}
轉過一個單詞列表會做什么。
$ echo "a b c d e f" | ./foo.sh
f e d c b a
$ echo "The quick brown fox jumps over the lazy dog" | ./foo.sh
dog lazy the over jumps fox brown quick The
為了描述它是如何工作的,首先找到將字符串轉換為數組,然后計算出數組中的項數,然后通過遞減I
迭代遍歷該數組,然后回顯出結果數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.