1. 编写简单的hello world脚本,查看脚本内容,并运行
    使用bash命令运行shell脚本

    [lwj@192 file01]$ touch test.sh  //创建新文件
    [lwj@192 file01]$ vim test.sh  //编辑文件内容
    [lwj@192 file01]$ cat test.sh  //查看文件内容
    #!/bin/bash
    echo "Hello World!"
    [lwj@192 file01]$ bash test.sh  //运行shell脚本
    Hello World!
  2. 使用declare进行运算,declare命令仅支持整数的数值运算,使用一段代码进行比较运算
    [lwj@192 file01]$ declare -i var=10+10  //加法
    [lwj@192 file01]$ echo $var
    20
    [lwj@192 file01]$ declare -i var=10*10  //乘法
    [lwj@192 file01]$ echo $var
    100
    [lwj@192 file01]$ declare -i var=20-10  //减法
    [lwj@192 file01]$ echo $var
    10
    [lwj@192 file01]$ declare -i var=10/3  //取整
    [lwj@192 file01]$ echo $var
    3
    [lwj@192 file01]$ vim test.sh  //编辑文件
    [lwj@192 file01]$ cat -n test.sh  //查看文件1  #!/bin/bash2    if [ $# -ne 2 ];then3      echo "Usage: $0 number1 number2"4     exit 15  fi6 number1="$1"7    number2="$2"8    if (( number1>number2 ));then9      echo "$number1 > $number2"10    elif (( number1 <  number2 ));then11    echo "$number1 < $number2"12    elif (( number1 == number2 ));then13     echo "$number1 == $number2"14    else15     echo "should never be here."16 fi
    [lwj@192 file01]$ bash test.sh 1 2  //1和2比较
    1 < 2
    [lwj@192 file01]$ bash test.sh 2 1  //2和1比较
    2 > 1
    [lwj@192 file01]$ bash test.sh 2 2  //2和2比较
    2 == 2
    
  3. 使用test命令对两个数值比较(等于、小于、大于等于)
    [lwj@192 file01]$ vim myscript.sh
    [lwj@192 file01]$ cat myscript.sh
    #!/bin/bash
    a=11;b=16
    test $a -eq $b && echo "$a = $b" || echo "$a != $b"  //等于
    test $a -gt $b && echo "$a < $b" || echo "$a >= $b"  //小于
    test $a -ge $b && echo "$a >= $b" || echo "$a <  $b"  //大于等于
    [lwj@192 file01]$ sh myscript.sh
    11 != 16
    11 >= 16
    11 <  16
  4. 使用test命令对两个字符串比较(等于、非空),test字符串运算
    [lwj@192 file01]$ vim myscript.sh
    [lwj@192 file01]$ cat myscript.sh
    #!/bin/bash
    a="centos"
    test -z "$a" && echo "'$a' is null" || echo "'$a' is not null"  // 非空
    test "$a" ==  "centos" && echo "'$a'='centos'" || echo "'$a'!='centos'"  //等于
    [lwj@192 file01]$ sh myscript.sh
    'centos' is not null // 非空
    'centos'='centos'  //等于
    
  5. 使用test命令进行布尔运算,注释说明含义

    -a:当表达式$a和$b同时为真时复合表达式返回真,否则为假
    -o:当表达式$a和$b任意一个表达式为真时复合表达式返回真,否则为假

    [lwj@192 file01]$ vim myscript.sh
    [lwj@192 file01]$ cat myscript.sh
    #!/bin/bash
    a=11;b=16
    f1="myscript.sh"
    f2="file2"
    test  $b -o $a && echo "$a >= $b" || echo "$a < $b"
    test -e "$f1" -a -r "$f1" && echo "$f1 is readable" || echo "$f1 is not exist or not readable"
    [lwj@192 file01]$ sh myscript.sh
    11 >= 16
    myscript.sh is readable
  6. 使用[] 对文件类型、文件权限、文件比较等进行练习。

    文件类型-f:当文件fname的文件类型为普通文件时返回真,否则为假
    文件权限-r:当文件fname存在具有可读权限时返回真,否则为假
    文件比较-nt:当文件fname1比文件fname2新时返回真,否则为假

    [lwj@192 file01]$ vim myscript.sh
    [lwj@192 file01]$ cat myscript.sh
    #!/bin/bash
    f1="myscript.sh"
    f2="file2"
    [ -f "$f1" ] && echo "$f1:ordinary file" || echo "$f1:not ordinary file"
    [ -r "$f1" ] && echo "$f1:readable" || echo "$f1: not readable"
    [ "$f1" -nt "$f2" ] && echo "$f1 is newer than $f2" || echo "$f2 is newer than $f1"
    [lwj@192 file01]$ sh myscript.sh
    myscript.sh:ordinary file
    myscript.sh:readable
    myscript.sh is newer than file2
    
  7. 使用if-elif结构完成功能。猜数字游戏

    [lwj@192 file01]$ vim myscript.sh
    [lwj@192 file01]$ cat -n myscript.sh1  #!/bin/bash2    a=03   b=994  random_num=$(( $RANDOM*100/32767 ))5   read -p "请输入竞猜数字:" guess_num6 while [ $random_num -ne $guess_num ]7   do8   if [ $random_num -gt $guess_num ]; then9       echo "猜小了,范围变为 "$guess_num"-"$b",请输入竞猜数字:"10          a=$guess_num11             read guess_num12     elif [ $random_num -lt $guess_num ]; then13        echo "猜大了,范围变为 "$a"-"$guess_num",请输入竞猜数字:"14          b=$guess_num15             read guess_num16        else17          echo "请输入0-99的数字"18    fi19  done20  echo "恭喜,竞猜正确"
    [lwj@192 file01]$ sh myscript.sh
    请输入竞猜数字:50
    猜大了,范围变为 0-50,请输入竞猜数字:
    25
    猜大了,范围变为 0-25,请输入竞猜数字:
    13
    猜大了,范围变为 0-13,请输入竞猜数字:
    7
    猜大了,范围变为 0-7,请输入竞猜数字:
    4
    猜大了,范围变为 0-4,请输入竞猜数字:
    2
    猜大了,范围变为 0-2,请输入竞猜数字:
    1
    猜大了,范围变为 0-1,请输入竞猜数字:
    0
    恭喜,竞猜正确
    
  8. 结合case-in和函数,完成简易计算器。(选择运算符,再输入数)

    [lwj@192 file01]$ vim mytest.sh
    [lwj@192 file01]$ cat -n mytest.sh1    #!/bin/bash2    y=y3   while [ "$y" === "y" ] || [ "$y" == "Y" ]4 do5   read -p "please input the first num:" num16   read -p "please input the operator:" a7   read -p "please input the second num:" num28    case "$a" in9      +)10         echo "运算结果为:$(($num1+$num2))"11          ;;12      -)echo "运算结果为:$(($num1-$num2))"13           ;;14      *)echo "运算结果为:$((%num1*$num2))"15           ;;16      /)echo "运算结果为:$(($num1/$num2))"17           ;;18 esac19  read -p "是否还要继续运算(y/n):" y20  done
    [lwj@192 file01]$ sh mytest.sh
    please input the first num:9
    please input the operator:-
    please input the second num:2
    运算结果为 :7
    是否还要将继续运算(y/n):y
    please input the first num:3
    please input the operator:+
    please input the second num:2
    运算结果为 :5
    是否还要将继续运算(y/n):y
    please input the first num:3
    please input the operator:*
    please input the second num:3
    运算结果为 :9
    是否还要将继续运算(y/n):n
  9. 对函数传递参数大于、等于、小于所需参数的三种情形分别试验

