一、脚本的调用
脚本调用有两种方式
方法一:
[root@localhost mnt]# sh westos.sh
hello
方法二:
[root@localhost mnt]# chmod +x westos.sh
[root@localhost mnt]# /mnt/westos.sh
hello
二、脚本的检查
方法一:
[root@localhost mnt]# sh -x /mnt/westos.sh
+ echo hello
hello
方法二:
root@localhost mnt]# vim westos.sh
[root@localhost mnt]# /mnt/westos.sh
+ echo hello
hello
[root@localhost mnt]# cat westos.sh
#!/bin/bash -x
echo hello
三、自定义脚本格式
1、新建文件按F4自动加入
[root@localhost mnt]# vim /etc/vimrc
map <F4> ms:call WESTOS()<cr>'s
function WESTOS()
call append(0,"##################################")
call append(1,"# Author : luo #")
call append(2,"# Mail : luo@westos.com #")
call append(3,"# Version : 1.0 #")
call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
call append(5,"# Description : #")
call append(6,"##################################")
call append(7,"")
call append(8,"#!/bin/bash")
endfunction
测试:vim file.sh
二、新建以.sh结尾的文件自动加入
[root@localhost mnt]# vim /etc/vimrc
autocmd BufNewFile *.sh exec ":call WESTOS()"
function WESTOS()
call append(0,"##################################")
call append(1,"# Author : luo #")
call append(2,"# Mail : luo@westos.com #")
call append(3,"# Version : 1.0 #")
call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
call append(5,"# Description : #")
call append(6,"##################################")
call append(7,"")
call append(8,"#!/bin/bash")
endfunction
测试:
示例:
1、显示当前主机的ip
[root@localhost mnt]# vim ip.sh
[root@localhost mnt]# sh ip.sh
172.25.254.177
脚本内容:
#!/bin/bash
ifconfig eth0 | head -2 | tail -1 | cut -c 14-27
2、显示当前主机能登陆系统的用户
[root@localhost mnt]# vim user_show.sh
[root@localhost mnt]# sh user_show.sh
root
student
xiaoming
user1
user2
user3
[root@localhost mnt]# cat user_show.sh
##################################
# Author : luo #
# Mail : luo@westos.com #
# Version : 1.0 #
# Create_Time :2018-05-17 #
# Description : #
##################################
#!/bin/bash
cat /etc/passwd | grep bash | cut -d : -f 1
3、清空日志
[root@localhost mnt]# cat clear_log.sh
##################################
# Author : luo #
# Mail : luo@westos.com #
# Version : 1.0 #
# Create_Time :2018-05-17 #
# Description : #
##################################
#!/bin/bash
> /var/log/messages
[root@localhost mnt]# sh clear_log.sh