linux · 2023年8月29日 0

知识点2

学习完了linux的基础,开始尝试写简单的脚本
在写脚本之前,可以先进行一些设置,这样以后新建的脚本都会包含这些内容

cd /etc
vim .vimrc

markdown复制代码
set ts=4
set expandtab
set ignorecase
set shiftwidth=4
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:                XXX")
     call setline(5,"#QQ:                    XXXXXXXXX")
     call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
     call setline(7,"#FileName:             ".expand("%"))
     call setline(8,"#URL:                   http://www.XXX.com")
     call setline(9,"#Description:          The test script")
     call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
     call setline(11,"#********************************************************************")
     call setline(12,"")
     endif
endfunc
autocmd BufNewFile * normal G

set nu 

1. 运行脚本可以显示出本机的ip地址
bash复制代码IP=$(ifconfig ens160 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n1)
echo IP地址为:"$IP"

2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间; 如果ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, ... magedu_100并且所有用户同属于magedu组
if复制代码   echo 当前系统时间为:$(date +%F%n%T)
else
   groupadd magedu
   for i in {0..100}; do
       if [ $i -lt 10 ]; then
       useradd magedu0$i -g magedu
       else
       useradd magedu$i -g magedu
       fi
   done
fi

3. 打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
bash复制代码echo 打印出文件中可以登陆的用户
cat /etc/passwd | grep -v sbin/nologin

4. yum安装nginx服务,并且启动该服务
lua复制代码yum -y install nginx
systemctl start nginx
systemctl status nginx