设为首页 加入收藏

TOP

Shell---控制流程(一)
2023-07-23 13:36:27 】 浏览:42
Tags:Shell---

操作系统:

RHEL7.x 或CentOS 7.x

  • 最小化安装
  • 配置好固定的IP,能访问互联网
  • 配置好yum源(yum repolist 可以查看yum源)
    • 本地光盘
      • 挂载光盘,开机自动挂载
        • vim + /etc/fstable
        • /dev/sr0 /mnt iso9660 defaults 0 0
      • 创建挂载点目录:
        • mkdir /media/cdrom
      • 挂载:mount -a
      • 配置yum源:
        • yum-config-manger --add-repo=file:/// media/cdrom
        • echo "gpgcheck = 0" >> /etc/yum.repos.d/media_cdrom.repo
    • EPEL
      • aliyun sohu 中科大 清华 网易

开发环境:vim

查看系统shell类型:

[root@template ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

查看当前默认shell:

[root@template ~]# echo $SHELL
/bin/bash

快速如何快速生成脚本开头的版本版权注释信息

[root@template ~]# cat ~/.vimrc
autocmd BufNewFile *.go,*.py,*.cc,*.sh,*.java 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,"#Email:admin@test.com")
call setline(6,"#Created time:".strftime("%F %T"))
call setline(7,"#Description:")
call setline(8,"#########################")
call setline(9,"")
endif
endfunc

修改Tab缩进

在/etc/vim/下有两个文件,分别为vimrc 和vimrc.tiny

在vimrc文件中加入:set tabstop=4

流程控制与判断练习:

1、ping主机测试,查看主机是否存活

[root@template chap04]# cat ping.sh
#!/bin/bash
#########################
#File name:ping.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-03-23 17:32:18
#Description:
#########################
read -p "please enter your host:" host
if ping -c2 $host &> /dev/null
then
        echo "$host is running"
else
        echo "$host is down"
fi

2、判断一个用户是否存在

[root@template chap04]# cat user.sh
#!/bin/bash
#########################
#File name:user.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-03-23 17:45:03
#Description:
#########################
read -p "please enter a username:" username
if id $username &> /dev/null
then
        echo "$username is exist"
else
        echo " $username is not exist "
fi

3、判断当前内核主版本是否为3,且次版本是否大于10

[root@template chap04]# cat sys.sh
#!/bin/bash
#########################
#File name:sys.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-03-23 17:52:56
#Description:
main_version=`uname  -r | awk -F . '{print $1 }'`
minor_version=`uname -r | awk -F . '{print $2}'`
if [ "$main_version"  -eq 3 ] && [ "$minor_version" -ge 10 ]
  then
     echo "主版本是:$main_version 次版本是:$minor_version"
else
       echo "不满足条件,此系统的主版本是:$main_version 次版本是:$minor_version"
fi

4、判断vsftpd软件包是否安装,如果没有则自动安装

#!/bin/bash
#########################
#File name:isvsftp.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-03-23 18:05:56
#Description:
#########################
if rpm -qa | grep vsftpd &> /dev/null
then
        echo "vsftp is exist"
else
        echo "vsftp is not exist"
        read -p "please enter your choice:" choice
        if [ $choice -eq 1 ]
                then
                        yum install vsftpd -y &> /dev/null
        else
                echo " break and uninstall"
        fi
fi

# 测试结果:最初环境没有安装,选择时随机输入,最后选择安装,最后一次测试检查是否安装成
[root@template chap04]# ./isvsftp.sh
vsftp is not exist
please enter your choice:2
 brea
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇mail_api_flask 接口开发及uwsgi.. 下一篇【问题解决】Linux 下 VSCode Int..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目