shell编程基础

2017-12-11  本文已影响0人  小小小小小台

Bash变量

· 在Bash中,变量的默认类型都是字符串型

    如果需要要转换,用declare命令

    declare [-aixr] 变量

    a: array, i: integer , x:环境变量, r: readonly只读变量

变量的“申明和赋值规则”:变量=值

· 变量的打印:echo $变量 或者 echo ${变量}

    把一个变量的值赋值给另外一个:变量=${变量}

· 变量为可扩增变量时:变量="$变量":或者变量=${变量}:

· 调用额外命令的信息:`指令` 或者  $(指令)

两者作用一致,使用反引号会更简洁

· 若该变量需要在其他子程序中执行,需要用export来使变量变成环境变量

· 取消变量的方法:unset

程序实例:

用shell脚本查看主机是否启动www、ssh、ftp、mail服务

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

touch /dev/shm/netstat_check.txt #创建文档用于存储信息

netfile=/dev/shm/netstat_check.txt  

netstat -tuln > ${netfile} #导入命令输出

testing=$(grep ":80 " ${netstat})# 引用grep的输出结果

if [ "${testing}" != "" ]; then

echo "WWW is running in your system."

fi

testing=$

(grep ":22 " ${netstat})#侦测看port 22在否?

if [ "${testing}" != "" ]; then

echo "SSH is running in your system."

fi

testing=$

(grep ":21 " ${netstat})#侦测看port 21在否?

if [ "${testing}" != "" ]; then

echo "FTP is running in your system."

fi

testing=$

(grep ":25 " ${netstat})#侦测看port 25在否?

if [ "${testing}" != "" ]; then

echo "Mail is running in your system."

fi

上一篇 下一篇

猜你喜欢

热点阅读