xdg-data-dirs-opt-apps.sh和zsh有兼容性问题

当用login shell方式使用zsh的时候,比如ssh登陆,会出现

/etc/profile.d/xdg-data-dirs-opt-apps.sh:10: command not found: shopt
/etc/profile.d/xdg-data-dirs-opt-apps.sh:18: no matches found: /opt/apps/*/entries

由于/etc/zsh/zprofile里是直接source /etc/profle.d下的文件

# Begin /etc/profile
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>
# modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>

# System wide environment variables and startup programs.

# System wide aliases and functions should go in /etc/bashrc.  Personal
# environment variables and startup programs should go into
# ~/.bash_profile.  Personal aliases and functions should go into
# ~/.bashrc.

# Source profile scripts
for script in /etc/profile.d/* ; do
        case "$script" in
                *.bash)
                        [ "$BASH" ] && . "$script" ;;
                *.sh)
                        . "$script" ;;
        esac
done

# Now to clean up
unset pth script
# End /etc/profile

而zsh用的是setopt,而不是shopt

所以我的解决办法是这样更改xdg-data-dirs-opt-apps.sh

# shopt -s nullglob
if [[ $(readlink /proc/$$/exe) == "/usr/bin/bash" ]]; then
        shopt -s nullglob
elif [[ $(readlink /proc/$$/exe) == "/usr/bin/zsh" ]]; then
        setopt NULL_GLOB
fi

判断当前的shell是zsh还是bash,来执行不同的命令

就结果而言好像是好了,但由于我对shell编程不是很熟,所以想请教一下这样改是可行的吗?

感谢报告!

思路基本是对的,但也许应该用 $SHELL 变量作为判断条件,您试试看?

关于$SHELL变量,在这篇文章里提到,指的是默认的shell,当在终端中调用不同的 shell,$SHELL 也保持不变。

假如默认shell是bash,然后用login shell的方式调用zsh的话,用$SHELL来判断,可能就会不生效。

然后参考这篇文章,最后使用的就是readlink /proc/$$/exe这种方式来确定现在运行的是哪个shell

原来如此,我明天研究下有没有更好的办法——如果您发现什么别的点子,也请不吝赐教