【发布时间】:2017-04-25 05:10:58
【问题描述】:
我有两个不同的用户用户“a”和用户“root”的两个 shell 脚本:
a.sh 用于用户“a”
export CODE=`pwd | cut -d / -f 5`
isEnabled=`grep -i isEnabled $HOME/$CODE/config/code.properties | awk -F'=' '{print $2}'`
b.sh 用于用户 'root'
su - a -c "sh /export/home/a/a1/bin/a.sh status
如您所见,root 执行脚本 b.sh 执行脚本 a.sh 但实际上它失败了,因为它没有找到脚本的路径,因为 运行 b.sh 时,a.sh 中的变量 $CODE 可能在运行时丢失
经过调查,我厌倦了 b.sh 中的以下内容:
. /path/to/a.sh
source /path/to/a.sh
. /path/to/a.sh "$CODE"
. $(/path/to/a.sh $CODE)
有人可以建议吗?
谢谢大家。
【问题讨论】:
-
su - a完成后,当前目录将设置为用户a的主目录。我认为这会在a.sh中抛弃您的逻辑。为什么需要依赖当前目录?code.properties的路径能否以更好的方式导出? -
当用户执行 a.sh 时,它运行良好。但是当我通过 root 执行它时,它会抛出一个错误,因为 root 缺少 $CODE 导致重新分级未知路径