我有一个脚本,它执行一些基本的根 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
答案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”。