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.变量定义 变量=前后不能有空格, ...
随机推荐
- C++进阶引导
1.C++的用途和意义 t0185b047e29feffc26.jpg 总体来说,C++作为一门软件开发语言,它的流行度是在减少的.主要原因在于语言的复杂和灵活导致软件开发成本提高,这体现在开发周期和 ...
- Java中SimpleDateFormat用法详解
所有已实现的接口: Serializable, Cloneable SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本) ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- RabbitMQ分布式消息队列服务器(一、Windows下安装和部署)
RabbitMQ消息队列服务器在Windows下的安装和部署-> 一.Erlang语言环境的搭建 RabbitMQ开源消息队列服务是使用Erlang语言开发的,因此我们要使用他就必须先进行Erl ...
- 对象级别锁 vs 类级别锁 – Java
同步针对的是多线程.同步的方法或代码块同时只能由一个线程执行. Java支持多线程来执行.这可能会导致两个或多个线程访问同一个字段或对象.同步是一个使所有并发执行的线程同步的过程.同步避免了由于共享内 ...
- Bash变量扩展修改符
1.未设置就临时替换(:-) 冒号:用来检验变量是否设置过,如果没有冒号,则认为设置过,不替换$fruit=peach$echo ${fruit:-plum}peach $fruit=$echo ${ ...
- 从String类型字符串的比较到StringBuffer和StringBuilder
1. String类型 String类源码 为了从本质上理解String类型的特性所在,我们从String类型的源码看起,在源码中String类的注释中存在以下: /**Strings are con ...
- linux中怎么进入root用户
如果你是第一次使用root用户,需要设置root用户密码:passwd root 根据提示输入然后切换到root用户:su root回车输入密码 回车
- Intent解析
一.综述 intent对象是一个信息桶.它包含了接收它的组件感兴趣的信息(如:携带的动作和数据),附加Android系统感兴趣的信息(如:处理intent和启动目标Activity指令的组件的类别) ...
- JAVA基础——数组详解
学习JAVA中数组的使用 一.什么是数组? 问:编写代码保存 4 名学生的考试成绩. 答:简单啊,定义 4 个变量呗 问:那"计算全年级 400 名学生的考试成绩",肿么办 答: ...