简体   繁体   English

无法在 ZSH 中运行 bash 文件

[英]Can't run bash file inside ZSH

I've placed a bash file inside .zshrc and tried all different ways to run it every time I open a new terminal window or source .zshrc but no luck.我在 .zshrc 中放置了一个.zshrc文件,并在每次打开新终端.zshrc或源 .zshrc 时尝试了所有不同的方法来运行它,但没有运气。

FYI: it was working fine on .bashrc仅供参考:它在.bashrc上运行良好

here is .zshrc script:这是.zshrc脚本:

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

Different approach:不同的方法:

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

Another approach:另一种方法:

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

All the above approaches didn't work (meaning it supposes to run an app named ampps but it doesn't in zsh.上述所有方法都不起作用(这意味着它应该运行一个名为 ampps 的应用程序,但它不在 zsh.

Note: It was working fine before switching to zsh from bash.注意:在从 bash 切换到 zsh 之前,它工作正常。 so it does not have permission or syntax problems.所以它没有权限或语法问题。

Update: content of ampps_runner.sh更新: 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

(1) I believe ~/.ampps_runner.sh is a bash script, so, its first line should be (1) 我相信~/.ampps_runner.sh是一个bash脚本,所以,它的第一行应该是

#!/bin/bash

or或者

#!/usr/bin/bash

not不是

#! /usr/bin/env

(2) Then, the call in zsh script ( ~/.zshrc ) should be: (2) 那么, zsh脚本 ( ~/.zshrc ) 中的调用应该是:

~/ampps_runner.sh

(3) Note: ~/.ampps_runner.sh should be executable . (3) 注意: ~/.ampps_runner.sh应该是executable的。 Change it to executable:将其更改为可执行文件:

$ chmod +x ~/ampps_runner.sh

The easiest way to run bash temporarily from a zsh terminal is tozsh终端临时运行bash的最简单方法是

exec bash 

or just要不就

bash 

Then you can run commands you previously could only run in bash.然后您可以运行以前只能在 bash 中运行的命令。 An example一个例子

help exec

To exit退出

exit 

Now you are back in your original shell现在您回到原来的 shell

If you want to know your default shell如果你想知道你的默认 shell

echo $SHELL 

or或者

set | grep SHELL=

If you want to reliably know your current shell如果您想可靠地知道您当前的 shell

ps -p $$

Or if you want just the shell name you might use或者,如果您只想要 shell 名称,您可以使用

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

And you might just put that last one in a function for later, just know that it is only available if it was sourced in a current shell.你可能只是把最后一个放在 function 中供以后使用,只要知道它只有在当前 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"
}

Call the method to see your results调用方法查看结果

whichShell 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM