2

In expect it is possible to spawn a process like,

spawn /usr/bin/sftp "$uri" /home/user/dest

Writing it like this makes things really simple, it will be a one liner. However, I am trying to replicate this for upload. This is a bit more complicated,

spawn /usr/bin/sftp "$uri" <<< $'put "$source"'

Most likely the issue is that this is bash code and not a single command. Is there a way to spawn something like this in expect, or will I have to do it the long way? I also tried with braces, to no success.

BR Patrik

patrik
  • 165
  • How are you running this? in a bash script or an expect script> – Inian Sep 04 '19 at 08:40
  • @Inian Running pure expect, but it will always be run from a bash shell. – patrik Sep 04 '19 at 08:50
  • I've always found perl's Expect.pm module (and the various protocol-specific modules like Net::SSH, Net::Telnet, and Net::SFTP, etc) to be a lot more useful and easier to work with than expect itself. Amongst other benefits, there's no need for messing around with shell quoting or escaping, just use perl vars where they're needed. Python has similar capabilities with, e.g., the pexpect library. – cas Sep 05 '19 at 00:50
  • Also, I think it's a waste of time learning a single-purpose language like expect, especially when you'll use it only rarely. IMO that time is better spent learning or improving your skill with a general purpose language like perl or python, which you'll use far more often for many and varied tasks. – cas Sep 05 '19 at 00:53
  • @cas not sure if "improving in perl" is completely accurate. I managed to get fairly fluent in perl in about 3 weeks (though this knowledge may not extend to big perl apps, if there ever is a point for these). Anyway, expect is very useful for short scripts where you just want to connect to a remote system, do just two things and terminate. I cannot say my expect skills are the result of more than about 8 hours of learning and this is sufficient for almost anything I use expect for. – patrik Sep 05 '19 at 07:24
  • any time you use a language, there's a chance you'll get better at it. I've been writing perl and awk and other stuff since the 90s and I still get better at them. familiarity breeds proficiency :). I learned expect in the early 90s too (there wasn't any alternative other than write-your-own back then). I've needed to use expect or something expect-like perhaps 5 or 10 (definitely less than 20) times since then. For me, learning expect was a waste of time, I just don't use it often enough. I use the other languages daily or near-daily - well worth getting more practice as often as i can. – cas Sep 05 '19 at 07:38
  • I learnt tcl too. I don't think I've used it at all since around '97 (tcl + tk was, at the time, looking like it was going to be a good, quick way to write GUI apps or front-ends on linux). There's nothing i can do in tcl that I can't do easier or better in perl or python. I know some people still love it, but for me it's effectively a dead language. – cas Sep 05 '19 at 07:41

1 Answers1

0

Well, this is embarrasing. As soon as I formulated the questions the solution began to clarify. Of course it is possible to spawn a bash shell and run whatever commands I want there,

spawn /bin/bash
send "/usr/bin/sftp \"$uri\" <<< $\'put \"$source\"\'\r"

Anyway, for all it's worth there might be more people running into this problem so I will post a solution to this instead of removing the post.

patrik
  • 165
  • Note, you can also use {...} to quote a string and it will not need any special backslash escapes (except for nested {}). See the section [6] Braces in tcl – meuh Sep 04 '19 at 09:36