shell程式設計的for和while迴圈
阿新 • • 發佈:2018-12-08
1. for迴圈
語法1:for NAME [in WORDS ... ] ; do COMMANDS; done
#!/bin/bash
if [[ $# -lt 1 ]] ; then echo no param!!!; exit; fi for a in "[email protected]" ; do echo hello$a > ${a} ; done
如果引數的個數小於1,直接退出。否則把hello+引數名依次重定向到引數名作為檔名的檔案。
[email protected]:命令列所有的引數
語法2:for (( exp1; exp2; exp3 )) do COMMANDS; done
for (( a=1 ; a < 11 ; a = a + 1 )) ; do echo $a ; done
迴圈列印1到10
2while迴圈
語法1:while COMMANDS; do COMMANDS; done
語法2: (( exp1))
while (( exp2 )) ; do
COMMANDS
(( exp3 ))
done
#!/bin/bash (( count = $# )) (( a = 1 )) while (( a <= count )) ; do echo$1; shift; (( a = a+1 )) done
迴圈列印命令的引數
$#:命令的引數個數;
shift:引數左移一位