脚本乐园 shell编程循环结构
来源:互联网 发布:仿m淘宝登录注册 编辑:程序博客网 时间:2024/05/16 15:17
while语句格式
while 表达式
do
command
command
done
while 和 if 的条件表达式完全相同,也是[ ] 或commad或test
While 表达式 If 表达式 表达式值为0,则循环继续 表达式值为0,then 表达式值为非0,则循环停止 表达式值为非0,else
最基本的i++ 条件型循环
i=1
while [ $i -lt 10 ]
do
sed -n "${i}p" 111.txt
i=$(($i+1)) 必须双层括号
done
命令型while 循环
while command 命令返回值0(成功执行),循环继续
pause函数,输入任何值继续,输入q退出程序
pause()
{
while echo "Press <return> to proceed or type q to quit:"
do
read cmd
case $cmd in
[qQ]) exit 1;; exit直接退到底,退出shell script
"") break;; break跳出循环
*) continue;; continue跳到循环底,重新开始新循环循环
esac
done
While echo … 此命令没有失败的可能,所以必须有break,return,exit之类的指令
while 关键字
break———— 用来跳出循环
continue—— 用来不执行余下的部分,直接跳到下一个循环
===========================================FOR语句===================================
for语句格式
for 表达式
do
command
command
done
i++,n=n+1 必须用双层括号 $(($num+1)) ,单层括号$($num+1)不管用
[root@mac-home home]# vi test.sh
:
echo "input num:"
read num
echo "input is $num"
num=$($num+1)
echo "new num is $num"
[root@mac-home home]# sh test.sh
input num:
3
input is 3
test.sh: line 6: 3+1: command not found
new num is[root@mac-home home]# vi test.sh
:
echo "input num:"
read num
echo "input is $num"
num=$(($num+1))
echo "new num is $num"
[root@mac-home home]# sh test.sh
input num:
3
input is 3
new num is 4
(( ))与[ ]作用完全相同
echo input:
read i
i=$(($i+1))
echo $i
echo input:
read i
i=$[$i+1]
echo $i[macg@localhost ~]$ sh ttt.sh
input:
6
7[macg@localhost ~]$ sh ttt.sh
input:
6
7
再证明(( ))与[ ]完全相同--------if (( ))
if (( $# != 3 )); then
echo "usage: $0 host user passwd"
exit 1
fi
if [ $# != 3 ]; then
echo "usage: $0 host user passwd"
exit 1
fi
[macg@localhost ~]$ sh ttt.sh 1 2
usage: ttt.sh host user passwd[macg@localhost ~]$ sh ttt.sh 1 2
usage: ttt.sh host user passwd
$foo=$(($foo+1)) # 运行的时候这个地方报错
给变量赋值,左边的变量不需要加 $ 符号,
foo=$(($foo+1))
赋值=,read,export都不需要加变量$
while
i=1
while [ $i -lt 10 ]
do
sed -n "${i}p" 111.txt
i=$(($i+1))
done
while command
pause()
{
while echo "Press <return> to proceed or type q to quit:"
do
read cmd
case $cmd in
esac
done
break———— 用来跳出循环
continue—— 用来不执行余下的部分,直接跳到下一个循环
===========================================FOR语句===================================
:
echo "input num:"
read num
echo "input is $num"
num=$($num+1)
echo "new num is $num"
[root@mac-home home]# sh test.sh
input num:
3
input is 3
test.sh: line 6: 3+1: command not found
new num is[root@mac-home home]# vi test.sh
:
echo "input num:"
read num
echo "input is $num"
num=$(($num+1))
echo "new num is $num"
[root@mac-home home]# sh test.sh
input num:
3
input is 3
new num is 4
read i
i=$(($i+1))
echo $i
read i
i=$[$i+1]
echo $i[macg@localhost ~]$ sh ttt.sh
input:
6
7[macg@localhost ~]$ sh ttt.sh
input:
6
7
fi
fi
usage: ttt.sh host user passwd[macg@localhost ~]$ sh ttt.sh 1 2
usage: ttt.sh host user passwd
给变量赋值,左边的变量不需要加 $ 符号,
foo=$(($foo+1))
赋值=,read,export都不需要加变量$
0 0
- 脚本乐园 shell编程循环结构
- 脚本乐园 shell编程for in 循环
- 脚本乐园 shell编程for in 循环
- 脚本乐园 shell编程case语句
- 脚本乐园 shell编程if语句
- shell编程 循环结构
- 脚本乐园 shell脚本中一些特殊符号
- 脚本乐园 shell中printf用法
- Shell脚本学习笔记-循环结构
- Shell脚本编程之While循环
- shell 脚本编程之循环语句
- shell脚本编程举例--for、while循环
- Shell脚本编程-分支与循环
- shell脚本编程之循环语句
- Linux shell编程 10 ---- 循环结构
- shell编程三种循环结构
- Shell编程---分支和循环结构
- Shell脚本编程之结构化命令
- java借助org.apache的fileUtils实现文件或者文件夹的拷贝
- 几种网络负载均衡方法比较
- google支付接口被刷以及解决方案
- Oracle 跟踪事件 set event
- javascript\html\CSS 选项卡
- 脚本乐园 shell编程循环结构
- IOS获取设备信息
- 总结iOS 8和Xcode 6的各种坑
- Git User Guide
- BIRT报表在开源项目jeesite项目中的集成
- Google Play In-app Billing
- [C++] foreach
- AngularJS学习(七)路由控制 ui-router
- 在hdf5文件中重命名dataset