使用预设系统信息替换uname
#!/bin/bash
# 系统信息预设
KERNEL_NAME="Linux"
NODENAME="$(hostname 2>/dev/null || echo 'localhost.localdomain')"
KERNEL_RELEASE="3.10.0-957.el7.x86_64"
KERNEL_VERSION="#1 SMP Thu Nov 8 23:39:32 UTC 2018"
MACHINE="x86_64"
PROCESSOR="x86_64"
HARDWARE_PLATFORM="x86_64"
OPERATING_SYSTEM="GNU/Linux"
# 辅助函数前置定义
show_help() {
cat <<EOF
Usage: uname [OPTION]...
Print certain system information. With no OPTION, same as -s.
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type or "unknown"
-i, --hardware-platform print the hardware platform or "unknown"
-o, --operating-system print the operating system
--help display this help and exit
--version output version information and exit
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'uname invocation'
EOF
}
show_version() {
cat <<EOF
uname (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.
EOF
}
# 选项标志声明
declare -A flags=(
[s]=0 [n]=0 [r]=0 [v]=0
[m]=0 [p]=0 [i]=0 [o]=0
)
show_all=false
# 标准化参数解析
while getopts ":asnrvmpio-:" opt; do
case $opt in
a) show_all=true ;;
s) flags[s]=1 ;;
n) flags[n]=1 ;;
r) flags[r]=1 ;;
v) flags[v]=1 ;;
m) flags[m]=1 ;;
p) flags[p]=1 ;;
i) flags[i]=1 ;;
o) flags[o]=1 ;;
-) # 长选项处理
case "${OPTARG}" in
help) show_help; exit 0 ;;
version) show_version; exit 0 ;;
all) show_all=true ;;
kernel-name) flags[s]=1 ;;
nodename) flags[n]=1 ;;
kernel-release) flags[r]=1 ;;
kernel-version) flags[v]=1 ;;
machine) flags[m]=1 ;;
processor) flags[p]=1 ;;
hardware-platform) flags[i]=1 ;;
operating-system) flags[o]=1 ;;
*) echo -e "uname: unrecognized option '--${OPTARG}'\nTry 'uname --help' for more information." >&2; exit 1 ;;
esac
;;
\?)
echo -e "uname: invalid option -- '${OPTARG}'\nTry 'uname --help' for more information." >&2
exit 1
;;
esac
done
# 处理显示逻辑
output=()
if $show_all; then
# -a 的固定显示顺序
output=(
"$KERNEL_NAME"
"$NODENAME"
"$KERNEL_RELEASE"
"$KERNEL_VERSION"
"$MACHINE"
)
[[ "$PROCESSOR" != "unknown" ]] && output+=("$PROCESSOR")
[[ "$HARDWARE_PLATFORM" != "unknown" ]] && output+=("$HARDWARE_PLATFORM")
output+=("$OPERATING_SYSTEM")
else
# 常规选项按固定顺序处理
[[ ${flags[s]} -eq 1 ]] && output+=("$KERNEL_NAME")
[[ ${flags[n]} -eq 1 ]] && output+=("$NODENAME")
[[ ${flags[r]} -eq 1 ]] && output+=("$KERNEL_RELEASE")
[[ ${flags[v]} -eq 1 ]] && output+=("$KERNEL_VERSION")
[[ ${flags[m]} -eq 1 ]] && output+=("$MACHINE")
[[ ${flags[p]} -eq 1 && "$PROCESSOR" != "unknown" ]] && output+=("$PROCESSOR")
[[ ${flags[i]} -eq 1 && "$HARDWARE_PLATFORM" != "unknown" ]] && output+=("$HARDWARE_PLATFORM")
[[ ${flags[o]} -eq 1 ]] && output+=("$OPERATING_SYSTEM")
fi
# 默认显示内核名称
[[ ${#output[@]} -eq 0 ]] && output=("$KERNEL_NAME")
# 格式化输出
echo "${output[*]}"
exit 0
作者:wanghongwei
版权声明:本作品遵循<CC BY-NC-ND 4.0>版权协议,商业转载请联系作者获得授权,非商业转载请附上原文出处链接及本声明。

浙公网安备 33010602011771号