1

It's not easy to formulate the question properly, maybe it helps when I describe what I'd like to do.

I want to execute a command and pipe it's output into a tool called pastebinit which uploads the STDOUT output to pastebin.

That works very well, however I would like to send the command itself on top of it but w/o typing it a second time.

Is there some command I can launch "my command" with that will

  1. Print "my command" on STDOUT
  2. Executes "my command"

I have the feeling that something like that exists but as hard as it is to formulate such a question properly, I was not able to dig it up with google so far.

hakre
  • 156
  • 1
  • 13

4 Answers4

3

You can easily write a small wrapper script to do this:

#!/bin/bash

# Start a subshell
(

# Print the command to standard out
echo "Command: $@"
echo

# Run the command as well
$@

# End the subshell, and pipe all standard output from it to pastebinit
) | pastebinit

Save this file and chmod +x it, then use the script you've created to run the command and pastebinit.

For example:

./run_and_pastebin.sh ls -la /root
Kyle Smith
  • 9,563
  • 1
  • 30
  • 32
  • That looks great, will test it right away. I smelled I need a script for it, probably I call it echoexec. – hakre Jun 09 '11 at 18:55
  • Works like a charm. I've left the pipe after the subshell out so I can pipe to everwhere I want to. Thanks a lot, exactly what I was looking for, any nice that you added comments into the script. – hakre Jun 09 '11 at 19:01
  • okay, well I didn't test more than `ls -l` the problem starts with using `'` or `"` for parameters. I assume the shell is removing those. As I execute parameters that need whitespaces and dollar signs preserved in it I think this needs some escaping / handling. However your base script works, I'll read some tutorials. -- **update:** just `"$@"` and it works, see http://serverfault.com/questions/269592/bash-quotes-getting-stripped-when-a-command-is-passed-as-argument-to-a-function – hakre Jun 09 '11 at 19:47
  • Ah, good point! :) Glad it helped. – Kyle Smith Jun 09 '11 at 21:23
  • yeah, thanks for the support. I could not fully solve the problem with this, but I could use it mainly for what I needed it. An older system with the x-server crashed and I wanted to write some docs w/o copying both the commandline and then the output per test. Put it later on my blog. – hakre Jun 09 '11 at 21:36
2

Try out the command tee. This command reads from an input and then write to a standard output and file.

Example:
cat mytext.txt | tee pastebininit

This will send the text of mytext.txt to standard out and to the command pastebininit.

Chris Ting
  • 899
  • 4
  • 5
0

A command to run shell commands is sh (the shell), it can be asked to echo the commands to stderr with the -x switch. You can redirect stderr to stdout then.

$ sh -x -c "echo my command" 2>&1
+ echo my command
my command

The side effect of this solution is that the command ('echo my command' in this example) stderr is also sent to stdout. This may be or may not be a problem for you. Also the '+ ' prefix is added to the command.

The '-x' switch will also work for whole scripts, echoing each of the shell commands executed.

Jacek Konieczny
  • 3,597
  • 2
  • 21
  • 22
0

You can have a look at my answer on another similar question:

https://serverfault.com/a/454535/146493

I made there a script that can show and execute almost "any" command (ie, the rest of the line is executed "as is", even if it include pipes, complex parameters, quotes, etc. It's not ok for multiline as-is, but could be made so with a few additions, such as a terminating string (to know where the commands to be executed ends), and a little awk script that copy lines from the lineno until the terminating string and put it in the tmp file to be sourced. (see the script for details)

Olivier Dulac
  • 1,202
  • 7
  • 14