为了搞懂环境变量,作了一个小实验,体会到环境变量究竟是什么样子的。node
[root@localhost ~]# cat 1.shshell
#!/bin/bashcentos
echo $nbash
[root@localhost ~]# cat 22.shide
#!/bin/bash测试
export n=98spa
/root/1.sh操作系统
我在22.sh文件中定义了环境变量n,而且执行1.sh文件。在1.sh文件中执行echo $n 。而后我给两个文件都加上x权限,执行22.sh,咱们将会获得什么呢?blog
[root@localhost ~]# ./22.sh排序
98
很明显,咱们获得了98。但在22.sh中,并无直接echo $n的命令,但咱们调用了1.sh命令,因此能够打印出$n。那么问题就来了,1.sh中并无变量n=98,98又是怎么跑到n里面的呢?让咱们来看下面这张图。
首先22.sh中声明了环境变量n=98,当22.sh执行1.sh文件时,1.sh会引用22.sh中的环境变量n,而后执行自身的echo $n命令,再将结果送到22.sh中。
实验总结:
环境变量对当前的shell进程和其子进程
环境变量随当前shell进程结束而消失,若是当前进程关闭,则子进程没法在使用父进程中的环境变量。
练习:
一、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操做系统版本,内核版本,CPU型号,内存大小,硬盘大小。
echo "The hostname is: `hostname`"
echo "The ip address is: `ifconfig | sed -n 's@^[[:space:]]\+inet[[:space:]]@@p' | head -n 1 | cut -d ' ' -f 1`"
echo "The systerm is: `cat /etc/centos-release`"
echo "The kenerl is : `uname -r`"
echo "The cpu is: `lscpu | sed -n '13p'|tr -s " "|cut -d " " -f 3-7`"
echo "The MemTotal is : `cat /proc/meminfo | sed -n '1p'| tr -s ' '|cut -d " " -f 2-3`"
echo "The disk size is : `fdisk -l | grep "\<sd[a-z]\>" | cut -d " " -f 3-4 | tr ',' ' 'dev/sd[a-z]`"
二、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#/bin/bash
cp -r /etc/ /root/etc`date +%F`
三、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
echo "The use number of disk :`df | grep "sd" | tr -s ' ' | cut -d " " -f 5|sort -n | tail -n 1`"
四、编写脚本/root/bin/links.sh,显示正链接本主机的每一个远程主机的IPv4地址和链接数,并按链接数从大到小排序
#!/bin/bash
echo "`netstat -nt|tr -s ' '| cut -d ' ' -f 4 | grep "^[0-9]"|cut -d: -f1 | uniq -c |sort -nr`"
五、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#/bin/bash
USER10=`cat /etc/passwd | sed -n '10p' | cut -d: -f 3`
USER20=`cat /etc/passwd | sed -n '20p' | cut -d: -f 3`
SUM=$(($USER10+$USER20))
echo "The sum is $SUM"
六、写一个脚本/root/bin/sumspace.sh,传递两个文件路径做为参数给脚本,计算这两个文件中全部空白行之和
#!/bin/bash
first=`grep "^$" $1 | wc -l`
second=`grep "^$" $2 | wc -l`
SUM=$[$first+$second]
echo "The sum is $SUM"
七、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash
Etc=`ls -1 /etc | wc -l`
Var=`ls -l /var | wc -l`
Usr=`ls -l /usr | wc -l`
Sum=$[Etc+Var+Usr]
echo "The SUM is $Sum"
八、写一个脚本/root/bin/argsnum.sh,接受一个文件路径做为参数;若是参数个数小于1,则提示用户“至少应该给一个参数”,并当即退出;若是参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash
read -p "please give me the filesname:" filename
[ -e $filename ] && echo "The file space lines are:$(grep "^$" $filename | wc -l )" || echo "No file"
九、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址作为参数,测试是否可连通。若是能ping通,则提示用户“该IP地址可访问”;若是不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash
read -p "you want to ping IPv4:" Ip
ping -c1 -W1 $Ip &> /dev/null && echo "ping successful" && exit || echo "ping failed"
十、判断硬盘的每一个分区空间和inode的利用率是否大于80,若是是,发邮件通知root磁盘满
#!/bin/bash
Inode=`df -i | grep "sd[a-z]" | tr -s " " | cut -d " " -f 5 | sed 's@\%@@'`
Disk=`df | grep "sd" | tr -s ' ' | cut -d " " -f 5 | sed 's@\%@@'`
[ $Inode -ge 80 ] || [ $Disk -ge 80 ] && mail -s "NO free size for your DISK" root < help.txt
十一、指定文件作为参数,判断文件是否为.sh后缀,若是是,添加x权限
#!/bin/bash
read -p "please give a filename:" file
[[ $file =~ .*.sh$ ]] && chmod +x ./$file && echo "This is a shellfile." || echo "This is not a shellfile."
十二、判断输入的IP是否为合法IP
#!/bin/bash
read -p "please write IP:" Ip
echo "$Ip"| egrep -o "\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"&> /dev/null && echo "right ip" || echo "worng ip "
1三、计算1+2+3+...+100
#!/bin/bash
echo {1..100} | tr " " "+" |bc
1四、输入起始值A和最后值B,计算从A+(A+1)...+(B-1)+B的总和
#!/bin/bash
read -p "first number:" a
read -p "second number:" b
[ $a -ge $b ] && echo "sum is `seq -s+ $b $a | bc`" || echo "sum is `seq -s+ $a $b | bc`"