[lwj@192 file01]$ vim myhs.sh
[lwj@192 file01]$ cat -n myhs.sh1  #!/bin/bash2    function sum()3 {4   echo 'input paramenters are:$@ = "'$@'"'5  if (test $# -lt 2);then6    echo "传递的参数小于指定参数个数"7     return 18   elif (test $# -gt 2);then9  echo "传递的参数大于指定参数个数"10    return 111  fi12    echo "传递的参数等于指定参数个数"13    echo "var_sum=$(($1+$2))"14    }15 sum 116 sum 1 217   sum 1 2 3
[lwj@192 file01]$ sh myhs.sh
input paramenters are:$@ = "1"
传递的参数小于指定参数个数
input paramenters are:$@ = "1 2"
传递的参数等于指定参数个数
var_sum=3
input paramenters are:$@ = "1 2 3"
传递的参数大于指定参数个数

CentOS 终端命令(5)Bash变量运算符和条件测试分支结构相关推荐

  1. shell脚本编程学习笔记3(xdl)——Bash变量的运算与测试

    Bash变量的运算与测试 1,数值运算和运算符 declare申明变量类型也就是说变量运算没有申明的时候,会以字符串形式的拼接, 只有把变量声明为数值型的时候才可以运算,申明变量为数值型有 三种方法方 ...

  2. 很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序...

    很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用m ...

  3. shell脚本中的特殊变量与if条件测试

    1.特殊变量 实际工作中我们不可避免的遇到一些xxxx.sh脚本文件,实际阅读shell脚本代码时经常会遇到很多特殊变量(例如:$0.$n.$#.$@.$*.$?.$$等),我们常常会被这些特殊符号折 ...

  4. CentOS终端命令(3)vim操作

    vim: 用于文本编辑,而非文字排版 (1)使用↑↓←→上下左右键移动                                                          (2) 复制光 ...

  5. linux命令框显示中文乱码_CentOS终端命令行显示中文乱码的解决方法

    这篇文章主要介绍了CentOS终端命令行显示中文乱码的解决方法,以及测试方法和缺少中文字体时的解决方法,需要的朋友可以参考下 安装CentOS的时候选择了中文,结果在终端不能显示中文,都是乱码,解决方 ...

  6. Linux初级运维(七)——bash脚本编程(常见测试)

    一.bash中常用的条件测试 测试方法: [ expression ] [[expression]] test expression 1.整数测试 -gt:大于 -le:小于等于 -ne:不等于 -e ...

  7. linux emacs命令,Linux Bash Shell 终端 Terminal Emacs 模式 常用 快捷键 命令

    背景: OS:Ubuntu 16.04 Bash 的编辑模式:Emacs 模式 本文介绍的快捷键 仅当 您的 Bash 的编辑模式 为 Emacs 模式 时 有效,Bash 默认的编辑模式为 Emac ...

  8. 【快速入门Linux】3_Linux命令—终端命令格式、命令帮助信息、bash标准输入输出

    文章目录 一.终端命令格式 二.查阅命令帮助信息 2.1 --help 2.2 man 2.2.1 使用 `man` 时的操作键 三.bash的标准输入输出 3.1 名词解释 3.2 相关符号 一.终 ...

  9. centOS安装成功后如何进入图形界面以及如何打开终端命令?

    一.我们将centOSan安装好以后会显示如下图: 二.那我们如何进入图形操作页面呢? 1.安装X(X Window System),命令如下:yum groupinstall "X Win ...

最新文章

  1. tableau必知必会之仪表板的最佳实践
  2. Linux 条件变量使用细节(为何调用 pthread_cond_wait 前加锁,函数内部解锁,返回时又加锁)
  3. Spring Android 1.0.0.M3 发布
  4. 【BZOJ2989】数列(CDQ分治,扫描线)
  5. 手把手教你用Vue.js封装Form组件
  6. Shell 脚本进程并发进程数控制
  7. Excel怎么将数据数据首尾倒置
  8. hexo博客中如何插入图片
  9. 霍树棠《忆真妃》又名《剑阁闻铃》
  10. JAVA读取Excel行数问题
  11. 天河二号上运行ZHT(a zero-hop distributed table)
  12. gitbook踩坑指南-无法转pdf、epub等;pdf调字体大小
  13. springBoot使用poi导出Excel
  14. jenkins 下载插件失败处理办法
  15. 学渣的刷题之旅 leetcode刷题 26. 删除排序数组中的重复项
  16. HTTP协议、【HTTP请求、响应格式】及一次HTTP请求的完整过程
  17. h5 生成图片并保存到手机(浏览器)
  18. PB处理BLOB类型的方法
  19. 胡策day 10.26 T2 人、镜子与墙
  20. 无法安装战网,提示007D

热门文章

  1. 单片机有锁存为啥子还需要while循环
  2. tensorflow实现FM算法
  3. 热敏式 打印机 小票排版
  4. 题目中常见的英文单词
  5. 强化学习实践:DDQN—LunarLander月球登入初探
  6. VMware Workstation设置静态IP(固定IP)踩坑记及常见问题
  7. 《多导睡眠图学》--童茂荣学习记录
  8. VAE与GAN的关系(1)
  9. 《Python编程:从入门到实践》第二章练习题
  10. 使用MobileNetV3的PSPNet网络结构