Bash 脚本实现每次登录到 Shell 时可以查看 Linux 系统信息

MAGESH MARUTHAMUTHU 的头像

·

·

·

7,444 次阅读

Linux 中有很多可以查看系统信息如处理器信息、生产商名字、序列号等的命令。你可能需要执行多个命令来收集这些信息。同时,记住所有的命令和他们的选项也是有难度。

你可以写一个 shell 脚本 基于你的需求来自定义显示的信息。

以前我们出于不同的目的需要写很多个 bash 脚本

现在我们写一个新的 shell 脚本,在每次登录到 shell 时显示需要的系统信息。

这个脚本有 6 部分,细节如下:

  1. 通用系统信息
  2. CPU/内存当前使用情况
  3. 硬盘使用率超过 80%
  4. 列出系统 WWN 详情
  5. Oracle DB 实例
  6. 可更新的包

我们已经基于我们的需求把可能需要到的信息加到了每个部分。之后你可以基于自己的意愿修改这个脚本。

这个j脚本需要用到很多工具,其中大部分我们之前已经涉及到了。

你可以参照以前文章,了解工具详情。

如果你想为这个脚本增加其他的信息,请在评论去留下你的需求,以便我们帮助你。

Bash 脚本实现每次登录到 Shell 时可以查看 Linux 系统信息

这个脚本会在你每次登录 shell 时把系统信息打印到 terminal。

# vi /opt/scripts/system-info.sh
#!/bin/bash
echo -e "-System Information"
if id oracle >/dev/null 2>&1; then
/bin/ps -ef|grep pmon
then
else
echo "oracle user does not exist on $(hostname)"
fi
echo ""

if (( $(cat /etc/*-release | grep -w "Oracle|Red Hat|CentOS|Fedora" | wc -l) > 0 ))
then
echo -e "-Package Updates-"
yum updateinfo summary | grep 'Security|Bugfix|Enhancement'
echo -e "--"
fi

把上面脚本内容保存到一个文件 system-info.sh,之后添加可执行权限:

# chmod +x ~root/system-info.sh

当脚本准备好后,把脚本文件的路径加到 .bash_profile 文件末尾(红帽系列的系统:CentOS、Oracle Linux 和 Fedora):

# echo "/root/system-info.sh" >> ~root/.bash_profile

执行以下命令,来让修改的内容生效:

# source ~root/.bash_profile

对于 Debian 系统的系统,你可能需要把文件路径加到 .profile 文件中:

# echo "/root/system-info.sh" >> ~root/.profile

运行以下命令使修改生效:

# source ~root/.profile

你以前运行上面 source 命令时可能见过类似下面的输出。从下次开始,你在每次登录 shell 时会看到这些信息。当然,如果有必要你也可以随时手动执行这个脚本。


-System Information
oracle user does not exist on CentOS8.2daygeek.com

-Package Updates-
    13 Security notice(s)
         9 Important Security notice(s)
         3 Moderate Security notice(s)
         1 Low Security notice(s)
    35 Bugfix notice(s)
     1 Enhancement notice(s)

via: <https://www.2daygeek.com/bash-shell-script-view-linux-system-information/>

作者:[Magesh Maruthamuthu](https://www.2daygeek.com/author/magesh/) 选题:[lujun9972](https://github.com/lujun9972) 译者:[lxbwolf](https://github.com/lxbwolf) 校对:[wxy](https://github.com/wxy)

本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

3 条回复

  1. 来自山东烟台的 Firefox 71.0|Windows 10 用户 的头像
    来自山东烟台的 Firefox 71.0|Windows 10 用户

    这行多了个字母j,看着难受:“这个j脚本有 6 部分,细节如下:”

    来自烟台
  2. Mr.Wang [Chrome 78.0|Mac 10.14] 的头像
    Mr.Wang [Chrome 78.0|Mac 10.14]

    脚本38行then应该删掉。(虽然原文也是这么写的)

    来自北京
  3. 来自四川成都的 Chrome 91.0|Windows 7 用户 的头像
    来自四川成都的 Chrome 91.0|Windows 7 用户

    ?哪行呢

    来自成都

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注