shell笔记(三)——其他循环结构
来源:互联网 发布:形容网络发达的句子 编辑:程序博客网 时间:2024/05/21 21:37
=====================================while语句========================================
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
fiif [ $# != 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语句格式
while
i=1
while [ $i -lt 10 ]
do
sed -n "${i}p" 111.txt
i=$(($i+1))
done
while command
pause函数,输入任何值继续,输入q退出程序
pause()
{
while echo "Press <return> to proceed or type q to quit:"
do
read cmd
case $cmd in
esac
done
break———— 用来跳出循环
continue—— 用来不执行余下的部分,直接跳到下一个循环
===========================================FOR语句===================================
for
[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
read i
i=$[$i+1]
echo $i[macg@localhost ~]$ sh ttt.sh
input:
6
7[macg@localhost ~]$ sh ttt.sh
input:
6
7
if (( $# != 3 )); then
fiif [ $# != 3 ]; then
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))
赋值=,read,export都不需要加变量$
- shell笔记(三)——其他循环结构
- Java笔记(三)之循环结构
- shell编程三种循环结构
- Linux Shell 笔记二(循环结构)
- Linux Shell笔记之结构循环
- Shell脚本学习笔记-循环结构
- 黑马程序员-C语言笔记(三)循环结构
- shell学习笔记(三)—— shell的配置文件
- shell笔记(二)——for循环
- c#循环结构(三)----for循环
- shell学习笔记三 分支结构
- Swift学习笔记(二十五)——循环结构
- Python基础教程笔记——条件,循环和其他语句
- Python基础教程笔记——条件,循环和其他语句
- shell脚本1——分支结构、循环、函数
- 第五章-条件、循环和其他语句——python基础教程(第二版)笔记
- shell编程 循环结构
- 05 shell循环结构
- 飘逸的python - 多条件排序及itemgetter的应用
- 杭电1159-Common Subsequence
- C#学习笔记02:程序结构
- kinetis晶体谐振器到底要不要加电容和电阻
- ::Coinitialize and ::CoUninitialize
- shell笔记(三)——其他循环结构
- poj 1679 Prim判断次短路
- vs2010添加TSTCON( ActiveX Control Test Container )工具
- MOS管构成的简易双向电平转换电路
- Eclipse的JNI环境搭建
- 嵌入式系统如何安装内核模块
- oracle日记
- POJ2960 寻找SG值
- 74HC/LS/HCT/F系列芯片的区别