bash编程之while和until循环、变量替换
2014-01-15 20:46
597 查看
一、循环体
while 测试条件; do
语句1
语句2
...
done
while [ $Count -le 5 ]; do
let Count++
done
条件满足时就循环,直到条件不再满足,则退出循环;
for如何退出循环?遍历列表中的元素完成;
while循环呢?在循环体改变测试条件相应变量等的值。
补充:算术运算符
Sum=$[$Sum+$I]可简写为以下方式
Sum+=$I
+=
-=
*=
/=
%=取余等
Sum=$[$Sum+1]
Sum+=1
let Sum++
let Count--
and --> AND
exit --> EXIT
quit
计算100以内所有正整数的和;
for循环
Sum=0
for I in {1..100}; do
Sum+=$I
done
echo $Sum
while循环
#!/bin/bash
Sum=0
Count=1
while [ $Count -le 100 ]; do
let Sum+=$Count
let Count++
done
echo $Sum
二、while循环遍历文件的每一行:
while read LINE; do
statement1
statement2
...
done < /path/to/somefile
例,如果用户的ID号为偶数,则显示其名称和shell;对所有用户执行此操作;
while read LINE; do
Uid=`echo $LINE | cut -d: -f3`
if [ $[$Uid%2] -eq 0 ]; then
echo $LINE | cut -d: -f1,7
fi
done < /etc/passwd
三、until循环体
until 测试条件; do
语句1
语句2
...
done
条件不满足就循环,直到满足时,退出循环;
例,转换用户输入的字符为大写,除了quit(遇见quit退出);
read -p "A string: " String
until [ "$String" == 'quit' ]; do
echo $String | tr 'a-z' 'A-Z'
read -p "Next [quit for quiting]: " String
done
例,每隔5秒查看hadoop用户是否登录,如果登录,显示其登录并退出;否则,显示当前时间,并说明hadoop尚未登录:
who | grep "^hadoop" &> /dev/null
RetVal=$?
until [ $RetVal -eq 0 ]; do
date
sleep 5
who | grep "^hadoop" &> /dev/null
RetVal=$?
done
echo "hadoop is here."
until who | grep "^hadoop" &> /dev/null; do 命令可直接作为判断条件 无需两端中括号
date
sleep 5
done
echo "hadoop is here."
who | grep "^hadoop" &> /dev/null
RetVal=$?
while [ $RetVal -ne 0 ]; do
date
sleep 5
who | grep "^hadoop" &> /dev/null
RetVal=$?
done
echo "hadoop is here."
四、bash编程之变量替换进阶:
1.read -p "A string" -t 5 String -t5秒超时后使用默认值但变量本身为空。
${parameter:-word} 使用默认值但变量为空
2.${parameter:=word} 使用word并赋予变量此值 ,即赋予默认值。
3.${parameter:?word}不赋予变量任何值 显示word错误信息 输出到标准错误输出
4.${parameter:+word} 替换值 如果没有赋值无响应,如果赋值则显示word值而非变量值但未改变变量值
以上四种均在变量被引用时被触发,一般来说需要被引用前即赋予默认值,则如下,
String=${String:-word} 用户未输入时立即使用默认值
while 测试条件; do
语句1
语句2
...
done
while [ $Count -le 5 ]; do
let Count++
done
条件满足时就循环,直到条件不再满足,则退出循环;
for如何退出循环?遍历列表中的元素完成;
while循环呢?在循环体改变测试条件相应变量等的值。
补充:算术运算符
Sum=$[$Sum+$I]可简写为以下方式
Sum+=$I
+=
-=
*=
/=
%=取余等
Sum=$[$Sum+1]
Sum+=1
let Sum++
let Count--
and --> AND
exit --> EXIT
quit
计算100以内所有正整数的和;
for循环
Sum=0
for I in {1..100}; do
Sum+=$I
done
echo $Sum
while循环
#!/bin/bash
Sum=0
Count=1
while [ $Count -le 100 ]; do
let Sum+=$Count
let Count++
done
echo $Sum
二、while循环遍历文件的每一行:
while read LINE; do
statement1
statement2
...
done < /path/to/somefile
例,如果用户的ID号为偶数,则显示其名称和shell;对所有用户执行此操作;
while read LINE; do
Uid=`echo $LINE | cut -d: -f3`
if [ $[$Uid%2] -eq 0 ]; then
echo $LINE | cut -d: -f1,7
fi
done < /etc/passwd
三、until循环体
until 测试条件; do
语句1
语句2
...
done
条件不满足就循环,直到满足时,退出循环;
例,转换用户输入的字符为大写,除了quit(遇见quit退出);
read -p "A string: " String
until [ "$String" == 'quit' ]; do
echo $String | tr 'a-z' 'A-Z'
read -p "Next [quit for quiting]: " String
done
例,每隔5秒查看hadoop用户是否登录,如果登录,显示其登录并退出;否则,显示当前时间,并说明hadoop尚未登录:
who | grep "^hadoop" &> /dev/null
RetVal=$?
until [ $RetVal -eq 0 ]; do
date
sleep 5
who | grep "^hadoop" &> /dev/null
RetVal=$?
done
echo "hadoop is here."
until who | grep "^hadoop" &> /dev/null; do 命令可直接作为判断条件 无需两端中括号
date
sleep 5
done
echo "hadoop is here."
who | grep "^hadoop" &> /dev/null
RetVal=$?
while [ $RetVal -ne 0 ]; do
date
sleep 5
who | grep "^hadoop" &> /dev/null
RetVal=$?
done
echo "hadoop is here."
四、bash编程之变量替换进阶:
1.read -p "A string" -t 5 String -t5秒超时后使用默认值但变量本身为空。
${parameter:-word} 使用默认值但变量为空
2.${parameter:=word} 使用word并赋予变量此值 ,即赋予默认值。
3.${parameter:?word}不赋予变量任何值 显示word错误信息 输出到标准错误输出
4.${parameter:+word} 替换值 如果没有赋值无响应,如果赋值则显示word值而非变量值但未改变变量值
以上四种均在变量被引用时被触发,一般来说需要被引用前即赋予默认值,则如下,
String=${String:-word} 用户未输入时立即使用默认值
相关文章推荐
- bash脚本编程:while循环和until循环
- bash编程之while与until循环
- 【bash】编程之for,while,until循环
- linux raid while、until循环 bash组合测试条件
- 并行编程之多线程共享非volatile变量,会不会可能导致线程while死循环
- linux笔记 第六天 bash编程入门之变量、条件判断、条件测试和for循环等
- bash编程之变量替换 及 函数
- shell脚本编程-循环(for、while、until)
- [bash]for、while/until、循环控制、循环输出定向和管接
- bash脚本之case语句应用,while、until和select循环应用及其示例
- bash编程之变量替换
- bash --循环(for while until)
- Linux shell 编程之while循环、until循环
- linux shell编程学习--循环流程while,for,until命令
- 并行编程之多线程共享非volatile变量,会不会可能导致线程while死循环
- Shell编程之while&until循环详解
- bash 脚本之for,while,until循环
- shell 编程之 for while until 循环
- BASH编程基础之变量、条件判断