1、编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

#!/bin/bash
read -p "please input a filename:" file
if [ ! -e $file ];then
        echo "file is not exits"
elif [ ! -r $file -a ! -w $file ];then
        echo "$USER can't read and write $file"
else
        echo "$file can read or write"
fi

执行结果:

[[email protected] testdata]# chmod +x per.sh
[[email protected] testdata]# ./per.sh
please input a filename:/etc/shadow
/etc/shadow can read or write
[[email protected] testdata]# su cwj
[[email protected] testdata]$ ./per.sh
please input a filename:/etc/shadow
cwj can't read and write /etc/shadow

2、编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如 果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash
read -p "please input a file:" file
if [ -e $file ];then
	if [ -f $file ];then
		suffix=`echo "$file" | awk -F. '{print $NF}'`
		if [ $suffix == "sh" ];then
			chmod a+x $file
			echo "$file change mode success"
		else
			echo "$file is not script file"
		fi
	else
		echo "file is not a normal file"
	fi
else
	echo "file is not exits"
fi

运行结果:

[[email protected] testdata]# ./excute.sh 
please input a file:scp1.exp
scp1.exp is not script file
[[email protected] testdata]# ./excute.sh 
please input a file:per.sh
per.sh change mode success

3、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和允许普通用户登录系统

禁止用户登录:

#!/bin/bash
read -p "please input user name:" name
id $name >/dev/null
if [ $? -eq 0 ];then
	userid=`id $name |sed -r 's/.*=([0-9]+).*/\1/'`
	if [ $userid -ge 500 ];then
		usermod -s /sbin/nologin $name
		echo "usermod success"
	else
		echo "$name is a system user"
	fi
else 
	echo "$name is not exits"
fi

运行结果:

please input user name:cwj
usermod success
[[email protected] testdata]# ./nologin.sh
please input user name:root
root is a system user
[[email protected] testdata]# getent passwd cwj
cwj:x:1000:1000:cwj:/home/cwj:/sbin/nologin

允许用户登录:

#!/bin/bash
read -p "please input user name:" name
id $name >/dev/null
if [ $? -eq 0 ];then
	userid=`id $name |sed -r 's/.*=([0-9]+).*/\1/'`
	if [ $userid -ge 500 ];then
		usermod -s /bin/bash $name
		echo "usermod success"
	else
		echo "$name is a system user"
	fi
else 
	echo "$name is not exits"
fi

运行结果:

[[email protected] testdata]# chmod +x login.sh 
[[email protected] testdata]# ./login.sh 
please input user name:cwj
usermod success
[[email protected] testdata]# ./login.sh 
please input user name:root
root is a system user
[[email protected] testdata]# getent passwd cwj
cwj:x:1000:1000:cwj:/home/cwj:/bin/bash

4、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第 20用户的ID之和

 #! /bin/bash
user10="`head -n $2 $1 | tail -n 1|cut -d: -f3`"
user20="`head -n $3 $1 | tail -n 1|cut -d: -f3`"
let sum=$user10+$user20
echo "user id sum is $sum"

5、用两种以上方式查目的地当前服务器上面io最繁忙的是哪块硬盘

  1. top命令
    脚本练习、计划任务管理
  2. 使用iostat,可以非常详细的查看到是哪块硬盘最繁忙,iostate 默认不安装在系统中,需要使用Yum安装: yum install iostat

6、在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup 0 6-12/3 * 12 * /usr/bin/backup

  1. 编写backup脚本
#!/bin/bash
cp /etc/passwd /home/cwj/testdata/passwd.bak`date +%F-%T`

2、编写计划任务

0 6-12/3 * 12 * /home/cwj/testdata/backup

相关文章:

  • 2021-05-18
  • 2022-01-13
  • 2021-09-27
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-01-16
猜你喜欢
  • 2022-01-05
  • 2021-06-07
  • 2021-10-19
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案