Posts Tagged ‘bash’

EOF

Wednesday, July 1st, 2009

Since I’m always forgetting the specific syntax (which is another issue, as it’s quite straight-forward), I figured I’d write it down:

cat > /tmp/test << "EOF"
this is a test
it does nothing useful
simply to demonstrate and document
EOF

This is handy when you want to bung a configuration file in a shell script and easily write it out to the filesystem. An alternative (but much less succinct way) would be to do:

FILE=/tmp/test
echo "this is a test" > $FILE
echo "it does nothing useful" >> $FILE
echo "simply to demonstrate and document" >> $FILE

bash

Tuesday, May 12th, 2009

So, here was my problem:

$ cat test.txt
this is a test
        this is another test
$

Now, if I wanted to read this file line-by-line:

$ for line in `cat test.txt`; do echo $line; done
this
is
a
test
this
is
another
test
$

Well, that sucks.

$ while read line; do echo $line; done < test.txt
this is a test
this is another test
$

Better, but what happened to the spacing at the beginning of the second line?

$ while read; do echo "$REPLY"; done < test.txt
this is a test
        this is another test
$

The latter solution was found here.

bash sequences

Monday, June 16th, 2008

I always did them this way:

for x in `seq 1 10`; do
  echo $x
done

However, you can actually do them this away also:

for x in {1..10}; do
  echo $x
done

You just never know when seq’s going to magically disappear and you need some critical sequences!