1

I have a text file that has over a hundred lines, each of which is an sh command:

echo foo
...
echo bar

I'm looking for an sh command that I can invoke from a terminal which will run the command from said file associated with a specified line number. I do not want the terminal to show the usual system-related content that is normally associated with commands run in a terminal.

For example, if I use something like xdotool type "$(sed -n 1p file)" & (note that I'm open to a more elegant command),

I get the following terminal output:

[1] 9531
user@host:~$ echo foo

I wish to suppress the [1] 9531 line. Please keep in mind, however, that I do not wish to suppress the echo bar line, as that is where I have a chance to verify that I'm about to execute the desired command and not something else. Nor do I wish for any text to be displayed to the left of the command prompt.

I then hit the <Enter> key of my keyboard and get:

foo
[1]+  Done                    xdotool type "$(sed -n 1p file)" &
user@host:~$

I wish to suppress the [1]+ Done... line.

How can I suppress the terminal output of said two lines? I've tried every redirect scheme I can find, with no joy.

5
  • @Quasímodo I have a long, manual procedure that I run about every two years or so. Scattered throughout said procedure, I have about a hundred or so bash commands (some quite lengthy) that are run from a terminal emulator. Instead of typing in a particular command when it is called for, I have copied all of said commands to a text file. I then just have to run the xdotool... command as shown in the OP, changing just the line number (goes a lot quicker). The output I want to suppress just gets in the way when I am running said procedure...
    – Digger
    Commented Nov 27, 2020 at 22:00
  • So it seems you want to run commands from some lines of the text file? If yes, can you update your question with that?
    – Quasímodo
    Commented Nov 27, 2020 at 22:03
  • @Quasímodo That is correct.
    – Digger
    Commented Nov 27, 2020 at 22:06
  • @Quasímodo I definitely want a chance to check each particular command before executing same (I have added a sentence to the OP to emphasize this). This answer in your link does a pretty nice job, but with an extra key stroke I'd like to avoid...
    – Digger
    Commented Nov 27, 2020 at 22:39
  • Let us continue this discussion in chat.
    – Quasímodo
    Commented Nov 27, 2020 at 23:06

1 Answer 1

5

Just to simplify, what you are seeing is the normal output when you run a command in the background (& symbol). One can see the same if one runs this simplified version:

ls &

To avoid seeing that output, you can sorround your command with parenthesis:

(ls &)

Or, in your case

(xdotool type "$(sed -n 1p file)" &)

In your particular case, it doesn't seem you really need the &, so it may be easier to remove it from your command:

xdotool type "$(sed -n 1p file)"

PS

If you want to run all the file sequentially from beginning to end, you might prefer doing something like

x=0

and then, for each line, run

x=$((x+1)); (xdotool type "$(sed -n "${x}p" file)" &)

This way you don't need to modify the number each time.

Also have a look at this: How to interactively run all commands in a file

8
  • That works well!
    – Digger
    Commented Nov 28, 2020 at 4:17
  • Actually, I'll keep the "&", since it prevents the command from being repeated to the left of the command prompt...
    – Digger
    Commented Nov 28, 2020 at 4:56
  • To summarize, the best solution for me (so far) is: (xdotool type "$(sed -n 1p file)" &)
    – Digger
    Commented Nov 28, 2020 at 14:43
  • Suggestions? I think you're correct, but I'm struggling with a suitable title...
    – Digger
    Commented Nov 28, 2020 at 15:46
  • To summarize, the best solution for me (so far) is: (x=1; xdotool type "$(sed -n "${x}p" file)" &).
    – Digger
    Commented Nov 28, 2020 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .