Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select
1、expr计算整数变量值
格式 :expr arg
1、分步计算,即先计算2+3,再对其和乘4
s=`expr 2 + 3`
expr $s \* 4
2、一步完成计算:
expr `expr 2 + 3 ` \* 4
–:expr 3 \* 2 expr 3 “*” 2 expr 3 ‘*’ 2
[fsy@localhost ~]$ s=`expr 2 + 3`
[fsy@localhost ~]$ echo $s
5
[fsy@localhost ~]$ expr $s \* 4
20
[fsy@localhost ~]$ expr `expr 2 + 3` \* 4
20
[fsy@localhost ~]$ expr 2 \* 3
6
[fsy@localhost ~]$ expr 2 "*" 3
6
[fsy@localhost ~]$ expr 2 '*' 3
6
[fsy@localhost ~]$ expr 2 * 3
expr: 语法错误
[fsy@localhost ~]$
–例子:计算(2+3)×4的值
[fsy@localhost ~]$ let s=(2+3)*4
[fsy@localhost ~]$ echo $s
20
[fsy@localhost ~]$
3、for语句——坑爹的开始...... 和其他语言的for不同!
对一组参数进行一个操作语法格式:for 变量 in 列表do命令行(通常用到循环变量)done说明:–“列表”为存储了一系列值的列表,随着循环的进行,变量从列表中的第一个值依次取到最后一个值;–do和done之间的命令通常为根据变量进行处理的一系列命令,这些命令每次循环都执行一次;–如果“ in 列表”部分省略掉,Bash则认为是“in $@”,即执行该程序时通过命令行传给程序的所有参数的列表。例1、自定义列表
#!/bin/bash
for var in one two three four five
do
echo ------
echo '$var is' $var
done
运行输出:
------
$var is one
------
$var is two
------
$var is three
------
$var is four
------
$var is five
例2、以命令返回值作为列表
#!/bin/bash
for var in `ls`
do
echo -----
echo $var
done
运行输出:
-----
abb
-----
abc
-----
a.out
-----
a.sh
例3、命令行参数指定为列表中的数值
#!/bin/bash for var
do
echo "It's $var"
done
运行输出:
[fsy@localhost ~]$ sh c.sh a b c d
It's a
It's b
It's c
It's d
4、while语句
while 表达式do命令行done
#!/bin/bash
num=1
while [ $num -le 10 ]
do
echo -e "\t the num is $num"
let num=num+1
done
运行输出:
the num is 1
the num is 2
the num is 3
the num is 4
the num is 5
the num is 6
the num is 7
the num is 8
the num is 9
the num is 10
5、until语句
unitil 表达式do命令行done
例:计算1到10的和
#!/bin/bash sum=0
num=10 until test $num -eq 0
do
sum=`expr $sum + $num`
num=`expr $num - 1`
done
echo "sum = $sum"
运行输出
sum = 55
#!/bin/bash while [ -n "$*" ]
do
echo $1 $2 $3 $4 $5 $6
shift
done
运行输出
[fsy@localhost scripts]$ sh b.sh 1 2 3 4 5 6 7
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
7、if语句
if 条件表达式then #当条件为真时执行以下语句命令列表else #当条件为假时执行以下语句命令列表fi
例:
#!/bin/bash if test -f "$1"
then
echo "$1 is an ordinary file"
else
echo "$1 is not an ordinary file"
fi
运行输出:
[fsy@localhost scripts]$ sh c.sh abb
abb is not an ordinary file
[fsy@localhost scripts]$ sh c.sh a.sh
a.sh is an ordinary file
[fsy@localhost scripts]$
8、case语句
例:
#!/bin/bash case $1 in
1)
echo " you choice is 1";;
2)
echo " your choice is 2";;
*)
echo " your choice is others";;
esac
运行输出:
[fsy@localhost scripts]$ sh d.sh 1
you choice is 1
[fsy@localhost scripts]$ sh d.sh 2
your choice is 2
[fsy@localhost scripts]$ sh d.sh 3
your choice is others
9、break与continue
10、函数
例:
#!/bin/bash
num=1
hello()
{
echo "hello boy~ It's our $num meeting"
let num=num+1
}
hello
hello
hello
运行输出
hello boy~ It's our 1 meeting
hello boy~ It's our 2 meeting
hello boy~ It's our 3 meeting
11、select语句
格式:
select 变量 in 列表do命令行(通常用到循环变量)done
制作一个选择表,在列表中选择一个选项执行命令行。如果选择的变量不在列表序列中,则返回一个空值。需要用break退出循环。
例子:
#!/bin/bash echo "a is 5 ,b is 3. Please select your method: " a=5
b=3 select var in "a+b" "a-b" "a*b" "a/b"
do
break
done case $var in
"a+b")
echo 'a+b= '`expr $a + $b`;;
"a-b")
echo 'a-b= '`expr $a - $b`;;
"a*b")
echo 'a*b= '`expr $a \* $b`;;
"a/b")
echo 'a/b= '`expr $a / $b`;;
*)
echo "input error"
esac
运行输出:
[fsy@localhost scripts]$ sh e.sh
a is 5 ,b is 3. Please select your method:
1) a+b
2) a-b
3) a*b
4) a/b
#? 1
a+b= 8
Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select的更多相关文章
- 【转】linux expr命令参数及用法详解
在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...
- (转)linux expr命令参数及用法详解
linux expr命令参数及用法详解 原文:http://blog.csdn.net/tianmohust/article/details/7628694 expr用法 expr命令一般用于整数值, ...
- Linux时间子系统之(三):用户空间接口函数
专题文档汇总目录 Notes:用户空间时间相关接口函数: 类型 API 精度 说明 时间 time stime time_t 精度为秒级 逐渐要被淘汰.需要定义__ARCH_WANT_SYS_TIME ...
- linux expr命令参数及用法详解
expr用法 expr命令一般用于整数值,但也可用于字符串.一般格式为: #expr argument operator argument expr也是一个手工命令行计数器. #$expr 10 + ...
- Linux kernel 内存 - 页表映射(SHIFT,SIZE,MASK)和转换(32位,64位)
0. Intro 如下是在32位下的情况,32位下,只有三级页表:PGD,PMD,PTE 在64位情况下,会有四级页表:PGD,PUD,PMD,PTE 但是原理基本上是一样的,本文主要是想记录一下页表 ...
- linux中模块的构建,传参,和printk函数的简单使用
静态编译,动态加载应用想访问内核需要通过系统调用 驱动:1.模块(打包,加入内核)2.内核机制3.操作硬件 在Kconfig里面配置menuconfig的时候,不同的类型会在图形化界面的终端显示不用的 ...
- linux下代替system的基于管道的popen和pclose函数
linux下使用system需要谨慎,那么代替它的方法是什么呢? 标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由pop ...
- Linux系统编程(3)——文件与IO之fcntl函数
linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...
- 三十四、Linux 进程与信号——信号特点、信号集和信号屏蔽函数
34.1 信号特点 信号的发生是随机的,但信号在何种条件下发生是可预测的 进程杠开始启动时,所有信号的处理方式要么默认,要么忽略:忽略是 SIGUSR1 和 SIGUSR2 两个信号,其他都采取默认方 ...
随机推荐
- JMeter跨线程组保持登录(多线程组共享cookie)
使用__setProperty设置全局变量: 1.jmeter中创建一个登录请求,然后执行,察看结果树-->查看返回cookie信息,我的是在Response data中的 Response h ...
- 『与善仁』Appium基础 — 7、ADB Shell命令的使用
目录 1.查看进程 2.查看实时资源占用情况 3.查看进程 UID 4.其它ADB Shell命令说明 Android系统是基于 Linux 内核的,也就是说Android系统的底层是Linux系统. ...
- Intellij IDEA 配置Junit
导包: 1.Hamcrest Core 包: https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core/1.3 2.Junit包 ...
- c++学习笔记(五)
数组作为函数参数 定义 数组可以作为函数的参数使用,进行数据传送. 数组用作函数参数有两种形式,一种是把数组元素(下标变量)作为实参使用:另一种是把数组名作为函数的形参和实参使用. 1.数组元素作为函 ...
- 微信小程序(二)
创建项目: hello.wxml hello world 每个学习的开始 <view>hello world!</view> hello.js 像 app.js 一样 Page ...
- 浏览器 Proxy SwitchyOmega 插件设置代理访问内网服务器
使用Proxy SwitchyOmega 插件通过代理 直接访问到内网网站 一.使用场景 如下图所示,如果在电脑的网络设置中开启代理,每次更换代理就需要进入这里设置改变代理.且我们可能回需求到两个网页 ...
- 如何将rabbitmq集群中的某个节点移除.
首先将要移除的节点停机. root@rabbitmq-03:~# rabbitmqctl stop Stopping and halting node 'rabbit@rabbitmq-03' ... ...
- python网络自动化运维paramiko实验
运行环境: 物理机:win10 1903 网络设备:EVE-NG模拟器上运行思科三层路由器 网络设备OS版本:cisco ios(versions 15.6) python环境:pycharm 3.5 ...
- 简单聊下.NET6 Minimal API的使用方式
前言 随着.Net6的发布,微软也改进了对之前ASP.NET Core构建方式,使用了新的Minimal API模式.之前默认的方式是需要在Startup中注册IOC和中间件相关,但是在Minimal ...
- distmat 计算遗传距离
distmat 可用于计算遗传距离,得到距离矩阵 1 在线运算 可通过在线进行遗传距离的计算,网址:http://www.bioinformatics.nl/cgi-bin/emboss/distma ...