bash shell文件

142 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

五 影响bash shell的文件

系统环境变量配置文件:(系统定义的)

  • 1、/etc/profile
  • 2、/etc/bashrc
  • 3、~/.bashrc
  • 4、~/.bash_profile

上述四个文件在登录shell即su - egon时的执行顺序是

login shell:        
    1. /etc/profile         系统级别配置文件
    2. ~/.bash_profile      用户级别配置文件 (用户自定义的环境变量) 
    3. ~/.bashrc            用户级 (定义别名)
    4. /etc/bashrc          系统级

在非登录shell即su egon时,执行的文件只有两个,顺序为

non-login shell: 
    1. ~/.bashrc            用户级
    2. /etc/bashrc          系统级

Ps:

1、vim /home/user1/.bash_history -------每次登陆用户的操作都记录其中
2、su - user1进行操作然后退出,查看该文件内容
3、vim /home/user1/.bash_logout --------添加一条  
4、>/home/user1/.bash_history这样每次退出用户后都会自动清空~/.bash_history中的内容  

六 元字符

shell语法中的特殊字符

  • 1、 与$():取命令的结果\ [root@localhost ~]# echo `pwd`\ /root\ [root@localhost ~]# echo $(pwd)\ /root\ ​\ 不一样的地方在于$()可以嵌套,而不能嵌套
    [root@localhost ~]# echo (ls(ls (pwd))
  • 2、~家目录
  • 3、.与..
  • 4、!取反
    [root@localhost ~]# touch /test/{1.txt,2.txt,a.txt,aaa_bbb.txt}
    [root@localhost ~]# find /test ! -name 1.txt
    /test
    /test/2.txt
    /test/a.txt
    /test/aaa_bbb.txt


    [root@localhost ~]# ls /test/[!0-9].txt # .txt前只有一个字符,但是非数字
    /test/a.txt
    [root@localhost ~]# ls /test/[^0-9].txt # .txt前只有一个字符,但是非数字
    /test/a.txt
  • 5、@无特殊意义
  • 6、#注释
  • 4、取变量值\ [root@localhost ~]# x=1\ [root@localhost ~]# echo x
    1
  • 5、%、-、+运算符\

    数学运算\

    1、bc是比较常用的linux计算工具了,而且支持浮点运算:\

    [root@localhost ~]# res=echo 1+1 | bc
    [root@localhost ~]# echo res\ 2\ ​\ [root@localhost ~]# res=`echo 10 % 3 | bc`\ [root@localhost ~]# echo res
    1

    [root@localhost ~]# res=echo 1.2+1.3|bc
    [root@localhost ~]# echo res\ 2.5\ ​\ [root@localhost ~]# res=`echo 5.0+3.0|bc`\ [root@localhost ~]# echo res
    8.0

    [root@localhost ~]# res=echo "scale=2;5.0/3.0"|bc
    [root@localhost ~]# echo res\ 1.66\ ​\ [root@localhost ~]# res=`echo "scale=2;5.0/6.0"|bc`\ [root@localhost ~]# echo res
    .83
    ​\

    2、expr不支持浮点数计算。而且要注意数字与运算符中的空格\

    [root@localhost ~]# res=expr 5 / 3 # 不支持浮点计算
    [root@localhost ~]# echo res\ 1\ ​\ [root@localhost ~]# res=`expr 1+1` # 注意:要有空格\ [root@localhost ~]# echo res
    1+1
    [root@localhost ~]# res=expr 1 + 1
    [root@localhost ~]# echo $res
    2
    ​\

    3、$(()) 同expr,不支持浮点数运算\

    [root@localhost ~]# echo ((1+1))\ 2\ [root@localhost ~]# echo ((1.0+2.0))
    -bash: 1.0+2.0: 语法错误: 无效的算术运算符 (错误符号是 ".0+2.0")


    #4、let 不支持浮点数运算,而且不支持直接输出,只能赋值
    [root@localhost ~]# let res=1+1
    [root@localhost ~]# echo res\ 2\ [root@localhost ~]#\ [root@localhost ~]# let res=50/5\ [root@localhost ~]# echo res
    10
    [root@localhost ~]# let c=1.33
    -bash: let: c=1.3
    3: 语法错误: 无效的算术运算符 (错误符号是 ".3*3"
  • 6、^同!一样
  • 7、*任意多个字符
    [root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt
    [root@localhost ~]# rm -rf *.txt
    [root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt a1c.txt
    [root@localhost ~]# ls *.txt
    1.txt 2.txt a1c.txt aaa.txt aa.txt
  • 8、()在子shell中执行
    [root@localhost ~]# (x=1)
    [root@localhost ~]# echo $x

    应用
    [root@localhost ~]# (umask 066;touch a.txt) # umask的设置只在子shell中有效
    [root@localhost ~]# ll a.txt
    -rw-------. 1 root root 0 8月 13 15:22 a.txt
    [root@localhost ~]# touch b.txt
    [root@localhost ~]# ll b.txt
    -rw-r--r--. 1 root root 0 8月 13 15:23 b.txt
  • 9、_下划线:无特殊意义,可以用于名字的声明
  • 10、=赋值,判断相等性
    [root@localhost ~]# [ 1 = 1 ] # 条件1 = 1的左右两边必须有空格
    [root@localhost ~]# echo $? # 判断上一条命令的结果是否为真,0=》true
    0
  • 11、|管道:把一个进程的处理结果传递给另外一个进程
    [root@localhost ~]# ps aux | grep python


    xargs参数传递,把上一个命令的结果作为下一个命令的参数
    [root@localhost ~]# find /home/ -type d -name "test*" |xargs ls
    1.txt 2.txt 3.txt
    [root@localhost ~]# ls /home/test
    1.txt 2.txt 3.txt
  • 12、\转义特殊字符
    [root@localhost ~]# mkdir a\ b.txt # 虽然可以,但不推荐
    [root@localhost ~]# ll
    总用量 0
    drwxr-xr-x. 2 root root 6 8月 13 15:35 a b.txt


    [root@localhost ~]# echo RMB # 默认会当成变量\ ​\ [root@localhost ~]# echo 'RMB' # 取消特殊意义
    RMB\ [root@localhost ~]# echo \RMB # 取消特殊意义
    $RMB
  • 13、[]条件测试,后续会详细介绍
    [root@localhost ~]# name="egon"
    [root@localhost ~]# [ name="egon"];echoname = "egon" ];echo ?
    0
    [root@localhost ~]# name="adf"
    [root@localhost ~]# [ name="egon"];echoname = "egon" ];echo ?
    1

    [root@localhost ~]# [ -d /test ];echo $?
  • 14、引号
    '' 强引用(在单引号中都视为普通字符)
    " " 弱引用 (在双引号中保留变量)

    [root@localhost ~]# x=111
    [root@localhost ~]# echo "x"\ 111\ [root@localhost ~]# echo 'x'
    $x
    [roo
  • 15、;与&&与||连接多条命令
    [root@localhost home]# gagaga;ls # 不论前一条命令运行成功与否,都会执行后续命令
    bash: gagaga: 未找到命令...
    egon
    [root@localhost home]# gagaga && ls # 只有前一条命令执行成功,才会执行后续命令
    bash: gagaga: 未找到命令...

    [root@localhost home]# ls /test || mkdir /test # 前一条命令执行不成功才会执行后续命令
    0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
  • 16、/路径分隔符
  • 17、{}包含
    [root@localhost home]# touch /test/{0..9}.txt
    [root@localhost home]# ls /test/
    0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
  • 18、&后台运行
    [root@localhost home]# echo "hello";sleep 3;echo "world" &
  • 19、重定向\

    输出重定向
    < << 输入重定向

    覆盖 >> 追加
    [root@localhost home]# cat >> a.txt << EOF
    111
    222
    333
    EOF

    0标准输入、1标准正确输出、2标准错误输出,&标准正确和错误输出
    [root@localhost home]# pwd 1>a.txt
    [root@localhost home]# cat a.txt
    /home
    [root@localhost home]# gagag 2>a.txt
    [root@localhost home]# cat a.txt
    bash: gagag: 未找到命令...
    [root@localhost home]# gagaga &>/dev/null

    < << 输入重定向
    [root@localhost ~]# mysql -uroot -p123 < bbs.sql
    [root@localhost home]# grep root < /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin


    [root@localhost home]# dd if=/dev/zero of=/a.txt bs=1M count=10
    记录了10+0 的读入
    记录了10+0 的写出
    10485760字节(10 MB)已复制,0.024387 秒,430 MB/秒
    [root@localhost home]# dd </dev/zero >/b.txt bs=1M count=10
    记录了10+0 的读入
    记录了10+0 的写出
    10485760字节(10 MB)已复制,0.0202365 秒,518 MB/秒

    [root@localhost home]# ll /a.txt
    -rw-r--r--. 1 root root 10485760 8月 13 16:02 /a.txt
    [root@localhost home]# ll /b.txt
    -rw-r--r--. 1 root root 10485760 8月 13 16:03 /b.txt

  • 20、?任意一个字符
    [root@localhost ~]# ls ??.txt
    aa.txt
    [root@localhost ~]# ls a?c.txt
    a1c.txt
    [root@localhost ~]# rm -rf *.txt
  • 21、范围中的任意一个字符 [12] [ac] [a-z] [0-9]
    [root@localhost ~]# touch a1c a2c axc aXc axd
    [root@localhost ~]# ls a?c
    a1c a2c axc aXc
    [root@localhost ~]# ls a[1x]c
    a1c axc
    [root@localhost ~]# ls a[a-z]c
    axc aXc
    [root@localhost ~]# ls a[A-Z]c # 不区分大小写
    axc aXc
    [root@localhost ~]# ls a[x]c
    axc
    [root@localhost ~]# ls a[X]c
    aXc
    [root@localhost ~]# ls a[0-9]c
    a1c a2c
    [root@localhost ~]# ls /dev/sd[a-z]*
    /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb1