shell脚本记录

vimrc常用配置

"搜索时忽略大小写
set ignorecase
"语法高亮
syntax on
""显示行数
"set nu
"不兼容vi模式
set nocompatible
"不自动缩进
set noautoindent
"tab占几个空格长度
set tabstop=4
""用空格代替tab
set expandtab
"自动写入作者信息
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "###############################################################################")
call setline(3, "# File Name:".expand("%"))
call setline(4, "# Version:V1.0")
call setline(5, "# Author:francis")
call setline(6, "###############################################################################")
endif
endfunc

案例01:书写脚本每次用户登录后显示系统的基本信息

#!/bin/bash
###############################################################################
# File Name:01_case_output_info.sh
# Version:V1.0
# Author:francis
###############################################################################
#写get_info函数,获取系统信息
get_info(){
local_name=$HOSTNAME		#获取主机名并赋值给变量local_name
local_ip=`hostname -I`		#获取ip并赋值给变量local_ip
total_mem=`free -h|awk 'NR==2{printf $2}'`		#获取总内存并赋值给变量total_mem
free_mem=`free -h|awk 'NR==2{printf $NF}'`		#获取可用内存并赋值给变量free_mem
sys_info=`uptime|awk '{print $(NF-2),$(NF-1),$(NF)}'`		#获取系统负载并赋值给变量sys_info
}
#写out_info函数,输出系统信息
out_info(){
echo "主机名:${local_name}"
echo "ip地址:${local_ip}"
echo "内存总数:${total_mem}"
echo "可用内存:${free_mem}"
echo "系统负载:${sys_info}"
}
#写main函数,调用get_info和out_info函数获取并输出系统信息
main(){
get_info
out_info $1
}
#调用main函数实现功能
main


#将脚本放到/etc/profile.d路径下保存为sh后缀文件,实现每次登陆自动调用

案例02:执行脚本的时候输入用户名,判断用户名是否存在

#!/bin/bash
###############################################################################
# File Name:02_case_check_user_have.sh
# Version:V1.0
# Author:francis
###############################################################################
. /etc/init.d/functions		#调用系统方法,用于后面输出【成功】【失败】标志
username=$1		#将输入用户名赋值给变量username
id $username >/dev/null		#使用id命令判断用户名是否存在,将输出重定向为空
end_result=$?		#判断上一条判断是否成功,并赋值给变量end_result
if [ $# -eq 0 ];then		#使用if方法,判断输入参数个数是否为0,0表示没有输入用户名
    echo "ERROR: placse input you want to check user"		#未输入时提示ERROR: placse input you want to check user
elif [ $end_result -eq 0 ];then		#判断用户名验证结果是否为0,0表示成功即用户存在,
    action "$username is have" /bin/true		#提示用户存在并加【成功】标志
else		#其他情况,即用户不存在
    action "$username is not have" /bin/false		#提示用户不存在,并加【失败】标志
fi		#if方法结束

案例03:脚本执行出错时提示使用帮助

#!/bin/bash
###############################################################################
# File Name:03_case_check_bash_result.sh
# Version:V1.0
# Author:francis
###############################################################################
bash_name=$0		#获取脚本名称并赋值给变量bash_name
input_cmd=$1		#获取命令内容并赋值给变量bash_name
case "$input_cmd" in		#使用case方法判断命令内容
    start)		#命令内容为start时
        echo "$bash_name is startting......"		#输出03_case_check_bash_result.sh is startting...... 
        ;;
    stop)		#命令内容为stop时
        echo "$bash_name is stoping......"		#输出03_case_check_bash_result.sh is stopting......
        ;;
    restart)		#命令内容为restart时
        echo "$bash_name is restart......"		#输出03_case_check_bash_result.sh is restartting......
        ;;
    *)		#命令为其他内容时
        echo "Usage: $bash_name {start|stop|restart}" 	#输出Usage:  脚本名字 {start|stop|restart}
esac		#case方法结束

案例04:判断脚本参数个数,不是1个时提示错误

###############################################################################
# File Name:04_case_check_param_num.sh
# Version:V1.0
# Author:francis
###############################################################################
. /etc/init.d/functions		#调用系统方法,用于后面输出【成功】【失败】标志
num=$#		#获取参数个数并赋值给变量num
if [ $num -ne 1 ];then		#调用if方法,判断变量值是否不等于1,即参数个数是否不等于1
    action "Usage: $0 need and only need one param" /bin/false				#不是一个参数时输出错误提示
else		#其他情况,只有一个参数
    action "you input param is $1" /bin/true		#输出正确提示
fi		#if方法结束

案例05:输出输入参数的个数和所有参数

#!/bin/bash
###############################################################################
# File Name:05_case_out_param_all.sh
# Version:V1.0
# Author:francis
###############################################################################
if [ $# -eq 0 ];then		#调用if方法,判断参数总数是否为0,即没有输入参数
    echo "you not input any param"		#提示没有输入参数
else		#其他情况
	echo "you input param is $#"		#输出参数总个数
	echo "you input all param have $*"		#输出所有参数
fi		#if方法结束

案例06:输入命令,检查命令执行结果

#!/bin/bash
###############################################################################
# File Name:06_case_check_cmd_result.sh
# Version:V1.0
# Author:francis
###############################################################################
. /etc/init.d/functions		#调用系统方法,用于后面输出【成功】【失败】标志
if [ $# -eq 0 ];then		#调用if方法判断是否输入命令
        action "you not input any cmd" /bin/false		#提示没有输入任何命令
        exit 1		#退出脚本执行
fi		#if方法结束
cmd=$*		#获取输入命令赋值给变量cmd
$cmd >/dev/null		#执行命令并将命令输出内容重定向为空
result=$?		#将命令执行结果赋值给变量result
if [ $result -eq 0 ];then		#调用if方法判断命令执行是否成功
    action "the cmd: ${cmd} is ok" /bin/true		#提示命令执行成功
else		#其他情况,即命令执行失败
    action "the cmd: ${cmd} is fail
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值