格式化预计在bash脚本用户命令的脚本中 [英] Formatting Expect Script Inside of User Command in Bash Script

查看:164
本文介绍了格式化预计在bash脚本用户命令的脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我对于NVIDIA的CUDA API 3安装程序 - 首先是一个驱动器,并配有漂亮的无提示安装标志选项(但你必须要根都已经运行3级)

So I have three installers for NVIDIA's CUDA API -- the first is a driver and comes with nice silent install flag options (but you have to be root and have to have run level 3).

后两个后续被示出手动下面安装(切出安装之后为简洁长乱七八糟)

The second two follow are shown manually installing below (cut out the long mess of install afterwards for brevity)

[根]#SH cudatoolkit_4.1.28_linux_64_rhel5.x.run验证存档
  诚信...都不错。 Uncom pressing NVIDIA
  CUDA.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

  进入安装路径(默认是/ usr /本地/ CUDA,'/ CUDA'将是
  追加):
CUDA的previous版本在/ usr /本地/ CUDA / bin中
被发现
  你想卸载? (是/否/中止):是的。

[root]# sh cudatoolkit_4.1.28_linux_64_rhel5.x.run Verifying archive integrity... All good. Uncompressing NVIDIA CUDA.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Enter install path (default /usr/local/cuda, '/cuda' will be appended):
A previous version of CUDA was found in /usr/local/cuda/bin
Would you like to uninstall? (yes/no/abort): yes

在换句话说,我需要认识到:
进入安装路径和输出的'\\ n'

In other words, I need to recognize: "Enter install path" and output a '\n'

现在棘手的部分是卸载可能不会永远存在。如果不是我需要简单地等待安装完成,但如果我看到你想卸载吗?我需要输出是来完成的。

Now the tricky part is the uninstall may not be always be there. If it's not I need to simply wait for the install to finish, but if I see "Would you like to uninstall?" I need to output "yes" to complete.

第三和最终安装的输出如下所示....

The third and final installer's output is shown below....

[根]#SH gpucomputingsdk_4.1.28_linux.run
验证存档
  诚信...都不错。
Uncom pressing NVIDIA GPU计算
  

SDK............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

  
输入安装路径(默认〜/ NVIDIA_GPU_Computing_SDK):
  在/ usr /本地/ CUDA_SDK

位于CUDA位于/ usr /本地/ CUDA
如果
  这是正确的,选择默认的下方。
如果它是不正确的,
  输入正确的路径,CUDA

输入CUDA安装路径
  (默认是/ usr /本地/ CUDA):

[root]# sh gpucomputingsdk_4.1.28_linux.run
Verifying archive integrity... All good.
Uncompressing NVIDIA GPU Computing

SDK............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Enter install path (default ~/NVIDIA_GPU_Computing_SDK): /usr/local/CUDA_SDK

Located CUDA at /usr/local/cuda
If this is correct, choose the default below.
If it is not correct, enter the correct path to CUDA

Enter CUDA install path (default /usr/local/cuda):

有关这一项,没有卸载动作,所以它看似简单一点。

For this one, there is no uninstall action so it's seemingly a bit simpler.

我只需要检测进入安装路径和输出在/ usr /本地/ CUDA_SDK \\ n,然后检测进入CUDA安装路径输出\\ n

I just need to detect "Enter install path" and output "/usr/local/CUDA_SDK\n" and then detect "Enter CUDA install path" and output "\n"

我的想法是用一双期待的脚本 - 每个安装程序 - 但由于命令的双引号内嵌套切换到root,我有这个有些困难。我目前拥有的是:

My idea was to use a pair of expect scripts -- one for each installer -- but due to the nesting within the double quotes of the command to switch to root, I'm having some difficulties with this. What I currently have is:

#!/bin/bash
CR="\"\n\""
YES="\"Yes\""
INSTALL_PATH_REQUEST="\"Enter install path\""
CUDA_PATH_REQUEST="\"Enter CUDA install path\""
UNINSTALL_REQUEST="\"Would you like to uninstall?\""
TOOLKIT=`ls -t cudatoolkit* | head -n 1`
TOOLKIT_EXPECT="sh $TOOLKIT"
SDK=`ls -t gpucomputingsdk* | head -n 1`
SDK_INSTALL_PATH="\"/usr/local/CUDA_SDK\n\""
SDK_EXPECT="sh $SDK"
/bin/su root -c "yum -q -y install expect expectk;
/sbin/init 3; sh `ls -t NVIDIA*|head -n 1` -s --update -a -X;
/usr/bin/expect <<EOF;
spawn $TOOLKIT_EXPECT
expect $INSTALL_PATH_REQUEST
send $CR
expect $UNINSTALL_REQUEST
send $YES
EOF
/usr/bin/expect <<EOF;
spawn $SDK_EXPECT
expect $INSTALL_PATH_REQUEST
send $SDK_INSTALL_PATH
expect $CUDA_PATH_REQUEST
send $CR
EOF
/sbin/init 5"

这将切换到适当的根(一旦输入了密码),并与内置的选项正确安装驱动程序。然后它会显示产卵第二安装进程,并进入第一个参数(回车),但似乎退出第二安装prematurely(例如我没有看到是选项)。

This switches to root properly (once the password is entered) and installs the driver with the built in options correctly. It then appears to spawn the second install process and enter the first argument (a carriage return), but seems to exit the second installer prematurely (e.g. I don't see the "yes" option.).

我觉得我pretty密切,希望有人能指点我要去哪里错了,并提出正确的语法。

I feel like I'm pretty close, hopefully somebody can point me to where I'm going wrong and suggest the correct syntax.

注意:
我加了百胜安装命令,如一些我安装的机器没有想到(股份的CentOS 6 ),以便有拯救我的麻烦.. ..

NOTES: I added the yum install command, as some of the machines I'm installing on didn't have expect (stock CentOS 6), so that saves me the trouble there....

推荐答案

可能对于超时的问题在这里...不知道安装程序需要多长时间。结果
默认的期望超时时间为10秒,如果没有看到当时的预期的文本,它会继续不分,你可以改变像这样的超时值:

Might be an issue with timeout here... not sure how long the installer takes.
The default expect timeout is 10 seconds, if it doesn't see the expected text in that time, it will proceed regardless, you could change the timeout values like so:

expect -timeout 100 $INSTALL_PATH_REQUEST

另外从

YES="\"Yes\""

要:

YES="\"Yes\r\""

(最好使用\\ r而非\\ n的$ CR太)

(Best to use \r instead of \n in $CR too)

这也是期待一些安全弦在结束一个不错的主意安装,例如:

It's also a good idea to expect some 'safety string' at the end of the install, for example:

 expect -timeout 320 "Install Complete."

因此​​,预计脚本不会终止之前产生的进程,就完成了。

So the expect script doesn't terminate before the spawned process is complete.

这篇关于格式化预计在bash脚本用户命令的脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