I came across the code below, which uses PIPE with multiple processes.
I'm new to shell scripting, and therefore don't fully understand the meaning/intent of the following code:
1 #!/bin/ksh
2
3 if [ $# -lt 1 ]; then
4 echo "Usage: $0 InputFile"
5 exit 1
6 fi
7
8 if [ "$SELF_LOGGING" != "1" ]; then
9 # The parent process will enter this branch and set up logging
10
11 # Create a named piped for logging the child's output
12 PIPE=temporary.fifo
13 mkfifo $PIPE
14
15 date_string=`date +"%m_%d_%Y_%H_%M_%S"`
16 log_name="${date_string}_createzip.log"
17
18 # Launch the child process without redirected to the named pipe
19 SELF_LOGGING=1 ksh "$0" "$date_string" "$@" 2>&1 >$PIPE &
20
21 # Save PID of child process
22 PID=$!
23
24 # Launch tee in a separate process
25 tee "$log_name" <$PIPE &
26
27 # Unlink $PIPE because the parent process no longer needs it
28 rm $PIPE
29
30 # Wait for child process running the rest of this script
31 wait $PID
32
33 # Return the error code from the child process
34 exit $?
35fi
36
37echo "Logging to file: $1_createzip.log"
38
39if [[ ! -r "$2" || ! -f "$2" ]]; then
40 echo "Error: cannot read input file. Exiting."
41 exit 1
42fi
43 ......MORE CODE.....
Questions:
Line 3: checks if there is fewer than 1 argument to the script, then print the usage?
Line 8: Is this check for parent process, the child process started in forthcoming lines of code, won't enter this
if
block ?Line 19: This line seems to launch a new child process. Is the child process same as parent process script
$0
? Is it that the statementSELF_LOGGING=1
, sets a new (environment?) variable, so that Line 8 part is skipped in the child process? How come the initial execution of the script (parent process) behaves correctly where this variable is not defined, I mean if the variable is not defined then how come theif
evaluates to true. Or is it that I've got this completely wrong.Line 28: why is
rm
done on PIPE.Line 39: Is this part for the child process or parent process? Input file seems to be at
$1
.
Would appreciate any response on the above. Thanks.