一、变量

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基本语法的更多相关文章

  1. Shell函数语法

    Shell函数语法 定义函数: function   函数名(){ 指令... } 调用函数,方法1: 函数名 调用函数,方法2: 函数名  参数一   参数二 return在函数里面使用会跳出函数并 ...

  2. shell 的语法

    SHELL 的语法 n  变量:字符串,数字,环境和参数 n  条件:shell中的布尔值 n  程序控制:if, elif, for, while until, case n  命令列表 n  函数 ...

  3. shell脚本语法基础汇总

    shell脚本语法基础汇总 将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n) out=$(cat text.txt) 输出1 2 3 out="$(cat te ...

  4. 运维shell全部语法进阶

    Linux运维之shell脚本进阶篇 一.if语句的使用 1)语法规则 1 2 3 4 5 6 7 8 9 if [条件]     then         指令 fi 或 if [条件];then ...

  5. U-Boot shell脚本语法

    /********************************************************************** * U-Boot shell脚本语法 * 说明: * 之 ...

  6. (转)shlex — 解析 Shell 风格语法

    原文:https://pythoncaff.com/docs/pymotw/shlex-parse-shell-style-syntaxes/171 这是一篇协同翻译的文章,你可以点击『我来翻译』按钮 ...

  7. shell 基础语法

    shell 基础语法 =============================================== 推荐:http://c.biancheng.net/cpp/shell/ ==== ...

  8. Shell脚本语法---在Makefile等文件…

    1. Shell脚本语法 1.1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Sta ...

  9. Shell的语法

    Shell的语法: 变量:字符串.数字.环境和参数: 条件:shell中的布尔值: 程序控制:if.elif.for.while.until.case: 命令列表: 函数: Shell内置命令: 获取 ...

  10. shell基本语法备忘

    1.第一行要写明shell种类 #!/bin/bash   2.打印输出 #!/bin/bashecho "Hello World !~"   3.变量定义 变量=前后不能有空格, ...

随机推荐

  1. WPF中用户控件对比自定义控件(UserControl VS CustomControl)

    接着这篇文章(http://www.cnblogs.com/shiyue/archive/2013/02/02/2889907.html)写: 用户控件(组合) 用于在一个项目中使用多次 自定义控件( ...

  2. Python 的枚举 Enum

    枚举是常用的功能,看看Python的枚举. from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May' ...

  3. JAVAEE学习——struts2_03:OGNL表达式、OGNL与Struts2的结合和练习:客户列表

    一.OGNL表达式 1.简介 OGNL:对象视图导航语言.  ${user.addr.name} 这种写法就叫对象视图导航. OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 2.使用OG ...

  4. EF添加

    1.添加单个模型(CreatRule()是构造模型)(Shop_ActivityRuleProduct是类) var rule = CreatRule(model); var ruled = db.S ...

  5. navicat连接oracle 报 ORA-12737 set CHS16GBK

    1首先,我们打开“工具”-->"选项"菜单,见到如下界面,依据OCI library(oci.dll) 路径,导航到 navicat oci 目录下,备份里面的文件(通过在该 ...

  6. docker 内部组件结构 -- docker daemon, container,runC

    Docker, Containerd, RunC : 从 Docker 1.11 开始, docker 容器运行已经不是简单地通过 Docker Daemon 来启动, 而是集成了Container, ...

  7. Spring 3整合Quartz 2实现手动设置定时任务:新增,修改,删除,暂停和恢复(附带源码)

    摘要:在项目的管理功能中,对定时任务的管理有时会很常见.但一般定时任务配置都在xml中完成,包括cronExpression表达式,十分的方便.但是如果我的任务信息是保存在数据库的,想要动态的初始化, ...

  8. ⑨的完美冻青蛙(frog)

    ⑨的完美冻青蛙(frog) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 第一行是一个正整数n,表示上式中的p的个数.   接下来有n行,每一行两个正整数pi 和ei . 输出 ...

  9. Vuejs技术栈从CLI到打包上线实战全解析

    前言 本文是自己vue项目实践中的一些总结,针对Vue2及相关技术栈,实践中版本为2.3.3. 开发前须知 vue-cli 在开发前,我们要至少通读一遍vue官方文档和API(看官方文档是最重要的,胜 ...

  10. 有关LinkedList常用方法的源码解析

    上文里解析了有关ArrayList中的几个常用方法的源码——<有关ArrayList常用方法的源码解析>,本文将对LinkedList的常用方法做简要解析. LinkedList是基于链表 ...