views:

4441

answers:

2

What I want to do is the following:

  1. read in multiple line input from stdin into variable A
  2. make various operations on A
  3. pipe A without loosing delimiter symbols (\n,\r,\t,etc) to another command

The current problem is that, I can't read it in with read command, because it stops reading at newline.

I can read stdin with cat, like this:

my_var=`cat /dev/stdin`

, but then I don't know how to print it. So that the newline, tab, and other delimiters are still there.

My sample script looks like this:

#!/usr/local/bin/bash

A=`cat /dev/stdin`

if [ ${#A} -eq 0 ]; then
        exit 0
else
        cat ${A} | /usr/local/sbin/nextcommand
fi
+3  A: 

This is working for me:

myvar=`cat`

echo "$myvar"

The quotes around $myvar are important.

Tanktalus
Should read myvar=$(cat); echo "${myvar}" -- but otherwise my solution exactly.
gnud
+1  A: 

Yes it works for me too. Thanks.

myvar=`cat`

is the same as

myvar=`cat /dev/stdin`

Well yes. From the bash man page

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, , \, and, when history expansion is enabled, !. The characters $ and retain their special meaning within double quotes.