Linux Shell脚本自动化编程实战开篇介绍

获取最新更新以及文章用到的软件包,请移步点击查看更新

一、Shell作用

二、C、Java、Python、Shell执行方式的对比

1、 程序语言执行

 2、Bash中调用python

 备注:

    -:可以支持下方输入内容使用tab键

2、当前shell和子shell

当前shell:使用.和source执行。

子shell:大部分的shell脚本常规都是子shell执行。

 三、Shell技术

四、linux支持的shell

1、查看linux系统支持的shell

[root@jenkins ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin

2、login shell和nologin shell区别

bash相关的文件:

[root@jenkins ~]# rpm -qc bash
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc

例子演示:

[root@jenkins ~]# usermod -s /bin/bash alice
[root@jenkins ~]# su alice
[alice@jenkins root]$ ls
ls: cannot open directory .: Permission denied
[alice@jenkins root]$ exit
exit
[root@jenkins ~]# su - alice
Last login: Sat Jan  8 02:23:03 EST 2022 on pts/0
[alice@jenkins ~]$ pwd
/home/alice
[alice@jenkins ~]$ exit
logout

图解:

 登录shell执行:

[root@jenkins ~]# ll /etc/profile
-rw-r--r--. 1 root root 1819 Apr 11  2018 /etc/profile
[root@jenkins ~]# ll /etc/bashrc 
-rw-r--r--. 1 root root 2853 Apr 11  2018 /etc/bashrc
[root@jenkins ~]# ll ~/.bash_profile 
-rw-r--r--. 1 root root 176 Dec 28  2013 /root/.bash_profile
[root@jenkins ~]# ll ~/.bashrc 
-rw-r--r--. 1 root root 176 Dec 28  2013 /root/.bashrc

离开shell执行:

[root@jenkins ~]# ll ~/.bash_logout 
-rw-r--r--. 1 root root 18 Dec 28  2013 /root/.bash_logout
[root@jenkins ~]# ll ~/.bash_history 
-rw-------. 1 root root 938 Jan  8 01:28 /root/.bash_history

五、GNU/bash shell特点

备注:

      1、vim 编辑某个文件,在视图模式下,按crtl+z ,暂停,退出编辑文件,fg回到编辑文件的模式

      2、!string中string会调出最近以string字符开的的命令

      3、linux中的小火车 sl命令,安装yum install -y sl

      4、>输出重定向,覆盖 ; >>输出重定向,追加 ;2>错误输出,覆盖;2>>错误输出,追加;2>&1错误2输出到1 ;&>混合输出

      5、screen命令

[root@jenkins ~]# yum install -y screen
[root@jenkins ~]# screen -S install_nginx       
[root@jenkins ~]# screen -list
There are screens on:
11078.install_nginx (Attached)
1311.install_nginx (Detached)
2 Sockets in /var/run/screen/S-root.

[root@jenkins ~]# screen 1311

   6、cat命令

[root@jenkins ~]# cat < /etc/hosts                    #没有加参数,使用的重定向
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.120 iregistry.baidu-int.com  vm.gitlab-local.com
[root@jenkins ~]# cat  /etc/hosts                     #加了一个参数
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.120 iregistry.baidu-int.com  vm.gitlab-local.com
[root@jenkins ~]# cat < /etc/hosts >/etc/hosts1       #重定向,相当于cp

   7、管道|tee

     

     备注:   

           tee:重定向内容,而不截流

六、命令排序

备注:

    ; :不具备逻辑判断

    &&:具备逻辑判断,只有当前一个命令执行成功,才会执行后面的命令

    ||:具备逻辑判断,上一个命令执行失败,才会执行后面的命令

 七、shell通配符(元字符)表示的不是本意                                        

八、颜色输出文本

      echo:用于字符串的输出   

      选项:

          -n 不换行输出

    -e 启用反斜线转义解释

    -E 禁用反斜线转义解释(默认)

echo -e "\e[1;36mThis is text.\e[0m"

e\1;36m   #打印颜色文本

字颜色:30-----------39 
30:黑 
31:红 
32:绿 
33:黄 
34:蓝色 
35:紫色 
36:深绿 
37:白色 

字背景颜色范围:40----49 
40:黑 
41:深红 
42:绿 
43:黄色 
44:蓝色 
45:紫色 
46:深绿 
47:白色 

\e[0m     #重置颜色
原文地址:https://www.cnblogs.com/aqicheng/p/15778276.html