一、Linux高级配置
-
配置静态IP
-
配置文件地址:/etc/sysconfig/network-scripts/ifcfg-ens33
-
修改内容:
-
BOOTPROTO值由“dhcp”修改为:static
-
增加:【值的设置参考VMware虚拟网络编辑器内容】
- IPADDR
- GATEWAY
- DNS1
-
-
-
设置主机名
-
临时设置:立即生效
- 命令:hostname 主机名
[root@localhost ~]# hostname bigdata01 [root@localhost ~]# hostname bigdata01
-
永久设置:重启后生效
- 修改配置文件:/etc/hostname
-
-
关闭防火墙
-
临时关闭
-
命令:systemctl stop firewalld
[root@bigdata01 ~]# systemctl stop firewalld [root@bigdata01 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) since Wed 2022-02-23 23:25:08 CST; 8s ago Docs: man:firewalld(1) Process: 710 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS) Main PID: 710 (code=exited, status=0/SUCCESS) Feb 23 23:14:53 bigdata01 systemd[1]: Starting firewalld - dynamic firewall daemon... Feb 23 23:14:54 bigdata01 systemd[1]: Started firewalld - dynamic firewall daemon. Feb 23 23:25:08 bigdata01 systemd[1]: Stopping firewalld - dynamic firewall daemon... Feb 23 23:25:08 bigdata01 systemd[1]: Stopped firewalld - dynamic firewall daemon.
-
-
永久关闭
-
命令:systemctl disable firewalld
[root@bigdata01 ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. =========================================查看开机启动项=================================================== [root@bigdata01 ~]# systemctl list-unit-files | grep firewalld firewalld.service disabled
-
-
二、shell编程
-
基本规则:
-
扩展名:建议以.sh为扩展名
-
脚本第一行内容为:#!/bin/bash
-
除第一行外,其他以#开头的行为注释
-
执行命令:
- bash filename 或 sh filename:可直接执行shell脚本,无需为其添加执行权限【原因:将脚本作为参数传入bash或sh命令】
- 路径/filename:需为脚本添加执行权限后才能执行
- filename:需将当前路径添加至环境变量才能执行【也需添加执行权限】
====================创建目录存放shell脚本=================================== [root@bigdata01 ~]# mkdir shell [root@bigdata01 ~]# cd shell ====================创建hello.sh脚本================================= [root@bigdata01 shell]# vi hello.sh ====================脚本内容============================== #!/bin/bash #first command echo "hello world!" ~ ====================脚本结束============================== ====================使用bash命令执行脚本============================== [root@bigdata01 shell]# bash hello.sh hello world! ====================使用sh命令执行脚本============================== [root@bigdata01 shell]# sh hello.sh hello world! ====================直接使用(路径/filename或filename)命令执行脚本报错============================== [root@bigdata01 shell]# ./hello.sh -bash: ./hello.sh: Permission denied [root@bigdata01 shell]# hello.sh -bash: ./hello.sh: Permission denied ====================添加执行权限============================== [root@bigdata01 shell]# chmod u+x hello.sh ====================使用(路径/filename)命令执行脚本============================= [root@bigdata01 shell]# ./hello.sh hello world! ====================使用(filename)命令执行脚本报错============================= [root@bigdata01 shell]# hello.sh -bash: hello.sh: command not found ====================查看环境变量============================= [root@bigdata01 shell]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin ====================修改环境变量============================= [root@bigdata01 shell]# vi /etc/profile =============在profile文件末尾添加以下内容【将当前路径添加至环境变量】==================== export PATH=.:$PATH =============重新加载profile文件【重要!重新加载后才能生效】==================== [root@bigdata01 shell]# source /etc/profile ====================查看修改后环境变量============================= [root@bigdata01 shell]# echo $PATH .:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin ====================重新使用(filename)命令执行脚本============================= [root@bigdata01 shell]# hello.sh hello world!
-
-
变量
-
变量基础
- shell中的变量不需要提前声明,初始化时不需要指定类型
- 变量的命名以字母、数字和下划线组成,不能以数字开头
- 变量赋值用“=”,变量名、=和变量值之间不能有空格
- 变量值的打印:
- echo ${变量名}【字符串拼接打印直接在{}后写要拼接的字符串即可】
- echo $变量名【不能直接进行字符串拼接】
-
四种变量
-
本地变量
- 变量形式:var_name=value
- 作用范围:当前进程中有效,在其他进程或当前进程的子进程中无效
=======================================当前进程======================================================= [root@bigdata01 shell]# name=kpstu [root@bigdata01 shell]# echo ${name} kpstu =======================================当前进程的子进程======================================================= ==================================安装pstree命令查看进程树=========================================== [root@bigdata01 shell]# yum -y install psmisc .......... .......... Installed: psmisc.x86_64 0:22.20-17.el7 Complete! ==================================使用pstree命令查看进程树=========================================== [root@bigdata01 shell]# pstree systemd─┬─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─auditd───{auditd} ├─crond ├─dbus-daemon───{dbus-daemon} ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─polkitd───6*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] *********************************** ├─sshd───sshd───bash───pstree *********************************** ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] └─vmtoolsd ==================================进入子进程并查看进程数=========================================== [root@bigdata01 shell]# bash [root@bigdata01 shell]# pstree systemd─┬─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─auditd───{auditd} ├─crond ├─dbus-daemon───{dbus-daemon} ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─polkitd───6*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] *************************************** ├─sshd───sshd───bash───bash───pstree *************************************** ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] └─vmtoolsd ==================================子进程中无法获取${name}=========================================== [root@bigdata01 shell]# echo ${name}meiyou meiyou ==================================退出子进程=========================================== [root@bigdata01 shell]# exit exit [root@bigdata01 shell]# echo ${name}you kpstuyou =========================================其他进程【克隆会话即开启其他进程】============================================== [root@bigdata01 ~]# echo ${name}meiyou meiyou
-
环境变量
- 变量形式:export var_name=value(临时环境变量)
- 作用范围:当前进程及其子进程中有效,其他进程中无效
[root@bigdata01 shell]# export age=18 =========================================当前进程============================================== [root@bigdata01 shell]# echo ${age} 18 =====================================当前进程的子进程=========================================== [root@bigdata01 shell]# bash [root@bigdata01 shell]# echo ${age} 18 =========================================其他进程==================================== [root@bigdata01 ~]# echo ${age}meiyou meiyou
-
位置变量
- 变量形式:$0,$1,……
- 使用场景:当一个脚本需要传入参数是,使用位置变量进行参数的获取,其中$0获取当前脚本名称,$1,……获取输入变量
[root@bigdata01 shell]# vi location.sh #!/bin/bash #位置变量 echo $0 echo $1 echo $2 echo $3 ~ [root@bigdata01 shell]# bash location.sh kpstu amber chris location.sh kpstu amber chris
-
特殊变量
-
$?:上一条命令执行后返回的状态码【取值范围为:0~255】
- 命令执行成功返回0
- 命令执行失败根据其失败原因返回对应状态,范围为:1~255
[root@bigdata01 shell]# ll total 8 -rwxr--r--. 1 root root 47 Feb 24 00:02 hello.sh -rw-r--r--. 1 root root 58 Feb 24 01:26 location.sh [root@bigdata01 shell]# echo $? 0 [root@bigdata01 shell]# lk bash: lk: command not found [root@bigdata01 shell]# echo $? 127
-
$#:输入shell脚本参数的个数
[root@bigdata01 shell]# cat paramnum.sh #!/bin/bash #输入参数个数 echo $# [root@bigdata01 shell]# bash paramnum.sh amber kpstu 2
-
-
-
引号和变量的特殊使用
-
''单引号:不解析变量
-
“”双引号:解析变量
-
``反引号:解析变量并将其值作为命令使用,与$()效果相同
-
‘ “ ” ‘外层单引号,内层双引号:不解析变量并给其增加双引号
-
" ’ ’ "外层双引号,内层单引号:解析变量并给其增加单引号
[root@bigdata01 shell]# name=kpstu [root@bigdata01 shell]# echo '${name}' ${name} [root@bigdata01 shell]# echo "${name}" kpstu [root@bigdata01 shell]# `${name}` bash: kpstu: command not found [root@bigdata01 shell]# $(${name}) bash: kpstu: command not found [root@bigdata01 shell]# name=pwd [root@bigdata01 shell]# `${name}` bash: /root/shell: Is a directory [root@bigdata01 shell]# echo `${name}` /root/shell [root@bigdata01 shell]# echo $(${name}) /root/shell [root@bigdata01 shell]# echo '"${name}"' "${name}" [root@bigdata01 shell]# echo "'${name}'" 'pwd'
-
-
-
for循环
-
语法格式
===========1========== for((i=0;i<10;i++)) do 循环体 done ===========2========== for i in 1 2 3 4 5 do 循环体 done
-
示例
===========1========== [root@bigdata01 shell]# vi for1.sh #!/bin/bash for((i=0;i<10;i++)) do echo $i done ~ "for1.sh" 5L, 48C written [root@bigdata01 shell]# bash for1.sh 0 1 2 3 4 5 6 7 8 9 ===========2========== [root@bigdata01 shell]# vi for2.sh #!/bin/bash for i in 1 2 3 4 5 do echo $i done ~ "for2.sh" [New] 5L, 47C written [root@bigdata01 shell]# sh for2.sh 1 2 3 4 5
-
-
while循环
-
语法格式:
while 测试条件 do 循环体 done
-
测试条件格式:test EXPR 或 [ EXPR ]【使用第二种格式时,中括号与表达式中间必须有空格】
-
表达式
- 整型数值比较:-gt(大于)、-lt(小于)、-ge(大于等于)、-le(小于等于)、-eq(等于)、-ne(不等于)
- 字符串比较:=(等于)、!=(不等于)
-
示例
[root@bigdata01 shell]# vi while1.sh #!/bin/bash while test 2 -gt 1 do echo yes sleep 1 done ~ "while1.sh" [New] 6L, 56C written [root@bigdata01 shell]# sh while1.sh yes yes yes yes ^C [root@bigdata01 shell]# cp while1.sh while2.sh [root@bigdata01 shell]# vi while2.sh #!/bin/bash while [ 2 -gt 1 ] do echo yes sleep 1 done ~ "while2.sh" 6L, 55C written [root@bigdata01 shell]# sh while2.sh yes yes yes ^C ==========================字符串========================== [root@bigdata01 shell]# cp while2.sh while3.sh [root@bigdata01 shell]# vi while3.sh #!/bin/bash while [ "abc" = "abc" ] do echo yes sleep 1 done ~ "while3.sh" 6L, 61C written [root@bigdata01 shell]# bash while3.sh yes yes yes ^C [root@bigdata01 shell]# vi while3.sh #!/bin/bash while [ "abc" != "abc" ] do echo yes sleep 1 done ~ "while3.sh" 6L, 62C written [root@bigdata01 shell]# bash while3.sh //无输出
-
-
if判断
-
语法格式
================单分支================ if 测试条件 then 选择分支 fi ================双分支================ if 测试条件 then 选择分支 else 选择分支 fi ================多分支================ if 测试条件1 then 选择分支1 elif 测试条件2 then 选择分支2 ...... else 选择分支n fi
-
示例
================单分支================ [root@bigdata01 shell]# vi if1.sh #!/bin/bash flag=$1 if [ $flag -eq 1 ] then echo one fi ~ "if1.sh" 6L, 60C written [root@bigdata01 shell]# sh if1.sh 1 one [root@bigdata01 shell]# sh if1.sh 2 [root@bigdata01 shell]# sh if1.sh if1.sh: line 3: [: -eq: unary operator expected ================双分支================ [root@bigdata01 shell]# vi if2.sh #!/bin/bash if [ $# -lt 1 ] then echo "no param" exit 1 fi flag=$1 if [ $flag -eq 1 ] then echo one else echo "not support" fi ~ "if2.sh" 14L, 144C written [root@bigdata01 shell]# sh if2.sh no param [root@bigdata01 shell]# echo $? 1 [root@bigdata01 shell]# sh if2.sh 2 not support [root@bigdata01 shell]# sh if2.sh 1 one ================多分支================ [root@bigdata01 shell]# vi if3.sh #!/bin/bash if [ $# -lt 1 ] then echo "no param" exit 1 fi flag=$1 if [ $flag -eq 1 ] then echo one elif [ $flag -eq 2 ] then echo two else echo "not support" fi ~ "if3.sh" 17L, 183C written [root@bigdata01 shell]# bash if3.sh no param [root@bigdata01 shell]# bash if3.sh 1 one [root@bigdata01 shell]# bash if3.sh 2 two [root@bigdata01 shell]# bash if3.sh 3 not support
-
-
shell扩展
-
shell脚本后台运行:在执行命令最后添加&
- 这种情况下,关闭会话窗口,则脚本停止运行,若需其一直运行,则需在执行命令前添加nohup,这是脚本运行日志将会存储在当前目录下的nohup.out文件中
-
标准输出:命令的正常信息,使用文件描述符1表示
-
标准错误输出:命令的错误信息,使用文件描述符2表示
-
重定向:【文件描述符与>|>>之间不能有空格】【文件描述符为1时可以省略文件描述符不写】
- 文件描述符>文件名:覆盖写入
- 文件描述符>>文件名:追加写入
-
案例
nohup bash while2.sh >/dev/null 2>&1 &
- nohup和&:使脚本一直在后台运行
>/dev/null
:将标准输出重定向到/dev/null中【/dev/null是一个特殊路径,任何数据放到该目录都不能找到】2>&1
:将标准错误输出重定向到标准输出中【重定向符号后可以直接跟文件名,若跟文件描述符则需在文件描述符前增加&符号】
-
-
crontab
-
功能:可以作用于周期性被执行的命令
-
格式:* * * * * username command
- 第一个*:分钟(0~59)
- 第二个*:小时(0~23)
- 第三个*:日(1~31)
- 第四个*:月(1~12)
- 第五个:星期(0~6)【0表示星期天】
- username:用户名
- command:定时执行的命令
-
使用
- 这条配置需要添加到crontab服务对应的文件中,在配置之前,需要先确认crontab的服务是否正常
- 确认crontab服务是否正常:
systemctl status crond
- 写入配置文件,文件路径:
/etc/crontab
-
示例:每分钟打印一次时间,时间格式为:年-月-日 时:分:秒
=========showTime脚本========== [root@bigdata01 shell]# cat showTime.sh #!/bin/bash showTime=`date +"%Y-%m-%d %H:%M:%S"` echo $showTime [root@bigdata01 shell]# sh showTime.sh 2022-02-24 05:06:15 ==========修改contab配置文件============ 在文件末尾增加一行:* * * * * root sh /root/shell/showTime.sh >/root/shell/showTime.log ==========监控showTime.log文件======== [root@bigdata01 shell]# tail -f showTime.log 2022-02-24 05:11:01 ^C
【注:对于没有输出信息的定时任务,可通过查看crontab日志文件判断是否执行,日志文件路径:
/var/log/cron
】
-