1 perl 调用 shell: system or ``(反斜杠)
2 shell 调用 perl:http://www.cnblogs.com/finallyliuyu/archive/2012/07/19/2598930.html
3 tcl 调用 shell: exec shell
tcl 中run shell脚本用exec ;这个命令超级好用
exec echo "rcc build exit in IH" | /bin/mailx -r 123@126.com -s "rcc build exit" 123@126.com 2>/dev/null
这种都可以
运行tcl文件 source.tcl中的[]相当于shell中的``。表示执行
set a [exec uname -a ]
4 c 调用 shell ,tcl or perl
4.1 system(/opt/cool/tools/bin/inject_wise)
(execl(curl_nvm_sim.pl, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, 0)
4.2
c 语言调用shell有两种方法:一个是system 一个是popen/pclose
system: system("killall klogd");
popen: 对执行过程可以控制
if (NULL == (fp = popen(sCmd.c_str(), "r")))
{
......
}
while (fgets(line, sizeof(line), fp))
{
output += line_ptr;
output += "\n";
}
5 expect 调用shell
shell在expect中很受限。 什么空格等格式一定要精确,否则就不能执行(管道|不能和command中间注意空格)
example 1:
use exec to run shell
[] to 执行 to get output
set a [ exec ls -ltr | head -2 | tail -1 ]
puts "a=$a"
#check lognmake.out results:RET=0 or RET=-1
set result [ exec tail -1 lognmake.out ]
puts "result=$result"
example 2: shell command can;t run well
exec ksh "cd \"$ROOT/cool/lib/apsparc\";rm -f *.so"
exec cd $ROOT/cool/lib/apsparc
When you call ‘exec‘ you are creating a sub-shell. When the exec’d program finishes, the sub-shell is terminated and you return to expect.
So ‘exec cd …’ is completely useless. The sub-shell changes directory (but not the parent), then exits. So the change of directory is lost.
This might have worked: exec “cd ….; rm –f ….” because the cd and the rm would both be in the same sub-shell.
特别注意:这样执行是local 的lognmake.out。不是remote server 的lognmake.out
send "cd $ROOT/abc\r"
expect {
timeout {puts "timeout\r"}
"$ps1"
]
set result [ exec tail -1 lognmake.out ]
example3 :
http://forum.ubuntu.org.cn/viewtopic.php?p=109814
麻雀岁五脏俱全,expect 调用shell的框架是对的
shell 调用expect 解析。
以here文档的方式将expect脚本,送给expect解析
在expect的脚本中调用talk.sh shell
要交互的脚本(talk.sh)如下:
#!/bin/bash
echo "Who are you?"
read who
echo "Hello,$who"
echo "Are you happy?"
read answer
echo "why?"
read answe
实现自动应答的脚本auto.sh如下:
#!/bin/bash
expect<<- END
spawn ./talk.sh
expect "who"
send "firefly\n"
expect "happy?"
send "Yes,I am happy.\n"
expect "why?"
send "Because it worked!\n"
expect eof
exit
END
执行auto.sh后可以看到自动交互如下:
spawn ./talk.sh
Who are you?
firefly
Hello,firefly
Are you happy?
Yes,I am happy.
why?
Because it worked!
目前只用到了expect最基本的用法,不过对用脚本实现自动化已经很有用了