shell基本语法
一、变量
1.变量的命名规则:以字母或下划线开头,后面跟数字,字母或下划线,最好不要随便命名,要做到看见变量名能猜出其含义
2.变量赋值: x=100
echo $x
删除变量:unset x
3.定义变量名的边界用大括号
[root@bogon ~]# egon_salary=20000
[root@bogon ~]# echo ${egon_salary}yuan
20000yuan
4.bash中不必声明数据类型,默认都是字符型
二、运算符
1.算术运算符:+ - * / %
[root@bogon ~]# echo $[5%2]
1
2.赋值运算符:=,+=,-=,*=,/=,%=
[root@bogon ~]# x=10
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11
3.关系运算符:<,>,!=,==,>=,<=,||,&&
关系运算符常与(( ))连用,[]可以达到同样的结果,但(( ))不能判断一个文件的类型,判断文件类型必须要用到[],[]又和test命令效果一样
用$?查看命令执行结果,结果为0代表真,非0代表假
[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0
4.shell里的计算器
之前说过用$[]可以进行一些简单的运算,但是如果涉及到小数的运算,就需要用到shell里面的计算器了
首先要安装软件,yum install -y bc
[root@bogon ~]# res=$(echo 'scale=2;1/3' |bc -l |cut -d'.' -f2)
[root@bogon ~]# echo ${res}%
33%
5.test 命令测试
test
-n str 字符串长度不为零
-z str 字符串长度为零
-b 文件存在且为块文件
-d 文件存在且为目录文件
-e 文件存在
-f 文件存在且为普通文件
-h 文件存在且为链接文件(同 -L)
-s 文件存在且大于零字节
文件之间的比较
file1 -nt file2 file1 的创建时间比file2晚
file1 -ot file2 file1 的创建时间比file2早
整数之间的比较
int1 -ne int2 int1和int2不相等
int1 -eq int2 int1和int2 相等
int1 -lt int2 int1小于int2
int1 -le int2 int1小于等于int2
int1 -gt int2 int1大于int2
int1 -ge int2 int1大于等于int2
字符串之间比较
str1 = str2 str1和str2相等
str1 !=str2 str1和str2不相等
表达式之间的比较
expression1 -a expression2 表达式1与表达式2都为真
expression1 -o expression2 表达式1或表达式2为真
6.测试举例
数字比较测试:
[root@bogon ~]# [[ 2 > 1 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20>10))
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20<10))
[root@bogon ~]# echo $?
1
字符串测试
[root@bogon ~]# [ "abc" = "abc" ]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ "abc" = "abc" ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (("abc" = "abc"))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (( a = a || 1 > 2 ))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a || 1 > 2 ]]
[root@bogon ~]# echo $?
0
单纯比较数字,用(( ))
除了单纯数字之外的比较,用[[ ]]
三、流程控制
1.if 分支机构
1)验证用户账号密码:
input your name : zhangcan
input password : 123
login successful
[root@bogon ~]# ./usertest.sh
input your name : hha
input password : hag
user or password error
#! /bin/bash
user='zhangcan'
password='' read -p 'input your name : ' name
read -p 'input password : ' code
if [ $name = $user -a $code = $password ];then
echo 'login successful'
else
echo 'user or password error'
fi
~
2)判断成绩档次
#!/bin/bash
#根据用户输入的成绩,判断所属档次,并输出给用户
read -p 'input your score : ' score
if [ $score -ge 90 ];then
echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
echo '及格'
elif [ $score -lt 60 ];then
echo '较差'
fi
2.while循环
while(条件)
do
命令
done
示例:判断用户输入的文件是何种类型
#!/bin/bash
while :
do
read -p 'input your file : ' file
if [ -z $file ];then
continue
else
break
fi
done
if [ -f $file ];then
echo "$file is regular file"
elif [ -b $file ];then
echo "$file is block file"
elif [ -d $file ];then
echo "$file is directory file"
else
echo "$file type unkonw"
fi
3.for循环
for i in {1..10} #in后面不一定是数字,只要是有返回结果的命令都可以
do
echo $i
done
示例1:写一个脚本,测试子网内可以使用的IP
#!/bin/bash
for i in {1..50}
do
ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次
if [ $? -ne 0 ];then
echo "192.168.16.$i successful"
echo "192.168.16.$i" >> ~/ipavailable.txt
fi
done
~
示例2:统计/dev下每种文件类型的数量
#!/bin/bash
dir='/dev'
for i in $(ls $dir)
do
if [ -h $dir/$i ];then
((link+=1))
elif [ -f $dir/$i ];then
(( rfile+=1))
elif [ -d $dir/$i ];then
((directory+=1))
elif [ -b $dir/$i ];then
(( block+=1 ))
else
(( typeunknow+=1))
fi
done
echo 'block' $block
echo 'regular file' $rfile
echo 'directory' $directory
echo 'link' $link
echo 'unknow' $typeunknow
4.嵌套循环
示例1:输出一个九九乘法表
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[$i*$j]"
done
echo
done
示例2:验证用户登陆账号密码,登陆成功后可以执行命令,当输入quit时退出
#!/bin/bash
user='zhangcan'
password=''
tag=true
while $tag
do
read -p 'input your name : ' name
read -p 'input your password : ' code
if [[ $name = $user ]] && [[ $code = $password ]];then
echo 'login successful'
while $tag
do
read -p '>>: ' cmd
if [[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done
shell基本语法的更多相关文章
- Shell函数语法
Shell函数语法 定义函数: function 函数名(){ 指令... } 调用函数,方法1: 函数名 调用函数,方法2: 函数名 参数一 参数二 return在函数里面使用会跳出函数并 ...
- shell 的语法
SHELL 的语法 n 变量:字符串,数字,环境和参数 n 条件:shell中的布尔值 n 程序控制:if, elif, for, while until, case n 命令列表 n 函数 ...
- shell脚本语法基础汇总
shell脚本语法基础汇总 将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n) out=$(cat text.txt) 输出1 2 3 out="$(cat te ...
- 运维shell全部语法进阶
Linux运维之shell脚本进阶篇 一.if语句的使用 1)语法规则 1 2 3 4 5 6 7 8 9 if [条件] then 指令 fi 或 if [条件];then ...
- U-Boot shell脚本语法
/********************************************************************** * U-Boot shell脚本语法 * 说明: * 之 ...
- (转)shlex — 解析 Shell 风格语法
原文:https://pythoncaff.com/docs/pymotw/shlex-parse-shell-style-syntaxes/171 这是一篇协同翻译的文章,你可以点击『我来翻译』按钮 ...
- shell 基础语法
shell 基础语法 =============================================== 推荐:http://c.biancheng.net/cpp/shell/ ==== ...
- Shell脚本语法---在Makefile等文件…
1. Shell脚本语法 1.1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Sta ...
- Shell的语法
Shell的语法: 变量:字符串.数字.环境和参数: 条件:shell中的布尔值: 程序控制:if.elif.for.while.until.case: 命令列表: 函数: Shell内置命令: 获取 ...
- shell基本语法备忘
1.第一行要写明shell种类 #!/bin/bash 2.打印输出 #!/bin/bashecho "Hello World !~" 3.变量定义 变量=前后不能有空格, ...
随机推荐
- 在.NET Core中使用Irony实现自己的查询语言语法解析器
在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建议,可以使用Compile As a Service的Roslyn为语 ...
- laravel 服务容器实现原理
前言 通过实现laravel 框架功能,以便深入理解laravel框架的先进思想. 什么是服务容器 服务容器是用来管理类依赖与运行依赖注入的工具.Laravel框架中就是使用服务容器来实现 ** 控制 ...
- vue-cli项目中怎么mock数据
在vue项目中, mock数据可以使用 node 的 express模块搭建服务 1. 在根目录下创建 test 目录, 用来存放模拟的 json 数据, 在 test 目录下创建模拟的数据 data ...
- 不用媒体查询做web响应式设计-遁地龙卷风
(0)写在前面 讲述知乎上看到的一篇文章中的一个案例,让我脑洞大开,佩服至极,特意第二天找到原文赞赏了 5元,原文地址https://zhuanlan.zhihu.com/p/27258076,案例用 ...
- web 直播&即时聊天------阿里云、融云(三)
经过前面的知识,基本已经把聊天室的功能搞定了,剩下的就是直播的问题了... 一如既往,阿里云的web demo也是少的可怜,只有一个web播放器(Prismplayer),所以这里主要就此播放器踩的坑 ...
- elememtui(有关权限的那些事)
前言:关于权限路由的那些事儿…… 业务情景描述:现有一个后台管理系统,共存在三种类型的人员,①超级管理员(称作1):②组别管理员(2):③普通用户(3):每种类型的人看到的操作栏并不一样,可以进行的操 ...
- Bash环境配置文件
一.环境配置文件读取优先级 其中~/.bash_profile,~/.bash_login,~/.profile三个文件只有一个有效,查找优先级从左至右降低.bash会一直检查是否有~/.bashrc ...
- Bash变量扩展修改符
1.未设置就临时替换(:-) 冒号:用来检验变量是否设置过,如果没有冒号,则认为设置过,不替换$fruit=peach$echo ${fruit:-plum}peach $fruit=$echo ${ ...
- 关于对WEB标准以及W3C的理解与认识问题
web标准简单来说可以分为结构.表现和行为.其中结构主要是有HTML标签组成.或许通俗点说,在页面body里面我们写入的标签都是为了页面的结构.表现即指css样式表,通过css可以是页面的结构标签更具 ...
- 使用CoApp创建NuGet C++静态库包
NuGet是微软开发平台下的包管理软件,使用它你可以非常方便的将一些第三方的库.框架整合进自己的项目中,省去了不少麻烦的配置过程.但是从官方文档上来看,貌似NuGet对C++的支持不是很好,并且在现阶 ...