3

How is it possible to execute a shell script without creating a file? For example assume I have the following script (testscript):

#!/bin/bash
function run_free() {
   free -m
}

run_free

I then of course can execute this with: sh testscript

I want to ovoid creating a file though. I tried:

sh echo '#!/bin/bash function run_free() { free -m } run_free'

Which did not work. Any ideas?

Justin
  • 5,668

5 Answers5

11

Most interpreters have a parameter to specify some code to execute. You can use this to invoke a specific interpreter and provide the code. For example:

bash -c 'function run_free() { free -m; }; run_free'

(Note that you need some semicolons in there since you don't have newlines.)

perl uses -e, and python uses -c.

mgorven
  • 31,399
2

You can use here documents to feed SSH scripts:

ssh -T myhost <<EOF
hostname
whoami
uptime
EOF

The '-T' option disables TTY allocation.

I use this technique to run a standard script across multiple user accounts on one of our 3rd-party services which doesn't allow for a commonly-accessible writable bin directory. The SSH here-document method obviates the need to copy (and maintain) the same script across multiple accounts.

1

Yo dawg, I heard you like shell, so I put shell in your shell so can shell while you shell.

In other words, you are spawning a shell for your echo command and then just echoing everything in one line. Shell interprets that as just an echo command. Even if it did not, the fact that everything is in one line (especially that there is no new line after #!/bin/bash) causes problems.

While typing this reply, I found out from your comment you are going to run commands over ssh. Then something like this should work:

ssh your_user@yourserver "function run_free() { free -m } run_free"
0

If I understood question correctly, you were very close, but sh echo '#!/bin/bash input string in the beginning was unnecessary.

I've tried your code in Ubuntu 20.04, bash version 5.0.17. All I've done is just started to print function in terminal:

enter image description here

@mgorven answer is OK, but doesn't allow multi-line code, that can be inconvenient enough, if code lines are long.

0

You could create the scripts on the ssh originating system and pipe them into the ssh session

ssh user@somehost.tld /bin/bash <local.script

Putting your example in a local script run_free

ssh somehost.tld /bin/bash <run_free
Pseudo-terminal will not be allocated because stdin is not a terminal.
         total       used       free     shared    buffers     cached
Mem:           371        246        124          0         49         80
-/+ buffers/cache:        116        254
Swap:          767          0        767

runs the local script on the remote host.

You can do the same thing with perl

ssh user@somehost.tld /usr/bin/perl <perl.script
user9517
  • 117,122