diff --git a/shell-MD/shell变量.md b/shell-MD/shell变量.md new file mode 100644 index 0000000..55fa872 --- /dev/null +++ b/shell-MD/shell变量.md @@ -0,0 +1,410 @@ +

Shell变量

+ +作者:行癫(盗版必究) + +------ + +## 一:变量概述 + +#### 1.什么是变量 + +​ 变量来源于数学,是计算机语言中能储存计算结果或能表示值的抽象概念 + +​ 变量可以通过变量名访问,在指令式语言中,变量通常是可变的;在某些条件下也是不可变的 + +#### 2.变量的规则 + +​ 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头 + +​ 中间不能有空格,可以使用下划线 + +​ 不能使用标点符号 + +​ 不能使用bash里的关键字 + +## 二:变量分类 + +#### 1.自定义变量 + +​ 定义变量:变量名=变量值 例如:xingdian=123 + +​ 引用变量:$变量名 或 ${变量名} + +​ 查看变量:echo $变量名 + +​ 取消变量:unset 变量名 + +​ 作用范围:仅在当前shell中有效 + +#### 2.环境变量 + +​ 定义环境变量: + +​ 方法一 export back_dir2=/home/backup + +​ 方法二 export back_dir1 将自定义变量转换成环境变量 + +​ 引用环境变量:$变量名 或 ${变量名} + +​ 查看环境变量:echo $变量名 + +​ 取消环境变量:unset 变量名 + +​ 变量作用范围:在当前shell和子shell有效 + +注意: + +​ 环境变量拥有可继承性:export之后就拥有继承性 + +​ 永久生效:写到环境变量脚本,/etc/profile ~/.baserc ~/.bash_profile /etc/bashrc + +案例: + +```shell +[root@xingdiancloud ~]# vim /etc/profile +JAVA_HOME=/usr/local/java +PATH=$JAVA_HOME/bin:$PATH +export JAVA_HOME PATH +``` + +```shell +[root@xingdiancloud ~]# vim ~/.bash_profile (只显示部分) +PATH=$PATH:$HOME/bin:/usr/local/mycat/bin +``` + +/etc/profile + +​ 这是系统最主要的shell设置文件,也是用户登陆时系统最先检查的文件,有关重要的环境变量都定义在此,其中包括PATH,USER,LOGNAME,MAIL,HOSTNAME,HISTSIZE,INPUTRC等。而在文件的最后,它会检查并执行/etc/profile.d/*.sh的脚本 + +~/.bash_profile + +​ 这个文件是每位用户的bash环境设置文件,它存在与于用户的主目录中,当系统执行/etc/profile 后,就会接着读取此文件内的设置值。在此文件中会定义USERNAME,BASH_ENV和PATH等环境变量,但是此处PATH除了包含系统的$PATH变量外加入用户的“bin”目录路径 + +~/.bashrc + +​ 接下来系统会检查~.bashrc文件,这个文件和前两个文件(/etc/profile 和~.bash_profile)最大的不同是,每次执行bash时,~.bashrc都会被再次读取,也就是变量会再次地设置,而/etc/profile,~./bash_profile只有在登陆时才读取。就是因为要经常的读取,所以~/.bashrc文件只定义一些终端机设置以及shell提示符号等功能,而不是定义环境变量 + +~/.bash_login + +​ 如果~/.bash_profile文件不存在,则系统会转而读取~/.bash_login这个文件内容。这是用户的登陆文件,在每次用户登陆系统时,bash都会读此内容,所以通常都会将登陆后必须执行的命令放在这个文件中 + +.profile + +​ 如果~./bash_profile ~./bash_login两个文件都不存在,则会使用这个文件的设置内容,其实它的功能与~/.bash_profile相同 + +.bash_logout + +​ 如果想在注销shell前执行一些工作,都可以在此文件中设置 + +```shell +[root@xingdiancloud ~]# vi ~.bash_logout +clear +仅执行一个clear命令在你注销的时候 +``` + +~/.bash_history + +​ 这个文件会记录用户先前使用的历史命令 + +注意: + +​ 在/etc/profile.d建立独立的环境变量配置文件 + +​ 常用环境变量:USER UID HOME HOSTNAME PWD PATH + +​ PATH:这个变量存放的是所有命令所在的路径 修改:PATH=$PATH:+目录 + +#### 3.位置变量 + +​ $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 + +案例: + +```shell +[root@xingdiancloud sh]# cat xingdian.sh +#!/bin/bash +echo "hello $1" +[root@xingdiancloud sh]# bash xingdian.sh xingdian +hello xingdian +``` + +4.预定义变量 + +```shell +$0 脚本名 +$* 所有的参数 +$@ 所有的参数 +$# 参数的个数 +$$ 当前进程的PID +$! 上一个后台进程的PID +$? 上一个命令的返回值 0表示成功 +``` + +案例: + +```shell +[root@xingdiancloud sh]# cat test.sh +#!/bin/bash +echo "第2个位置参数是$2" +echo "第1个位置参数是$1" +echo "第4个位置参数是$4" + +echo "所有参数是: $*" +echo "所有参数是: $@" +echo "参数的个数是: $#" +echo "当前进程的PID是: $$" + +echo '$1='$1 +echo '$2='$2 +echo '$3='$3 +echo '$*='$* +echo '$@='$@ +echo '$#='$# +echo '$$='$$ +``` + +## 三:变量赋值 + +#### 1.显示赋值 + +​ 变量名=变量值 + +示例: + +```shell +[root@xingdiancloud ~]# ip1=192.168.1.251 +[root@xingdiancloud ~]# school="BeiJing 1000phone" +[root@xingdiancloud ~]# today1=`date +%F` +[root@xingdiancloud ~]# today2=$(date +%F) +``` + +#### 2.键盘读入 + +```shell +read 变量名 +read -p "提示信息: " 变量名 +read -t 5 -p "提示信息: " 变量名 -t 后面跟秒数,定义输入字符的等待时间 +read -n 2 变量名 -n 后跟一个数字,定义输入文本的长度,很实用。 +``` + +案例1: + +```shell +[root@xingdiancloud ~]# vim first.sh +back_dir1=/var/backup +read -p "请输入你的备份目录: " back_dir2 +echo $back_dir1 +echo $back_dir2 +[root@xingdiancloud ~]# sh first.sh +``` + +案例2: + +```shell +[root@xingdiancloud ~]# vim ping2.sh +#!/bin/bash +read -p "Input IP: " ip +ping -c2 $ip &>/dev/null +if [ $? = 0 ];then + echo "host $ip is ok" +else + echo "host $ip is fail" +fi +[root@xingdiancloud ~]# chmod a+x ping2.sh +[root@xingdiancloud ~]# ./ping.sh +``` + +注意:定义或引用变量时注意事项 + +​ " " 弱引用 可以实现变量和命令的替换 + +​ ' ' 强引用 不完成变量替换 + +​ 反引 命令替换 等价于 $() 反引号中的shell命令会被先执行 + +```shell +[root@xingdiancloud ~]# school=1000phone +[root@xingdiancloud ~]# echo "${school} is good" +1000phone is good +[root@xingdiancloud ~]# echo '${school} is good' +${school} is good +[root@xingdiancloud ~]# touch `date +%F`_file1.txt +[root@xingdiancloud ~]# touch $(date +%F)_file2.txt +[root@xingdiancloud ~]# disk_free3="df -Ph |grep '/$' |awk '{print $4}'" 错误 +[root@xingdiancloud ~]# disk_free4=$(df -Ph |grep '/$' |awk '{print $4}') +[root@xingdiancloud ~]# disk_free5=`df -Ph |grep '/$' |awk '{print $4} +``` + +## 四:变量运算 + +#### 1.整数运算 + +方法一:expr + +```shell +[root@xingdiancloud ~]# expr 1 + 2 +[root@xingdiancloud ~]# expr $num1 + $num2 + - \* / % +``` + +方法二:$(()) + +```shell +[root@xingdiancloud ~]# echo $(($num1+$num2)) + - * / % +[root@xingdiancloud ~]# echo $((num1+num2)) +[root@xingdiancloud ~]# echo $((5-3*2)) +[root@xingdiancloud ~]# echo $(((5-3)*2)) +[root@xingdiancloud ~]# echo $((2**3)) +[root@xingdiancloud ~]# sum=$((1+2)); echo $sum +``` + +方法三:$[] + +```shell +[root@xingdiancloud ~]# echo $[5+2] + - * / % +[root@xingdiancloud ~]# echo $[5**2] +``` + +方法四:let + +``` +[root@xingdiancloud ~]# let sum=2+3; echo $sum +[root@xingdiancloud ~]# let i++; echo $i +``` + +#### 2.小数运算 + +​ 使用bc做小数运算,scale指定小数点位数 + +加法运算(scale参数无效) + +```shell +[root@xingdiancloud ~]# echo "5.999 + 5.001"|bc +6.000 +[root@xingdiancloud ~]# echo "5.111+ 5.1114"|bc +10.2224 +``` + +减法运算(scale参数无效) + +```shell +[root@xingdiancloud ~]# echo "2.22 - 1.11"|bc +1.11 +``` + +乘法运算 + +```shell +[root@xingdiancloud ~]# echo "5.12 * 5.6000"|bc +28.6720 +``` + +注意:乘积小数点位数默认以乘数中小数点位数最多的为准(不指定scale参数) + +除法运算 + +```shell +[root@xingdiancloud ~]# echo "scale=2;9.898 / 1.11"|bc +8.91 +[root@xingdiancloud ~]# echo "9.898 / 1.11"|bc +8 +``` + +## 五:扩展 + +#### 1.内容的删除 + +案例一 + +```shell +[root@xingdian ~]# url=www.sina.com.cn +[root@xingdian ~]# echo ${#url} 获取变量值的长度 + 15 +[root@xingdian ~]# echo ${url} 标准查看 +www.sina.com.cn +[root@xingdian ~]# echo ${url#*.} 从前往后,最短匹配 +sina.com.cn +[root@xingdian ~]# echo ${url##*.} 从前往后,最长匹配 贪婪匹配 +cn +[root@xingdian ~]# url=www.sina.com.cn +[root@xingdian ~]# echo ${url#a.} +www.sina.com.cn +[root@xingdian ~]# echo ${url#*sina.} +com.cn +``` + +案例二 + +```shell +[root@xingdian ~]# url=www.sina.com.cn +[root@xingdian ~]# echo ${url} +www.sina.com.cn +[root@xingdian ~]# echo ${url%.*} 从后往前,最短匹配 +www.sina.com +[root@xingdian ~]# echo ${url%%.*} 从后往前,最长匹配 贪婪匹配 +www +[root@xingdian ~]# echo $HOSTNAME +xingdian.1000phone.com +[root@xingdian ~]# echo ${HOSTNAME%%.*} +xingdian +``` + +#### 2.索引及切片 + +```shell +[root@xingdian ~]# echo ${url:0:5} +0:从头开始 +5:到第五个 +[root@xingdian ~]# echo ${url:5:5} +[root@xingdian ~]# echo ${url:5} +``` + +#### 3.变量内容替换 + +```shell +[root@xingdian ~]# url=www.sina.com.cn +[root@xingdian ~]# echo ${url/sina/baidu} +www.baidu.com.cn +[root@xingdian ~]# url=www.sina.com.cn +[root@xingdian ~]# echo ${url/n/N} +www.siNa.com.cn +[root@xingdian ~]# echo ${url//n/N} 贪婪匹配 +www.siNa.com.cN +``` + +#### 4.自增运算 + +对变量的值的影响 + +```shell +[root@xingdian ~]# i=1 +[root@xingdian ~]# let i++ +[root@xingdian ~]# echo $i +2 +[root@xingdian ~]# j=1 +[root@xingdian ~]# let ++j +[root@xingdian ~]# echo $j +2 +``` + +对表达式的值的影响 + +```shell +[root@xingdian ~]# unset i +[root@xingdian ~]# unset j +[root@xingdian ~]# +[root@xingdian ~]# i=1 +[root@xingdian ~]# j=1 +[root@xingdian ~]# +[root@xingdian ~]# let x=i++ 先赋值,再运算 +[root@xingdian ~]# let y=++j 先运算,再赋值 +[root@xingdian ~]# +[root@xingdian ~]# echo $i +2 +[root@xingdian ~]# echo $j +2 +[root@xingdian ~]# +[root@xingdian ~]# echo $x +1 +[root@xingdian ~]# echo $y +2 +``` diff --git a/shell-MD/shell并发控制.md b/shell-MD/shell并发控制.md new file mode 100644 index 0000000..2da1472 --- /dev/null +++ b/shell-MD/shell并发控制.md @@ -0,0 +1,167 @@ +

