1

I want to type a script into something like a heredoc and execute it without writing to a file first.

I use a Teensy3.2 microcontroller with an attached sdcard to automatically type out some repetitive tasks (like a USB macro keyboard).

Why? I have computers on which permanent file system changes/mass storage devices are not permitted and I have repetitive tasks that benefit from shell scripting.

But, I find this so very useful that I would like to load unmodified shell scripts onto the SDcard and execute them. The teensy can only type them out (no mass storage). Typing them out into a file requires them to be saved to the filesystem and execute permission granted before they can be executed and ultimately deleted.

I'm not real happy with this. It seems a bit hacky and I don't like automating file deletions with a "keyboard macro".

I've tried ssh << EOF etc... // but different computers require different passwords that aren't stored in the (unmodified) scripts and ssh does prompt for passwords and I am not permitted to change this configuration.

I've also tried exec << EOF etc... but I don't understand the behavior since it kills my shell when finished (which scares me).

Basically I want to:

Keyboard.print("exec << 'EOF'");
while (sdcardScriptFile.available()) {
  Keyboard.write(sdcardScriptFile.read());
}
Keyboard.print("EOF");

Would it be better to just open a subshell and type out each line of the script? If so, would I just:

Keyboard.print("(");
while (sdcardScriptFile.available()) {
  Keyboard.write(sdcardScriptFile.read());
}
Keyboard.print(")");

I would like to be able to add scripts to the SDcard and have them available to be typed and executed "on the fly" without making changes to the filesystem.

  • `sh -s < – Biffen Jun 28 '19 at 06:40
  • One thing I do is have my script in a file on my PC. I open it in an editor, select all, copy and paste it in the terminal window. I have never seen a bash script fail because of that. But I am not sure if that is what you want. – Nic3500 Jun 28 '19 at 16:27

1 Answers1

1

If it needs to be run in the current shell process, you could source it.

Keyboard.print(". /dev/fd/0 << 'EOF'");
while (sdcardScriptFile.available()) {
  Keyboard.write(sdcardScriptFile.read());
}
Keyboard.print("EOF");
Dabombber
  • 436
  • 3
  • 8