Redirection Inside Your Scripts
Naturally, you can use redirection inside your scripts, too.
You can now stick debugging statements into your scripts. (I'm not just trying to show you features of the shell, I'm trying to help you learn how to program in it.)
But now that you're thinking carefully, think about this: there is a Catch-22 looming.
Suppose you're developing a script by letting the ouput go to your screen. Now you redirect output to a file. Something mysterious happens and the output file's suddenly full of garbage instead of what you expect. (This has happened to you with other programming languages; it will happen to you in the shell, too.)
How do you debug it? Where's your debugging output? In the same pile of garbage.
This sets me up for what I'll talk about tomorrow.
Sometimes, when they're just starting out, people get confused about the difference between output produced by a script and output produced by commands inside a script. Snarf-n-barf this into a script:#!/bin/bash
# The shell lets you redirect output
echo hello, world > /tmp/$$ # "$$" is the process id of this script, just like Perl
cat /tmp/$$
rm -f /tmp/$$ # when you make temporary files, clean up after yourself
Run it, then think about what happened for a second.#!/bin/bash
echo "the 'hello world' step comes next"
echo hello, world > X
echo "the 'hello world' step is now done"
You can now stick debugging statements into your scripts. (I'm not just trying to show you features of the shell, I'm trying to help you learn how to program in it.)
But now that you're thinking carefully, think about this: there is a Catch-22 looming.
Suppose you're developing a script by letting the ouput go to your screen. Now you redirect output to a file. Something mysterious happens and the output file's suddenly full of garbage instead of what you expect. (This has happened to you with other programming languages; it will happen to you in the shell, too.)
How do you debug it? Where's your debugging output? In the same pile of garbage.
This sets me up for what I'll talk about tomorrow.
0 Comments:
Post a Comment
<< Home