作为练习,我们创建了一个bash 脚本 ,它将为我们提供以下信息:
系统基本信息
可用和已用磁盘空间
/home中每个用户的磁盘使用率
我们现在开始创建脚本。
nano linuxmi.com.sh
复制并粘贴以下内容:
#!/bin/bash
#
clear
echo "
选择一个选项:
1. 系统信息
2. 可用和已用磁盘空间
3. /home中每个用户的磁盘使用率
0. 退出
"
read -p "输入您的选项[0-3] > "
if [[ $REPLY =~ ^[0-3]$ ]]; then
if [[ $REPLY == 0 ]]; then
echo "脚本结束."
exit
fi
if [[ $REPLY == 1 ]]; then
echo "Hostname: $HOSTNAME"
uptime
exit
fi
if [[ $REPLY == 2 ]]; then
df -h
exit
fi
if [[ $REPLY == 3 ]]; then
if [[ $(id -u) -eq 0 ]]; then
echo " /home (All Users)的使用"
du -sh /home/*
else
echo "/home ($USER)的使用"
du -sh $HOME
fi
exit
fi
else
echo "输入错误." >&2
exit 1
fi
保存并关闭文件。
运行它:
bash linuxmi.com.sh
输出示例如下图: