0
怎麼辦我寫在bash一個循環,這並不像下面的東西:
for (i=0; i< len("[email protected]"); i+=2) {
print a[i], a[i+1]
// do stuff using the pair elements.
}
怎麼辦我寫在bash一個循環,這並不像下面的東西:
for (i=0; i< len("[email protected]"); i+=2) {
print a[i], a[i+1]
// do stuff using the pair elements.
}
而非索引到[email protected]
,簡單(與POSIX兼容)的方法是簡單地關閉開始轉向東西你去:
while [ "$#" -gt 0 ]; do
echo "$1, $2"
shift; shift
done
如果你確實想指數代替,並且不介意被專用於由KSH率先殼(如bash)的採用擴展:
# start at 1 to avoid $0
# quotes in immediately below line because SO syntax highlighter doesn't know bash
for ((i=1; i<("$#" + 1); i+=2)); do
echo "${@:i:1}, ${@:i+1:1}"
done
是的,bash是偉大的,我使用它,但我也不會在腳本中使用bashisms。 – DigitalRoss
@DigitalRoss,我顯然更願意將腳本語言寫入腳本(它們中的一些可以讓你做強壯的事情,但僅僅依賴於POSIX授權的功能時不可靠),但我至少嘗試在他們增加一些價值的地方這樣做。 :) –
當然,你可能有一個問題沒有被核心shell解決得很好,這是宏處理器和編程語言之間的妥協。那是當我切換到Ruby時,哈哈。 – DigitalRoss