我正在构建一个替代系统信息报告器,例如neofetch,它工作正常,但有一个问题。 运行 bash 时无法获取当前终端模拟器。 我尝试了这些命令:
readlink "/proc/$(cat /proc/$(echo $$)/stat|cut -d ' ' -f 4)/exe"
ps -o comm= -p $(ps -o ppid= -p $$)
正常执行时它们工作得很好,但是当我将它们放入 bash 脚本中时,它只返回 bash 或 /usr/bin/bash。
我想找到一个始终报告终端仿真器而不是 shell 的命令。 如果您愿意,这里是完整的代码:
#!/bin/sh
# yes. no logos. because why not?
# source the config file
if [ "$colorsoff" = "" ]; then
colorsoff=0
fi
[ -e /etc/betterfetchrc ] && . /etc/betterfetchrc 2> /dev/null
[ -e ~/.betterfetchrc ] && . ~/.betterfetchrc 2> /dev/null
CURRENT_VERSION_FILE="/etc/betterfetch-version" # Path to the file containing the current version
REMOTE_VERSION_URL="https://raw.githubusercontent.com/sctech-tr/betterfetch/main/betterfetch-version"
# Fetch the remote version from the URL
REMOTE_VERSION=$(curl -s $REMOTE_VERSION_URL)
# Read the current version from the version file
if [ -f "$CURRENT_VERSION_FILE" ]; then
CURRENT_VERSION=$(cat "$CURRENT_VERSION_FILE")
else
echo "error: current version file not found."
exit 1
fi
# Compare the remote version with the current version
if [ "$REMOTE_VERSION" != "$CURRENT_VERSION" ]; then
echo "betterfetch ($REMOTE_VERSION) is available! you are currently on version $CURRENT_VERSION."
exit 0
fi
# the meat and potatoes, actual fetch
# only set these if they are not already set by the config file
[ -z "$os" ] && . /etc/os-release 2>/dev/null || export os="Unknown"
[ -z "$host" ] && host=$(cat /proc/sys/kernel/hostname)
[ -z "$kernel" ] && kernel=$(sed "s/version // ; s/ (.*//" /proc/version)
[ -z "$uptime" ] && uptime=$(uptime -p 2>/dev/null | sed "s/up //")
[ -z "$shell" ] && shell=$(printf "$SHELL" | sed "s/\/bin\///" | sed "s/\/usr//")
[ -z "$de" ] && de=$(echo $XDG_CURRENT_DESKTOP)
[ -z "$terminal" ] && terminal=$(readlink "/proc/$(cat /proc/$(echo $$)/stat|cut -d ' ' -f 4)/exe")
printf "$USER@$host\n"
printf "OS $os\n"
printf "Kernel $kernel\n"
printf "Uptime $uptime\n"
printf "Shell $shell\n"
printf "DE $de\n"
printf "Terminal $terminal\n"
if [ "$colorsoff" != 1 ]; then
printf "\033[0;31m● \033[0;32m● \033[0;33m● \033[0;34m● \033[0;35m● \033[0;36m● \033[0;37m●\033[0m\n"
fi
一般情况下是不可能做到的。您可以遍历 pid 层次结构直到从已知列表中找到终端模拟器的唯一最有效的方法。