zoukankan      html  css  js  c++  java
  • Shell 基础笔记

    1-22-shell脚本的基础

    本节所讲内容:

         shell 基本语法

         变量

    第1章 什么是SHELL?.. 2

    1.1 shell编程.. 3

    第2章 shell变量及运用.. 5

    2.1 shell变量.. 5

    2.2 主要赋值类型.. 6

    2.3 给变量赋值多个单词.. 8

    2.4 把命令当作变量进行定义.. 9

    2.5 设定一个永久变量.. 9

    第3章 对变量的管理.. 10

    3.1 列出所有的变量.. 10

    3.2 删除变量.. 10

    第4章 位置变量和特殊变量.. 10

    4.1 位置变量.. 10

    4.2 特殊变量.. 11

    第5章 小综合实例.. 17

    5.1 变量在shell中的使用.. 17

    5.2 特殊变量的测试.. 17

    第6章 变量的数值计算.. 18

    第7章 Read命令.. 23

    7.1 定义.. 23

    7.2 参数用法.. 24

    第1章 什么是SHELL?

    【例1】先看一个简单的shell程序

    写入以下内容

    [root@xuegod67 scripts]# cat 1.sh

    #!/bin/bash

    #author : panda

    # change-date:2016-11-05

    # --------------------------------------------

    ########脚本测试开始##############

    echo "What is your name?"

    read person

    echo "Hello, $person"

    sleep 2

    date

    sleep 2

    a="game over"

    echo "$person,$a"

    ##########脚本测试结束###########

    [root@xuegod67 scripts]# sh 1.sh

    What is your name?

    awk

    Hello, awk

    2016年 11月 02日 星期三 18:48:00 CST

    awk,game over

    1.1 shell编程

    编程语言:

    (1)机器语言

    (2)汇编语言

    (3)高级语言

           静态语言:编译型语言  c  c++  java

         动态语言:解释型语言  php  shell  python   perl     

    编译器:(解释器)  将人类理解的语言翻译成机器理解的语言

    #!/bin/bash   #!跟shell命令的完全路径。 作用:显示后期命令以哪种shell来执行这些命令。如不指shell,以当前shell作为执行的shell。

    以shell中以#开始头表示,整个行就被当作一个注释。执行时被忽略。

    shell程序一般以.sh结尾,当然主要是看是否为执行,然后看里面有没有#!

    脚本的执行方式:

    [root@xuegod67 scripts]# ./1.sh

    [root@xuegod67 scripts]#sh 1.sh

    [root@xuegod67 scripts]#bash 1.sh

    [root@xuegod67 scripts]# chmod +x 1.sh

    [root@xuegod67 scripts]# /scripts/1.sh

    [root@xuegod67 scripts]# ./1.sh

    总结

    1、  脚本的执行顺序是从上到下,从左到右执行的

    2、  脚本里内容,可以理解为:好多单个命令集合,复杂一点脚本就是加上它自己的一些语法

    创建shell程序的步骤:

    第一步:创建一个包含命令和控制结构的shell文件。

    第二步:修改这个文件的权限使它可以执行。使用chmod u+x

    第三步:执行

    方法1:./example01.sh

           方法2: 使用绝对路径  [root@panda scripts]# /root/scripts/example01.sh

           方法3:[root@panda scripts]# bash example01.sh

           方法4:[root@panda scripts]#sh example01.sh 

           #注:example01.sh可以不用x权限

    第2章 shell变量及运用

    2.1 shell变量

    变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名。

    变量的引用:加上$符号

    【例2】x=3 

    [root@xuegod67 scripts]# x=3

    [root@xuegod67 scripts]# echo $x

    3

    Shell 有两类变量:临时变量和永久变量。

    (1)临时变量:是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。

    (2)永久变量:是环境变量,其值不随shell 脚本的执行结束而消失。

    【例3】$PATH   

    [root@panda scripts]# echo $PATH

    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

    作用:运行某个命令的时候,本地查找不到某个命令或文件,会到这个声明的目录中去查找。

    (3)用户定义变量

    由字母或下划线打头,不允许数字开头,后面由字母、数字或下划线组成,并且大小写字母意义不同。变量名长度没有限制。

    使用变量值时,要在变量名前加上前缀“$”。

    [root@xuegod67 scripts]# var=123

    [root@xuegod67 scripts]# echo var

    var

    [root@xuegod67 scripts]# echo $var

    123

    2.2 主要赋值类型

    (1)变量赋值 :赋值号“=”两边应没有空格。

    【例4】用“=”对变量赋值

    [root@xuegod67 scripts]# A=aaa

    [root@xuegod67 scripts]# A = aaa

    -bash: A: command not found

    [root@xuegod67 scripts]# A =aaa

    -bash: A: command not found

    [root@xuegod67 scripts]# A= aaa

    -bash: aaa: command not found

    (2)将一个命令的执行结果赋给变量

    [root@xuegod67 scripts]# A=date

    [root@xuegod67 scripts]# echo $A

    date

    [root@xuegod67 scripts]# A=`date`

    [root@xuegod67 scripts]# echo $A

    2016年 11月 02日 星期三 19:23:27 CST

    【例5】用“=”对命令赋值

    [root@xuegod67 scripts]# B=$(ls -l)

    [root@xuegod67 scripts]# echo $B

    总用量 36 -rw-r--r-- 1 root root 158 11月 2 18:05 17-1.sh -rw-r--r-- 1 root root 190 11月 2 18:06 17-2.sh -rw-r--r-- 1 root root 156 11月 2 18:03 17.sh -rwxr-xr-x 1 root root 296 11月 2 18:34 1.sh -rw-r--r-- 1 root root 186 11月 2 18:11 20.sh -rw-r--r-- 1 root root 231 11月 2 18:32 21.sh -rw-r--r-- 1 root root 292 11月 2 18:26 22.sh -rw-r--r-- 1 root root 26 11月 2 19:12 test.sh -rw-r--r-- 1 root root 259 11月 2 17:35 ts.sh

    [root@xuegod67 scripts]# A=$B

    [root@xuegod67 scripts]# echo $A

    总用量 36 -rw-r--r-- 1 root root 158 11月 2 18:05 17-1.sh -rw-r--r-- 1 root root 190 11月 2 18:06 17-2.sh -rw-r--r-- 1 root root 156 11月 2 18:03 17.sh -rwxr-xr-x 1 root root 296 11月 2 18:34 1.sh -rw-r--r-- 1 root root 186 11月 2 18:11 20.sh -rw-r--r-- 1 root root 231 11月 2 18:32 21.sh -rw-r--r-- 1 root root 292 11月 2 18:26 22.sh -rw-r--r-- 1 root root 26 11月 2 19:12 test.sh -rw-r--r-- 1 root root 259 11月 2 17:35 ts.sh

    (3)可以利用变量和其它字符组成一个新的字符串

    [root@xuegod67 scripts]# MYDIR=/home/mk

    [root@xuegod67 scripts]# echo $MYDIRzhangsan

    [root@xuegod67 scripts]# /home/mk/zhangsan^C

    [root@xuegod67 scripts]# echo $MYDIR/zhangsan

    /home/mk/zhangsan

    [root@xuegod67 scripts]# echo ${MYDIR}/zhangsan

    /home/mk/zhangsan

    [root@xuegod67 scripts]# DAY=mon

    [root@xuegod67 scripts]# echo today is $DAYday

    today is

    [root@xuegod67 scripts]# echo today is ${DAY}day

    today is monday

    2.3 给变量赋值多个单词

    [root@xuegod67 scripts]# name="mike ron"

    [root@xuegod67 scripts]# echo $name

    mike ron

    [root@xuegod67 scripts]# name='shen mk'

    [root@xuegod67 scripts]# echo $name

    shen mk

    【例6】赋值时“”和’’的区别

    总结:

    q  单引号:之间的内容原封不动地指定给了变量。

    q  双引号:特殊符号的含义保留。

    【例7】变量与符号的综合演示

    [root@panda ~]# a=192.168.1.63

    [root@panda ~l]# b='192.168.1.64'

    [root@panda ~]# c="192.168.1.65"

    [root@panda ~]# echo "a=$a"

    a=192.168.1.63

    [root@panda ~]# echo 'b=$b'

    b=$b

    [root@panda ~]# echo "c=${c}"   #c=${c}等同c=$c

    c=192.168.1.65

    2.4 把命令当作变量进行定义

    [root@xuegod67 scripts]# wen=`date +%F`    #利用反引号进行定义

    [root@xuegod67 scripts]# echo $wen

    2016-11-02

    [root@xuegod67 scripts]# wen=$(date)      #利用小括号加$进行定义

    [root@xuegod67 scripts]# echo $wen

    2016年 11月 02日 星期三 19:44:20 CST

    2.5 设定一个永久变量

    [root@xuegod67 scripts]# export PATH=/opt:$PATH

    [root@xuegod67 ~]# vim .bashrc

    [root@xuegod67 ~]# source .bashrc

    [mk@xuegod67 ~]$ . ./.bash_profile

    第3章 对变量的管理

    3.1 列出所有的变量

    set 命令,例:

    [root@panda scripts]# DAY=mon

    [root@panda scripts]# set|grep DAY

    DAY=mon

    3.2 删除变量

    [root@panda scripts]# unset DAY

    [root@panda scripts]# echo $DAY

    第4章 位置变量和特殊变量

    4.1 位置变量

    Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其它字作为参数。由出现在命令行上的位置确定的参数称为位置参数。 

    使用$N 来表示

    $0  获取当前执行shell脚本的文件文件名,包括脚本路径

    $n  获取当前脚本的第n个参数 n=0,1,2.....n 当n大于9时 用{n}表示。

    【例8】传递参数

    [root@panda scripts]# cat 8.sh

    #!/bin/bash

    # author:awk

    echo "----------------------"

    echo "Shell 传递参数实例!";

    echo "执行的文件名:$0";

    echo "第一个参数为:$1";

    echo "第二个参数为:$2";

    echo "第三个参数为:$3";

    echo "----------------------"

    [root@panda scripts]# sh 8.sh 1 2 3

    ----------------------

    Shell 传递参数实例!

    执行的文件名:example02.sh

    第一个参数为:1

    第二个参数为:2

    第三个参数为:3

    ----------------------

    4.2 特殊变量

    有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,以下是一些特殊变量:

    $*

    以一个单字符串显示所有向脚本传递的参数;

    如"$*"用【"】括起来的情况、以"$1 $2 … $n"的形式输出所有参数

    $#

    传递到脚本的参数个数

    $@

    与$*相同,但是使用时加引号,并在引号中返回每个参数

    如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数

    $$

    这个程序的PID

    $?

    显示最后命令的退出状态;0表示没有错误,其他任何值表明有错误

    $!

    上一个后台进程的ID

    【例9】

    [root@panda shell]# cat 9.sh

    echo $1 $2 $3 $4 $5 $6 $7 $8 $9

    echo "脚本名字"

    echo $0

    echo "脚本参数个数"

    echo "the number of values is $#"

    echo "脚本参数列表"

    echo  $*

    echo "脚本参数列表"

    echo  $@

    echo "上一次执行结果的状态码"

    echo  $? 

    echo "这个程序的PID号"

    echo $$

    执行结果:

    [root@panda scripts]# sh 9.sh `seq 9`

    1 2 3 4 5 6 7 8 9

    脚本名字

    example03.sh

    脚本参数个数

    the number of values is 9

    脚本参数列表

    1 2 3 4 5 6 7 8 9

    脚本参数列表

    1 2 3 4 5 6 7 8 9

    上一次执行结果的状态码

    0

    这个程序的PID号

    12869

    注意:$* 与 $@ 区别

    相同点:都是引用所有参数。

    不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)

    【例 10】

    [root@panda scripts]# cat 10.sh

    #!/bin/bash

    # author:awk

    echo "--- $* 演示 ---"

    for i in "$*"; do

        echo $i

    done

    echo "-- $@ 演示 ---"

    for i in "$@"; do

        echo $i

    done

    执行结果如下:

    [root@panda scripts]# sh 10.sh 1 2 3 4 5

    --- $* 演示 ---

    1 2 3 4 5

    -- $@ 演示 ---

    1

    2

    3

    4

    5

    【例11】分别取出路径和文件名

    分析:利用$0得到当前执行的路径分别进行输出

    脚本:

    [root@panda shell]# cat 11.sh

    echo $0

    dirname $0

    basename $0

    执行结果:

    [root@panda scripts]# sh /shell/0.sh

    / scripts/ example05.sh                      #执行的全路径

    / scripts                                             #执行的目录 

    example05.sh                                     #执行的文件名

    【例12】位置变量在服务启动与停止中的应用

    [root@panda scripts]# vim /etc/init.d/vsftpd

    case "$1" in                   #相当于执行/etc/init.d/vsftpd  $1

      start)

            start                

    #当选择start时 调用start函数来启动服务

            ;; 

      stop)

            stop                

    #当选择stop时 调用stop函数来停止服务

            ;;  

      restart|reload)            

     #当选择restart或者reload时 先停止服务 在启动服务

            stop              

            start             

            RETVAL=$?

            ;; 

    echo $"Usage: $start|stop|restart|try-restart|force-reload|status}"

    #当输入的$1不存在时 利用$0 来打印当前的执行脚本的方法

    补充:关于$?返回值的主要含义

    $0

    运行成功

    $2

    权限被拒绝

    $1--$125

    运行失败,命令错误或者参数错误

    $126

    找到命令,但是不能执行

    $127

    未找到改命令

    $128

    命令被系统强制结束

    第5章 小综合实例

    5.1 变量在shell中的使用

    [root@panda scripts]# cat zh.sh

    #!/bin/bash  

    var1="abcd efg"

    echo $var1

    var2=1234

    echo "The value of var2 is $var2"

    echo $HOME

    echo $PATH

    echo $PWD

    执行结果:

    [root@panda scripts]# ./zh.sh

    abcd efg

    The value of var2 is 1234

    /root

    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin

    /root/scripts

    5.2 特殊变量的测试

    [root@panda scripts]# cat ts.sh

    #!/bin/bash                         

    echo "$*”            

    echo "$#”                   

    touch /tmp/a.txt

    echo "$$”                 

    touch /tmp/b.txt &

    echo "$!”              

    echo "$$”   

    执行结果:

    [root@panda scripts]# ./ts.sh aaa bbb ccc

    aaa bbb ccc                  表示这个程序的所有参数

    3                              表示这个程序的参数个数

    3899                     表程序的进程ID

    3901                     执行上一个后台指令的PID

    3899                     表示当前程序的进程ID

    第6章 变量的数值计算

    用于执行简单的整数运算

    格式:$((算数运算符))

    ((表达式1,表达式2…))

    特点:

           1、在双括号结构中,所有表达式可以像c语言一样,如:a++,b--等。

           2、在双括号结构中,所有变量可以不加入:“$”符号前缀。

           3、双括号可以进行逻辑运算,四则运算

           4、双括号结构 扩展了for,while,if条件测试运算

           5、支持多个表达式运算,各个表达式之间用“,”分开

    常用的算数运算符

    运算符

       意义

    ++   --

    递增及递减,可前置也可以后置

    +  —  ! ~

    一元运算的正负号 逻辑与取反

    +  —  *  /   %

    加减乘除与余数

    <   <=   >   >=

    比较大小符号

    ==   !=

    相等 不相等

    >>  <<

    向左位移 向右位移

    & ^   |

    位的与 位的异或 位的或

    &&  ||

    逻辑与 逻辑或

    ? :

    条件判断

    【例13】三种简单的赋值运算

    [root@panda shell]# ((a=1+2**3-4%3))   #2**3为2的3次方

    [root@panda shell]# echo $a        

    8

    [root@panda shell]# b=$((1+2**3-4%3)) 

    [root@panda shell]# echo $b

    8

    [root@panda shell]# echo $((1+2**3-4%3)) 

    #省去了定义一个变量

    8

    【例14】递增递减的应用

    [root@panda shell]# echo $((a+=1))

    9

    [root@panda shell]# echo $((a++))    

     #等同于a+=1

    10

    [root@panda shell]# echo $((a--))  

    11

    [root@panda shell]# echo $a           

     #a++或a--为先赋值 再a加1或a减1

    10

    [root@panda shell]# echo $((--a))

    9

    [root@panda shell]# echo $a       

    #--a或++a为先a加1或a减1 再进行a的赋值

    9

    【例15】 比较大小

    [root@panda shell]# echo $((1>2))  

    0

    [root@panda shell]# echo $((1<=2)) 

     #真为1 假为0

    1

    【例16】1到100所有整数和

    [root@panda shell]# echo $((100*(1+100)/2))

    5050

    【例17】 写一个简单四则运算的脚本

    [root@panda shell]# cat 17.sh

    a=6

    b=2

    echo "a-b=$(($a-$b))"

    echo "a+b=$(($a+$b))"

    echo "a*b=$(($a*$b))"

    echo "a/b=$(($a/$b))"

    echo "a**b=$(($a**$b))"

    echo "a%b=$(($a%$b))"

    执行结果:

    [root@panda shell]# sh 17.sh

    a-b=4

    a+b=8

    a*b=12

    a/b=3

    a**b=36

    a%b=0

    升级后的脚本

    [root@panda shell]# cat 17-1.sh

    a=$1

    b=$2

    echo "a-b=$(($a-$b))"

    echo "a+b=$(($a+$b))"

    echo "a*b=$(($a*$b))"

    echo "a/b=$(($a/$b))"

    echo "a**b=$(($a**$b))"

    echo "a%b=$(($a%$b))"

    或者

    [root@panda shell]# cat 17-2.sh

    read -p "enter a:" a

    read -p "enter b:" b

    echo "a-b=$(($a-$b))"

    echo "a+b=$(($a+$b))"

    echo "a*b=$(($a*$b))"

    echo "a/b=$(($a/$b))"

    echo "a**b=$(($a**$b))"

    echo "a%b=$(($a%$b))"

    第7章 Read命令

    7.1 定义

    Read作用从键盘读入数据,赋给变量

    [root@xuegod67 scripts]# read a b c

    12 nihao 567

    [root@xuegod67 scripts]# echo $a

    12

    [root@xuegod67 scripts]# echo $b

    nihao

    [root@xuegod67 scripts]# echo $c

    567

    【例20】在shell中使用read命令

    [root@panda scripts]# cat 20.sh

    #!/bin/bash 

    echo "input first second third :"

    read  first second third

    echo "the first parameter is $first"

    echo "the second parameter is  $second"

    echo "the third parameter is $third"

    测试:

    [root@panda scripts]# sh 20.sh

    input first second third :

    aa 11 33

    the first parameter is aa

    the second parameter is  11

    the third parameter is 33

    7.2 参数用法

    q  read answer     从标准输入读取一行并赋值给变量answer

    q  read first last   从标准输入读取一行,直至遇到第一个空白符或换行符。把用户键入的第一个词存到变量first中,把该行的剩余部分保存到变量last中

    q  read –s passwd 将你输入的东西隐藏起来,值赋给passwd

    q  read –t 2 test 输入的时间限制

    q  read –n 2 test 输入的长度限制

    q  read –r line     允许输入包含反斜杠

    [root@panda ~]# read -p "请输入你的准考证号: " ID

    请输入你的准考证号: 20160308

    [root@panda ~]# echo $ID

    20160308

    【例21】

    [root@panda ~]# vim 21.sh

    #!/bin/bash

    read -p "请输入你的姓名:" NAME

    read -p "请输入你的年龄:" AGE

    read -p "请输入你的性别:" SEX

    echo "你的基本信息如下:"

    echo "姓名:$NAME"

    echo "年龄:$AGE"

    echo "性别:$SEX"

    执行结果如下:

    [root@xuegod67 scripts]# sh 21.sh

    请输入你的姓名:awk

    请输入你的年龄:10

    请输入你的性别:nan    

    你的基本信息如下:

    姓名:awk

    年龄:10

    性别:nan

  • 相关阅读:
    Linux下的tar压缩解压缩命令详解
    python学习笔记-day9-2【面向对象】
    python学习笔记-day9-1【发送邮件模块 yagmail】
    python学习笔记-day8-4-【python 内置函数补充:zip,map,filter】
    python学习笔记-day8-3-【python 网络请求及requests模块】
    python学习笔记-day8-2-【python 异常处理 try except】
    python学习笔记-day8-1-【python flask接口开发】
    day9-Python学习笔记(二十一)单元测试
    day9-Python学习笔记(二十)数据库备份,修改父类的方法
    day8-Python学习笔记(十八)面向对象,self,私有,属性方法
  • 原文地址:https://www.cnblogs.com/Devopser/p/6377907.html
Copyright © 2011-2022 走看看