执行INSTALL脚本时出现路径错误的问题求助
执行INSTALL脚本时出现路径错误的问题求助
大家好,我在运行名为INSTALL的脚本时遇到了路径相关的错误,想请各位帮忙排查一下问题。
这是我要运行的INSTALL脚本内容:
USERNAME=$(env | grep SUDO_USER | sed s/SUDO_USER=//g) echo "User Name is:" $USERNAME mkdir /home/$USERNAME/.config/autostart/ chown $USERNAME:$USERNAME -R /home/$USERNAME/.config/autostart cp ./mirror_display.desktop /home/$USERNAME/.config/autostart/ chown $USERNAME:$USERNAME /home/$USERNAME/.config/autostart/mirror_display.desktop cp ./mirror_display.sh /usr/bin/ chown $USERNAME:$USERNAME /usr/bin/mirror_display.sh sync sync echo "Please reboot the system"
运行脚本后,得到了如下错误输出:
User Name is: mkdir: cannot create directory ‘/home//.config/autostart/’: No such file or directory chown: cannot access '/home//.config/autostart': No such file or directory
从错误信息能看出来,USERNAME变量是空的,导致脚本里的路径变成了/home//.config/autostart/,进而无法创建目录和修改权限。我猜测是脚本获取用户名的方式有问题,当前环境里可能没有SUDO_USER这个环境变量,比如我可能没通过sudo来运行脚本?
想请教下各位,应该怎么修改脚本里获取用户名的部分,才能避免这个问题呢?
备注:内容来源于stack exchange,提问作者newbie
相关产品推荐
相关产品推荐

