简体   繁体   English

交互式 Bash Shell (TTY) 从 Bash Function

[英]Interactive Bash Shell (TTY) from Bash Function

So there are commands like docker exec -t , docker-compose exec $CONTAINER /bin/bash , kubectl exec $POD_NAME -it /bin/bash that I use to connect to an instance with a TTY that basically engages an interactive shell instance from my local terminal.所以有像docker exec -tdocker-compose exec $CONTAINER /bin/bashkubectl exec $POD_NAME -it /bin/bash这样的命令,我用来连接到一个带有 TTY 的实例,该 TTY 基本上与我本地的交互式 shell 实例终端。

I had build aliases around these or other scripts, but I'm interested in turning these into functions now.我已经围绕这些或其他脚本构建了别名,但我现在有兴趣将它们转换为函数。

So previously, I might have had:所以以前,我可能有:

alias container=`...; docker-compose exec $CONTAINER /bin/bash;` 

Where ... is an arbitrary set of other bash commands necessary for the last statement.其中...是最后一条语句所需的任意一组其他 bash 命令。 This successfully engaged the shell.这成功地吸引了 shell。

Now, I have:我现在有:

function container {
    ...
    docker-compose exec $CONTAINER /bin/bash
}

Which seems to hang in a loop.这似乎挂在一个循环中。 I'm a beginner to understanding Linux/Unix systems.我是了解 Linux/Unix 系统的初学者。 Could someone explain: - How I can contain this within a multiline function in my bash_profile as opposed to an alias?有人可以解释一下: - 我如何将它包含在我的 bash_profile 的多行bash_profile中而不是别名中? - What exactly is going on when I execute these commands? - 当我执行这些命令时到底发生了什么? I understand an alias is only a stored value...how does that affect the behavior here?我知道别名只是一个存储值......这对这里的行为有何影响?

I'm not sure what code you have in the ... it might be what causes the error.我不确定你有什么代码...这可能是导致错误的原因。
But her what i have done and it worked.但是她做了我所做的并且它奏效了。

Here is what i changed:这是我改变的:
I used docker instead of docker-compose .我使用docker而不是docker-compose
I added -it flags.我添加了-it标志。
Also added CONTAINER=$1 to get the container name as parameter.还添加了CONTAINER=$1以获取容器名称作为参数。

function container {                        
        CONTAINER=$1                        
        echo "Your code here"                     
        docker exec -it $CONTAINER /bin/bash
}                                           

Just put it in .bashrc or .bash_profile and it works.只需将它放在.bashrc.bash_profile中即可。
As far as i understand .bash_profile and .bashrc are just shell script that get sourced into current session any time you open new shell.据我所知, .bash_profile.bashrc只是 shell 脚本,每当您打开新的 shell 时,这些脚本就会获取当前的 session。
So Any function you put inside will be available as if you run it in regular script.因此,您放入其中的任何 function 都将可用,就像您在常规脚本中运行它一样。

You can also source more files inside them to keep them clean and make your custom commands and functions more portable.您还可以在其中获取更多文件以保持它们的整洁,并使您的自定义命令和函数更具可移植性。
Like this: .bash_profile像这样: .bash_profile

# dotfile dir is actually a git repo that i clone to any new machine i use.
# And Add this line only to my .bash_profile
if [ -f ~/dotfiles/.bash_customize ]; then
    . ~/dotfiles/.bash_customize
fi

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

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