Jay Taylor's notes

back to listing index

Prefix to each output of a command on runtime

[web search]
Original source (superuser.com)
Tags: bash shell-scripting superuser.com
Clipped on: 2016-04-03

I am trying to make a modular script. I have several scripts/commands which are called from a single script.
I want to prefix the output of each separate command.

Examle:

My files are allcommands.sh / command1.sh / command2.sh

command1.sh outputs
file exists
file moved

command2.sh outputs
file copied
file emptied

allcommands.sh runs the scripts command1.sh and command2.sh

I want to prefix each output of these two scripts like this:
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied

asked Jul 23 '13 at 10:58
Image (Asset 2/12) alt=
Ivan Dokov
1315
   upvote
  flag
Try running each command piping it through sed "s/\^/command1 /" – j_kubik Jul 23 '13 at 11:08
   upvote
  flag
Give me please an example with the information that I give. I don't really understand the sed functionality. I'm sorry. – Ivan Dokov Jul 23 '13 at 11:18
up vote 10 down vote accepted

I assume that what you are doing in your allcommands.sh is:

command1.sh
command2.sh

Just relace it with

command1.sh | sed "s/^/[command1] /"
command2.sh | sed "s/^/[command2] /"
answered Jul 23 '13 at 11:29
Image (Asset 3/12) alt=
j_kubik
22218
   upvote
  flag
This works perfectly for my case. Thanks! – Ivan Dokov Jul 23 '13 at 11:38
   upvote
  flag
+1 for simplicity. – Daniel Andersson Jul 23 '13 at 11:41

A minimal example of allcommands.sh:

#!/bin/bash
for i in command{1,2}.sh; do
    ./"$i" | sed 's/^/['"${i%.sh}"'] /'
done

With command1.sh and command2.sh executable and in the same directory just echoing the wanted strings, this gives the shell output:

$ ./command1.sh 
file exists
file moved
$ ./command2.sh 
file copied
file emptied
$ ./allcommands.sh 
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied

Quick sed breakdown

sed 's/^/['"${i%.sh}"'] /'
  • s/ enters "regexp pattern match and replace" mode
  • ^/ means "match the beginning of every line"
  • ${i%.sh} happens in the shell context and means "$i, but strip the suffix .sh"
  • ['"${i%.sh}"'] / at first prints a [, then exits the quoted context to grab the $i variable from the shell, then re-enters to finish with the ] and a space.
answered Jul 23 '13 at 11:28
Image (Asset 4/12) alt=
Daniel Andersson
14.9k12540
   upvote
  flag
Thanks for the clarifications. Your answer was helpful indeed, but @j_kubik's example was just the one I need. – Ivan Dokov Jul 23 '13 at 11:39

Your Answer

asked

2 years ago

viewed

1979 times

active

2 years ago

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.4.1.3424