shell—脚本语言理解

一、脚本的调用

脚本调用有两种方式
方法一:

[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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值