shell并发控制

+ +作者:行癫(盗版必究) + +------ + +## 一:FD文件描述符 + +#### 1.FD简述 + +​ FD:文件描述符/文件句柄 + +​ 进程使用文件描述符用来管理进程打开的文件 + +```shell +[root@xingdiancloud ~]# ll /proc/$$/fd +总用量 0 +lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0 +[root@xingdiancloud ~]# touch file1 +启用自定义文件描述符打开文件 +[root@xingdiancloud ~]# exec 6<> file1 +[root@xingdiancloud ~]# ll /proc/$$/fd +总用量 0 +lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 6 -> /root/file1 +[root@xingdiancloud ~]# echo "xingdian" > /proc/$$/fd/6 +[root@xingdiancloud ~]# cat file1 +xingdian +[root@xingdiancloud ~]# rm -rf file1 +[root@xingdiancloud ~]# ll /proc/$$/fd +总用量 0 +lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 6 -> '/root/file1 (deleted)' +[root@xingdiancloud ~]# cat /proc/$$/fd/6 +xingdian +释放文件描述符 +[root@xingdiancloud ~]# exec 6<&- +[root@xingdiancloud ~]# ll /proc/$$/fd +总用量 0 +lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0 +lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0 +``` + +注意: + +​ exec打开一个文件 + +​ exec关闭一个文件(释放文件句柄) + +​ 当一个文件FD未被释放,删除源文件也不会影响FD + +#### 2.管道简述 + +匿名管道 + +```shell +[root@xingdiancloud ~]# rpm -qa | grep rpm +``` + +命令管道 + +```shell +[root@xingdiancloud ~]# mkfifo /tmp/xingdian +[root@xingdiancloud ~]# file /tmp/xingdian +/tmp/xingdian: fifo (named pipe) +[root@xingdiancloud ~]# tty +/dev/pts/0 +[root@xingdiancloud ~]# rpm -qa > /tmp/xingdian + +另一个终端 +[root@xingdiancloud ~]# grep bash /tmp/xingdian +bash-5.1.8-6.el9.x86_64 +bash-completion-2.11-4.el9.noarch +[root@xingdiancloud ~]# tty +/dev/pts/1 +``` + +#### 3.FD案例 + +```shell +[root@xingdiancloud ~]# cat a.sh +#!/bin/bash +exec 7<> /etc/hosts +exec 8<> /etc/hostname + +while read -u 7 line +do + echo $line +done +while read -u 8 line2 +do + echo $line2 +done +exec 7<&- +exec 8<&- + +[root@xingdiancloud ~]# bash a.sh +127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 +::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +xingdiancloud +``` + +4.并发案例 + +```shell +#!/usr/bin/bash +#固定线程 +read -p "请输入线程数量:" diange +diangefile=/root/diangefile +mkfifo $diangefile //虚拟管道 +exec 8<>$diangefile //文件描述 +rm -rf $diangefile + +for i in `seq $diange` +do + echo >&8 //将内容写入到描述文件中,写如了空行 +done + +for i in {1..100} +do + read -u 8 //read 读取描述文件中的内容 + { + ip=192.168.101.$i + ping -c1 $ip &>/dev/null + if [ $? -eq 0 ];then + echo "$ip is up" + else + echo "$ip is down" + + fi + echo >&8 //等到进程执行完之后再往管道里丢内容以支持后面的循环 + }& +done +wait +echo "完成" +exec 8<&- //释放描述文件 +``` + + + + + + + + + + + + + + + + + + +