快速入门SHELL

基础

1、基本命令

shell是一个命令解释器,它接收应用程序/用户命令,然后调用操作系统内核

脚本是为了方便的快速开发和运维的。

root@ubuntu:~# echo $BASH   # 需要大写
/bin/bash
root@ubuntu:~# echo $bash

root@ubuntu:~# df -h   # 查看系统分区
Filesystem      Size  Used Avail Use% Mounted on
udev            934M     0  934M   0% /dev
tmpfs           192M  2.8M  189M   2% /run
/dev/vda1        40G  5.2G   33G  14% /
tmpfs           956M     0  956M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           956M     0  956M   0% /sys/fs/cgroup
tmpfs           192M     0  192M   0% /run/user/0

root@ubuntu:~/03Shell# cat /etc/shells    # 查看系统中的解析器
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash

root@ubuntu:/bin# ll |grep bash
-rwxr-xr-x  1 root root 1113504 Jun  7  2019 bash*
lrwxrwxrwx  1 root root       4 Sep 14  2020 rbash -> bash*   # 软链接

root@ubuntu:/bin# echo $SHELL  默认的解析器,  需大写
/bin/bash

2、写一个输出姓名的shell
#!/bin/bash     	 # 以这个开头(指定解析器)
#FILENAME: 01_test.sh  # 下面三行都是注解
#auto echo NAME
#by author liu 2021
echo "liu"     # 输出语句


root@ubuntu:~/03Shell# chmod o+x ./01_test.sh   # 添加权限
root@ubuntu:~/03Shell# ./01_test.sh   # 执行
liu


root@ubuntu:~/03Shell# chmod o-x 01_test.sh 
root@ubuntu:~/03Shell# ll
total 12
drwxr-xr-x 2 root root 4096 Jun 21 10:42 ./
drwx------ 9 root root 4096 Jun 21 10:42 ../
-rw-r--r-- 1 root root   82 Jun 21 10:42 01_test.sh
root@ubuntu:~/03Shell# /bin/bash 01_test.sh # 可以直接用/bin/bash执行,这样就不用加执行权限
liu

第一种是脚本需要自己执行,所以需要权限,

第二种是bash解析器帮你执行脚本,所以本身不需要执行权限。

3、定义变量(自定义变量和系统变量)
  • 自定义变量(中间不能有空格

    • 定义变量: 变量=值(中间不能有空格
    • 撤销变量: unset 变量
    • 声明静态变量:readonly 变量, 注意: 不能unset

    注意:

    1、变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。

    2、等号两侧不能有空格

    3、在bash中,变量默认类型都是字符串类型,无法直接进行数值运算

    4、变量的值如果有空格,需要使用双引号或单引号括起来。

    5、可把变量提升为全局环境变量,可供其他shell程序使用

    export 变量

    root@ubuntu:/bin# A=1
    root@ubuntu:/bin# echo $A
    1
    root@ubuntu:/bin# unset A
    root@ubuntu:/bin# echo $A
    
    root@ubuntu:/bin# readonly B=1
    root@ubuntu:/bin# echo $B
    1
    root@ubuntu:/bin# unset B
    -bash: unset: B: cannot unset: readonly variable
    
    
    
    root@ubuntu:/bin# C=1+1
    root@ubuntu:/bin# echo C
    C
    root@ubuntu:/bin# echo $C
    1+1
    
    
    root@ubuntu:/bin# S=i love you
    
    Command 'love' not found, but can be installed with:
    
    root@ubuntu:/bin# S="i love you"   # 中间有空格的,需要加上引号
    
    
    
  • 系统变量(不需要定义,直接使用)

    • $0 : 当前程序名称

    • $n: 当前程序的第n个参数 n = 1,2,9,十个以上的参数需要使用大括号如 ${10}

    • $* : 当前程序的所有参数(不包括程序本身)(把所有参数看成一个整体)

    • $@: 代表命令行中所有参数(不包括程序本身)(把每个参数区分对待)

    • $# : 当前程序的参数个数(不包括程序本身)(常用于循环)

    • $?: 命令或程序执行完后的状态,一般返回0表示执行成功, 判断上一条命令是否成功。如果是非0(具体什么数,于命令相关)

    • $UID: 当前用户的ID

    • $PWD: 当前所在的目录

    • $HOME: 自己用户的/home目录

    • $SHELL: 查看默认的解析器

    • $USER: 查看当前的用户

    测试脚本

    #!/bin/bash
    #define a var
    # by liu 
    
    A=3
    echo "A = $A"
    name="liu"
    echo "my name is $name"
    
    echo ================
    echo $0
    echo $1
    echo $#
    echo =======
    
    echo $UID
    echo $PWD
    
    ########################################################
    执行过程
    root@ubuntu:~/03Shell# ./02test.sh 
    A = 3
    my name is liu
    ================
    ./02test.sh
    
    0
    =======
    0
    /root/03Shell
    root@ubuntu:~/03Shell# ./02test.sh 1 name 2 age
    A = 3
    my name is liu
    ================
    ./02test.sh
    1
    4
    =======
    0
    /root/03Shell
    
4、运算符

1、基本语法

$((运算式)) 或 $[运算式]

expr + - \* / % 加 减 乘 除 取余

注意: expr 运算符间要有空格, 乘是\*

root@ubuntu:/bin# expr 2 +5  # 语法错误
expr: syntax error

root@ubuntu:/bin# expr 2 + 5
7
root@ubuntu:/bin# expr 2 \* 5
10

root@ubuntu:/bin# expr `expr 2 + 3` \* 2    # 计算 (2 + 3) * 2
10
也可以按照下面来写
root@ubuntu:/bin# s=$[(2+3)*4]
root@ubuntu:/bin# echo $s
20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值