嗨,我正在尝试制作Shell脚本。
sudo usermod -s $(whereis -b zsh) $(whoami)
$(whereis -b zsh)
对zsh: command not found zsh:
出错
似乎发生了错误,因为whereis -b zsh
的输出是zsh: /usr/bin/zsh /usr/lib/x86_64-linux-gnu/zsh /bin/zsh /etc/zsh /usr/share/zsh /home/linuxbrew/.linuxbrew/bin/zsh
现在,我想使用/usr/bin/zsh
作为脚本的输出。有什么方法可以从whereis -b zsh
的输出中获取第二个单词?
该脚本应如何获得我所需要的? shell脚本比我想象的要难。预先谢谢大家!
答案 0 :(得分:2)
最好在命令扩展处添加引号
sudo usermod -s "$(whereis zsh | cut -d ' ' -f2)" "$(whoami)"
从zsh
获取$PATH
的替代方法:
sudo usermod -s "$(command -v zsh)" "$(id -un)"
答案 1 :(得分:1)
如果在bash下运行它:
使用type
代替解析whereis的输出:
sudo usermod -s "$(type -P zsh)" "$(whoami)"
如果要搜索的程序不在PATH中,请不要忘记type -P
会产生一个空字符串。
如果不是bash,您也可以做
sudo usermod -s "$(which zsh)" "$(whoami)"
请注意,如果找不到该程序,which
会发出一条错误消息,因此,如果在这种情况下需要空输出,则必须丢弃stderr。
更新:考虑到这一点,IMO是Lea Gris建议的一个更好的解决方案:command -v
在bash和POSIX shell上可用,如果文件可以,则输出为空。找不到。
答案 2 :(得分:0)
您可以执行以下操作:
whereis -b zsh | awk '{print $2}'