stdout

How to write a Scala shell script that reads input from STDIN

As a quick note, if you need an example of how to write a Scala shell script that reads from STDIN (standard input) and writes to STDOUT (standard output), this code shows a solution:

#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#

import scala.io.StdIn

var line = ""
while ({line = StdIn.readLine(); line != null}) {
    println(line)
}
putStrLn doesn’t print to STDOUT ... it describes how to print to STDOUT alvin June 12, 2017 - 9:57am

putStrLn doesn’t print to standard out, it returns a value — of type IO () — which describes how to print to standard out, but stops short of actually doing it.”

From the article, An IO Monad for Cats.

Hints for writing Unix tools alvin January 31, 2017 - 1:34pm

Marius Eriksen has a good article titled Hints for writing Unix tools. Some key points: a) consume input from stdin, produce output to stdout; b) output should be free from headers or other decoration; c) output should be simple to parse and compose. There’s much more to it than that, and it’s a good read (or reminder).

What is the Unix/Linux “bit bucket”? alvin July 21, 2016 - 10:44am

Unix/Linux FAQ: What is the “bit bucket”?

The bit bucket is a way of referring /dev/null. Sending output to the /dev/null device file is like sending output directly to the trash. That’s why you see code like this a lot of times:

aCommand 2> /dev/null

That’s a way of saying, “Run the command aCommand and send it’s error output to the bit bucket.” In use like this, “error output” refers to STDERR, and redirecting STDERR to the bit bucket is the same as throwing it into the trash (or throwing it into a black hole, if you prefer).

Another way you can demonstrate this is by sending STDOUT to the bit bucket. In this next example, I send the output from the ls command to the bit bucket:

ls -l > /dev/null

If you run that command, you won’t see any output because the standard output — STDOUT — is redirected to /dev/null. There’s no practical reason for doing this in the real world; I just wanted to demonstrate that you can redirect both STDOUT and STDERR to the bit bucket, if you ever need to.

For more information, here’s a short tutorial on How to redirect Unix STDOUT and STDERR to the same location.

How to redirect the STDOUT and STDIN of external commands alvin June 19, 2015 - 11:22am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.15, “How to redirect the STDOUT and STDIN of external commands.”

Problem

You want to redirect the standard output (STDOUT) and standard input (STDIN) when running external commands. For instance, you may want to redirect STDOUT to log the output of an external command to a file.

Solution

Use #> to redirect STDOUT, and #< to redirect STDIN.

Scala: How to handle the STDOUT and STDERR of external commands alvin June 19, 2015 - 11:20am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.13, “How to handle the STDOUT and STDERR of external commands.”

Problem

You want to run an external command and get access to both its STDOUT and STDERR.

Solution

The simplest way to do this is to run your commands as shown in previous recipes, and then capture the output with a ProcessLogger. This Scala shell script demonstrates the approach:

Why curl defaults to stdout alvin November 17, 2014 - 12:42pm

This blog post on why curl defaults to stdout is an interesting discussion about decisions you have to make when designing things.

How to redirect Unix/Linux STDOUT and STDERR to the same file/location alvin March 14, 2011 - 12:43am

Unix/Linux redirection FAQ: How do I redirect Unix STDOUT and STDERR to the same file with one command?

To redirect both STDOUT and STDERR to the same file with one Unix/Linux command, use this syntax:

my-shell-script.sh > /dev/null 2>&1

As you can see in this command, I'm redirecting STDOUT to /dev/null as normal, and then the special 2>&1 syntax tells your Bash shell to redirect STDERR to STDOUT (which is already pointing to /dev/null).

Ruby stdout and stderr

Very cool, thanks to a comment from a reader I just learned about the Ruby $stdout and $stderr variables. In short, you can write to STDOUT and STDERR in Ruby using code like this:

$stdout.puts "stdout"
$stderr.puts "stderr"

If you put those two lines in a Ruby script, and then run the script like this:

ruby test.rb > stdout 2> stderr

you'll find that the stdout and stderr files will contain the two different strings.

How to run a sed command from the Linux command line alvin July 11, 2010 - 5:46pm

Linux sed FAQ: How do I run a Linux sed command from the command line?

I usually only use the Linux sed command in sed scripts, but today I needed to do something much easier than normal, and as I thought about how to run a sed command from the Linux command line, I had to pause for a few moments. Finally I remembered the sed command line syntax, and it looks like this:

sed 's/THE_DATE/2010-07-11/' < sitemap-orig.xml > sitemap.xml

This sed command can be read like this: