Can I outupt a .sh script to a file?

Xarzu

New Member
Joined
Apr 29, 2022
Messages
16
Reaction score
4
Credits
200
I just ran a .sh prebuild file that built a lot of stuff. There were some highlighted issues that came across the screen in bold. I wish I could have saved all that data to a file. Preferably, it would be great to ouptut it to HTML so showcase the bold and hilighted warnings. In Windows command-line it is done by simpuly using the '>' character to putut a comandline entry to an output file.
 


It's the same in Linux - and >> is an option.

See:


I can't say that I've tried it with a .sh file - and it certainly won't be HTML formatted. But, yeah, you can use both > and >> to write the output to a text file.
 
Technically speaking, this output redirection is a unix/linux feature, that windows eventually implemented, so the more correct way to say it is, oh, windows does a standard unix/linux thing, that's surprising! There's a ton of stuff you can do with redirection, it's a common way to do simple and advanced things easily on the command line.
 
I just ran a .sh prebuild file that built a lot of stuff. There were some highlighted issues that came across the screen in bold. I wish I could have saved all that data to a file. Preferably, it would be great to ouptut it to HTML so showcase the bold and hilighted warnings. In Windows command-line it is done by simpuly using the '>' character to putut a comandline entry to an output file.
As detailed in the linked source from @KGIII's blog post, do not forget to redirect also the standard error.

Code:
# To a different file:
$ command>stdout.txt 2>stderr.txt

# To the same file:
$ command>combined.txt 2>&1

The problem is the following: the console will capture both standard and output errors, but when redirecting them away you need to specify what to do with each one; otherwise you'll miss information in the file and you'll see a few lines popping out to the console, here and there.

The solution is the above code, which reads as follows:
  1. 1 is the standard output stream and it's the default when redirecting: you don't need to specify it upfront if you redirect it first (*)
  2. 2 is the standard error stream.
  3. When you said command>stdout.txt 2>stderr.txt, you're saying "Redirect standard output to stdout.txt and output 2 (error) to stderr.txt".
  4. When you said command>combined.txt 2>&1 you are saying "Redirect standard output to stdout.txt and add output 2 (error) to the same redirection as output 1 (standard)", meaning it will combine the two streams of info in the same file.
(*) you could have written command 2>stderr.txt 1>stdout.txt, though!
 
Also, in order to have the output as HTML, you could pipe the output from your script to something like ansi2html.

If memory serves, ansi2html is a Python module. But Python should already be pre-installed on all Linux distros. Installing the ansi2html module can be done via your usual package manager, or via pip (pip install ansi2html)

And to run it:
Bash:
/path/to/somescript.sh | ansi2html > /path/to/output.html
That will run a script and pipe its output to ansi2html and then redirect ansi2html’s output to a html file.

Where /path/to/somescript.sh is the fully qualified/absolute path to the script you want to run.
If you’re running a more localised script in the current directory, you could use a relative path, e.g.:
./somescript.sh

And where /path/to/output.html is the absolute path/filename of the output file you want to create. Again, you don’t have to use an absolute path, you could use a relative path.
E.g.
./output.html, or ../subdir/output.html

You don’t have to run a script. You could also create a bash one-liner that pipes data to several different commands and then finally pipes all of that output to ansi2html to generate an HTML file.

Also, it’s worth running ansi2html —help in order to see what additional parameters are available. I haven’t used it in a long while, but there are quite a few options available to format the output.

But using ansi2html without parameters will create a complete, default html page containing whatever input it receives. And it will preserve any coloured/formatted output.
 

Members online


Top