简体   繁体   English

Shell脚本shell在shell里面

[英]Shell scripting shell inside shell

I would like to connect to different shells (csh, ksh etc.,) and execute command inside each switched shell. 我想连接到不同的shell(csh,ksh等),并在每个切换的shell中执行命令。

Following is the sample program which reflects my intention: 以下是反映我意图的示例程序:

#!/bin/bash
echo $SHELL
csh
echo $SHELL
exit
ksh
echo $SHELL
exit

Since, i am not well versed with Shell scripting need a pointer on how to achieve this. 既然,我不熟悉Shell脚本需要一个如何实现这一点的指针。 Any help would be much appreciated. 任何帮助将非常感激。

If you want to execute only one single command, you can use the -c option 如果只想执行一个命令,可以使用-c选项

csh -c 'echo $SHELL'
ksh -c 'echo $SHELL'

If you want to execute several commands, or even a whole script in a child-shell, you can use the here-document feature of bash and use the -s (read commands from stdin ) on the child shells: 如果要在子shell中执行多个命令甚至整个脚本,可以使用bash的here-document功能并在子shell上使用-s (从stdin读取命令):

#!/bin/bash
echo "this is bash"

csh -s <<- EOF
    echo "here go the commands for csh"
    echo "and another one..."
EOF
echo "this is bash again"
ksh -s <<- EOF
    echo "and now, we're in ksh"
EOF

Note that you can't easily check the shell you are in by echo $SHELL , because the parent shell expands this variable to the text /././bash . 请注意,您无法通过echo $SHELL轻松检查您所在的shell,因为父shell将此变量扩展为文本/././bash If you want to be sure that the child shell works, you should check if a shell-specific syntax is working or not. 如果要确保子shell工作,则应检查特定于shell的语法是否有效。

It is possible to use the command line options provided by each shell to run a snippet of code. 可以使用每个shell提供的命令行选项来运行代码片段。

For example, for bash use the -c option: 例如,对于bash使用-c选项:

bash -c $code
bash -c 'echo hello'

zsh and fish also use the -c option. zshfish也使用-c选项。

Other shells will state the options they use in their man pages. 其他shell将声明他们在手册页中使用的选项。

You need to use the -c command line option if you want to pass commands on bash startup: 如果要在bash启动时传递命令,则需要使用-c命令行选项:

#!/bin/bash
# We are in bash already ...
echo $SHELL

csh -c 'echo $SHELL'
ksh -c 'echo $SHELL'

You can pass arbitrary complex scripts to a shell, using the -c option, as in 您可以使用-c选项将任意复杂脚本传递给shell,如下所示

sh -c 'echo This is the Bourne shell.'

You will save you a lot of headaches related to quotes and variable expansion if you wrap the call in a function reading the script on stdin as: 如果将函数包装在读取stdin上的脚本的函数中,则会为您节省很多与引号和变量扩展相关的麻烦:

execute_with_ksh()
{
  local script
  script=$(cat)
  ksh -c "${script}"
}

prepare_complicated_script()
{
   # Write shell script on stdout,
   #  for instance by cat-ting a here-document.
   cat <<'EOF'
echo ${SHELL}
EOF 
}

prepare_complicated_script | execute_with_ksh

The advantage of this method is that it easy to insert a tee in the pipe or to break the pipe to control the script being passed to the shell. 这种方法的优点是可以很容易地在管道中插入T形管或者破坏管道以控制传递给shell的脚本。

If you want to execute the script on a remote host through ssh you should consider encode your script in base 64 to transmit it safely to the remote shell. 如果要通过ssh在远程主机上执行脚本,则应考虑在base 64中对脚本进行编码,以将其安全地传输到远程shell。

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

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