我的第一个shell脚本

首先加一个通用代码,用vim时,当后缀名为"*.sh"时,自动添加作者,联系方式,版本,时间,描述等信息。我们可以在
用户家目录下.vimrc或者全局配置文件/etc/vimrc中加入以下代码:

set tabstop=4                                                               
set softtabstop=4
set shiftwidth=4
set expandtab
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#")
call setline(3, "#===========================================================
=====")
call setline(4, "#Author:                               自己改")
call setline(5, "#QQ:                                   自己改")
call setline(6, "#Date:                                 ".strftime("%Y-%m-%d"
))
call setline(7, "#FileName:                             ".expand("%"))
call setline(8, "#URL:                                自己改
")
call setline(9, "#Description:                  The test script")
call setline(10, "#Copyritght (C):              ".strftime("%Y")." ALL rights
 reserved")
call setline(11, "#==========================================================
======")
call setline(12,"")
endif
endfunc

ok,打开vim编辑器,编写第一个shell脚本createuser.sh
功能如下:
使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息

#!/bin/bash
#
#================================================================
#Author:                               zouybi
#QQ:                                   273838882
#Date:                                 2020-02-29
#FileName:                             createuser.sh
#URL:                                  http://www.和谐.com
#Description:                  The test script
#Copyritght (C):              2020 ALL rights reserved
#================================================================
read -p "give me a username:" userNAME
if id -u $userNAME &> /dev/null;then                                         
    echo "user exists"
    echo "$userNAME info is:`id $userNAME`"
else
    echo "user does not exist,to start create"
    useradd $userNAME && echo "$userNAME info is:`id $userNAME`" 
fi

#执行结果
[root@localhost data]# chmod +x createuser.sh    
[root@localhost data]# ./createuser.sh 
give me a username:wang
user does not exist,to start create
wang info is:uid=1001(wang) gid=1001(wang) groups=1001(wang)
[root@localhost data]# ./createuser.sh 
give me a username:wang
user exists
wang info is:uid=1001(wang) gid=1001(wang) groups=1001(wang)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容