如何反映用户脚本对 PATH 所做的更改

如何反映用户脚本对 PATH 所做的更改

我有一个脚本,它执行一些基本的根 PATH 完整性检查并输入一行来~/.bash_profile提供我的脚本,这样我在根目录中所做的脚本更改PATH在重新启动后仍然是永久的:

cat path_clean
#!/bin/bash

OLD_PATH=`echo $PATH`

OIFS=$IFS
IFS=:
for path in ${OLD_PATH}; do
    [ -d "${path}" ] || continue
    paths=( "${paths[@]}" "${path}" )
done

while read -r stat owner path; do
    [ "${owner}${stat:5:1}${stat:8:1}" = 'root--' ] || continue
    newpath="${newpath}:${path}"
done < <(stat -c "%A:%U:%n" "${paths[@]}" 2>/dev/null)

IFS=${OIFS}

NEW_PATH=${newpath#:}
export PATH=${NEW_PATH}
egrep "^[ ]*if.*fi;" ~/.bash_profile &> /dev/null
if [ $? -ne 0 ];
then
   echo 'if [ -f /root/path_clean ]; then source /root/path_clean; fi;' >>     ~/.bash_profile
fi

当我第一次运行脚本时,一切都按预期运行:

./path_clean
. ~/.bash_profile

但是这个脚本有一个问题。如果我. ~/.bash_profile多次执行这个脚本,那么每次我都会看到 PATH 变量中的值重复出现。

例如在执行脚本之前我的PATH是:

[root@labeir1 ~]# echo $PATH
    /usr/bin/X11:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin

现在. ~/.bash_profile第一次运行后一切正常:

[root@labeir1 ~]# ./path_clean
[root@labeir1 ~]# echo $PATH <--still same as original `PATH`, which is ok
   /usr/bin/X11:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/root/bin
[root@labeir1 ~]# . ~/.bash_profile <--now sourcing `.bash_profile`1st time
[root@labeir1 ~]# echo $PATH <--my script has changed `PATH` fine.
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin
[root@labeir1 ~]# . ~/.bash_profile
[root@labeir1 ~]# echo $PATH <-- problem starts
    /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin
[root@labeir1 ~]# . ~/.bash_profile
[root@labeir1 ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ulticom/diameter/bin:/apps/omni/bin

请建议对脚本进行哪些更改以使其正常运行并消除此问题。

答案1

使用正确的支票。

[ -d … ]检查给定的路径是否是目录,而您的脚本显然不是。

您可能想要-f、、-s-e

答案2

您添加的两行:

echo 'if [ -d /root/path_clean ]; then source /root/path_clean; fi;' >> /etc/profile
echo 'if [ -d /root/path_clean ]; then source /root/path_clean; fi; >> ~/.bash_profile

检查是否存在目录;你最有可能的意思是:

echo 'if [ -f /root/path_clean ]; then source /root/path_clean; fi;' >> /etc/profile
echo 'if [ -f /root/path_clean ]; then source /root/path_clean; fi; >> ~/.bash_profile

注意括号内的“-f”。

相关内容