首页 > 解决方案 > 无法在 ZSH 中运行 bash 文件

问题描述

.zshrc每次打开新的终端窗口或源代码时,我都在其中放置了一个 bash 文件,并尝试了所有不同的方法来运行它,.zshrc但没有运气。

仅供参考:它运行良好.bashrc

这是.zshrc脚本:

#Check if ampps is running
bash ~/ampps_runner.sh & disown

不同的方法:

#Check if ampps is running
sh ~/ampps_runner.sh & disown

另一种方法:

#Check if ampps is running
% ~/ampps_runner.sh & disown

上述所有方法都不起作用(这意味着它应该运行一个名为 ampps 的应用程序,但它不在 zsh.

注意:在从 bash 切换到 zsh 之前它工作正常。所以它没有权限或语法问题。

更新: ampps_runner.sh 的内容

#! /usr/bin/env

echo "########################"
echo "Checking for ampps server to be running:"


check=$(pgrep -f "/usr/local/ampps" )

#[  -z "$check" ] && echo "Empty: Yes" || echo "Empty: No"

if [ -z "$check" ]; then
    echo "It's not running!"
    cd /usr/local/ampps
    echo password | sudo -S ./Ampps
else
    echo "It's running ..."
fi

标签: linuxbashzsh

解决方案


从终端bash临时运行的最简单方法是zsh

exec bash 

要不就

bash 

然后您可以运行以前只能在 bash 中运行的命令。一个例子

help exec

退出

exit 

现在你回到了原来的外壳

如果你想知道你的默认 shell

echo $SHELL 

或者

set | grep SHELL=

如果你想可靠地知道你当前的 shell

ps -p $$

或者,如果您只想要您可能使用的 shell 名称

ps -p $$ | awk "NR==2" | awk '{ print $4 }' | tr -d '-'

而且您可能只是将最后一个放在一个函数中以备后用,只要知道它只有在它来自当前 shell 时才可用。

whichShell(){
   local defaultShell=$(echo $SHELL | tr -d '/bin/')
   echo "Default: $defaultShell" 

   local currentShell=$(ps -p $$ | awk "NR==2" | awk '{ print $4 }' | tr -d '-')
   echo "Current: $currentShell"
}

调用方法查看结果

whichShell 

推荐阅读