Bash, execute command but continue with interactive session

a 夏天 提交于 2019-12-12 10:35:26

问题


I want to create an alias for pagsh that will immediately get me the admin kerberos ticket.

The problem is that I can't figure out how to specify a command for the bash to run, but still continue with the interactive session after the command is done.

My current shot is:

alias admin=pagsh -c "bash -c \"kinit xtoth1@ADMIN.META\""

but bash logically ends right after kinit is done. How can I push a custom command into a begging of an interactive session of bash? I still need to run .bashrc normally, therefore I can't use --rcfile


回答1:


My advice would be using a custom bashrc file with --rcfile that sources your .bashrc, ex :

alias admin=pagsh -c "bash --rcfile myrc"

myrc :

source ~/.bashrc
kinit xtoth1@ADMIN.META



回答2:


If what you need is only a line or two, you can use the cool bash feature of "Process Substitution"[1] to provide it right on the invocation line. e.g. (to run a bash in a specific python virtualenv):

bash --rcfile <(echo '. ./pyvenv/bin/activate')

multiple lines are not a problem, nor is using vars or helpers:

bash --rcfile <(echo echo "Starting at `date`"; echo cd $HOME)

[1] man bash and look for "Process Substitution". Note that this is not supported on all systems, but it should work on all Linux.




回答3:


What about following?

alias admin='pagsh -c "bash -c \"kinit xtoth1@ADMIN.META; exec bash\""'

or even

alias admin='pagsh -c "kinit xtoth1@ADMIN.META; exec bash"'

might work. (I do not have working openafs environment around to test it properly.)



来源:https://stackoverflow.com/questions/4584736/bash-execute-command-but-continue-with-interactive-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!