欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

712脚本作业

程序员文章站 2022-07-09 17:21:48
1.判断/tmp/run目录是否存在,如果不存在就建立,如果存在就删除目录里所有文件#!/bin/bash[ -d /tmp/run ] && { echo "/tmp/run exists" rm -fr /tmp/run/* echo "the dir is clear"}||{ echo "/tmp/run not exists" mkdir -p /tmp/run ech...

1.判断/tmp/run目录是否存在,如果不存在就建立,如果存在就删除目录里所有文件

#!/bin/bash
[ -d /tmp/run ] && {
        echo "/tmp/run exists"
        rm -fr /tmp/run/*
        echo "the dir is clear"
}||{    
        echo "/tmp/run not exists"
        mkdir -p /tmp/run
        echo "/tmp/run now exists"
        exit
} 

 

2.输入一个路径,判断路径是否存在,而且输出是文件还是目录,如果是链接文件,还得输出是有效的连接还是无效的连接

#!/bin/bash
read -p "please input path: " path
[ -e $path ] && {
        echo "the path exists" 
            [ -d $path ]&& {
            echo "the path is dir"
            exit
        }
            [ -L $path ]&& {
            echo "the path is link"
            exit
        }
            [ -f $path ]&& {
            echo "the path is common file"
            exit
        }
}||{
        echo "the path not exists"
        exit
}

 

3.交互模式要求输入一个ip,然后脚本判断这个IP对应的主机是否能ping通,输出结果类似于: Server 10.1.1.20 is Down! 最后要求把结果邮件到本地管理员root@localhost mail01@localhost

#!/bin/bash
read -p "please input an ip:" ip

ping -c1 $ip &> /dev/null
if [ $? -eq 0 ]; then
        echo "$ip can ping"
        echo "server $ip can ping" | mail -s root@localhost mail01@localhost
else
        echo "$ip cannot ping"
        echo "server $ip cannot ping" | mail -s root@localhost mail01@localhost
fi

 

4.判断用户输入的字符串,如果是"hello",则显示"world";如果是"world", 则显示"hello"。

而脚本没有参数或者参数错误时,屏幕上输出 “usage:please input hello or world

#!/bin/bash
read -p "please input a string: " string
case $string in
        hello)
                echo "world"
                ;;
        world)
                echo "hello"
                ;;
        *)
                echo “usage:please input hello or world”
                ;;
esac

 

5.编辑一个脚本,实现自动搭建samba服务(未完善)

#!/bin/bash
echo "1.判断网络是否畅通"
ping -c1 172.25.254.20 &>/dev/null
      if [ $? -eq 0 ];then
           echo "the network is ok"
      else
           echo "Error:the network is not working"
           exit 1
      fi


echo "2.关闭selinux和firewalld"
setenforce 0 &>/dev/null
     if [ $? -eq 0 ];then
             echo "selinux is disabled"
     fi
systemctl stop firewalld &>/dev/null
     if [ $? -eq 0 ];then
             echo "firewalld is stoped"
     fi

echo "3.查看软件是否安装"
rpm -q samba-common &>/dev/null
     if [ $? -eq 0 ];then
             echo "samba-common is installed"
     else
             echo "samba-common is not installed"
             dnf install samba-common -y
             rpm -q samba-common &>/dev/null
                  if [ $? -eq 0 ];then
                       echo "samba-common is now installed"
                  else
                       echo "samba-common installed failed"
                       exit 1
                  fi
     fi


rpm -q samba &>/dev/null
     if [ $? -eq 0 ];then
             echo "samba is installed"
     else
             echo "samba is not installed"
             dnf install samba -y
             rpm -q samba &>/dev/null
                  if [ $? -eq 0 ];then
                       echo "samba is now installed"
                  else
                       echo "samba installed failed"
                       exit 1
                  fi
     fi


rpm -q samba-client &>/dev/null
     if [ $? -eq 0 ];then
             echo "samba-client is installed"
     else
             echo "samba-client is not installed"
             dnf install samba-client -y
             rpm -q samba-client &>/dev/null
                  if [ $? -eq 0 ];then
                       echo "samba-client is now installed"
                  else
                       echo "samba-client installed failed"
                       exit 1
                  fi
     fi

echo "4.启动smb服务"
systemctl status smb.service | grep active &>/dev/null
if [ $? -eq 0 ];then
        systemctl restart smb
        echo "smb.service restart success"
else
        systemctl enable --now smb
fi


echo "5.添加用户和共享路径"
read -p "please input a user: " user
read -p "please input a path: " path

useradd $user
mkdir -p $path

 

本文地址:https://blog.csdn.net/weixin_46833747/article/details/107321546