简体   繁体   中英

using multiple command interpreters in a linux shell script

I want to use a single shell script to perform a bunch of commands. Some of these commands include: scp, cd, tar, date, expect, spawn.

When I declare #!/bin/sh or #!/bin/bash none of the expect commands work- this is understandable. When I declare #!/usr/bin/expect -f then cd, tar, date don't work- I suppose this is to be expected (no pun) as well.

Is it possible to use all these commands using a single interpreter or can I declare another along the way? I dove into sub shells but that didn't work out.

You can feed commands to expect via a shell here-doc:

#!/bin/sh
# shell code....
expect <<EOS
# expect code...
EOS
# back to shell code...

Actually if you run under expect, you have all the commands of Tcl available and therefore:

  • "cd" will work because it's built-in
  • "date" is not needed because you can use the Tcl clock command, eg. clock format [clock seconds]
  • "tar" and other external commands can be run with exec, eg. exec tar cf stuff.tar file1 file2 file3

The data and control constructs are different from sh or bash, but more powerful once you get used to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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