I would like to get more details on what the below command is doing:
PASSING=0 FAILED=0 > log_${ENV} print "Test Started at `date`\n"
I would like to get more details on what the below command is doing:
PASSING=0 FAILED=0 > log_${ENV} print "Test Started at `date`\n"
The command may be rewritten as
PASSING=0 FAILED=0 print "Test Started at `date`\n" >log_${ENV}
as the redirection may occur at any point on the command line.
The redirection sends the output of the print
command to the file named log_${ENV}
(or log_$ENV
, the curly braces won't do anything here), where ENV
is supposedly a shell variable with some value that exists in the current environment.
The two assignments to PASSING
and FAILED
will set these two variables in the environment of the print
command. They will not exist after the execution of this line (unless they existed before in the script, in which case their original values would remain unmodified after execution of this command line).
Since print
does not seem like it's using these two variables (unless it's a script or a function or something else that we don't know about), it's difficult to say what the intention is with the variables.
The print
thing takes a text string as its only argument, where a part of the string is an old-styled command substitution that will expand to the output of the date
command before print
is invoked. Using a more modern command substitution syntax, the argument could also be written as "Test Started at $(date)\n"
.
The overall effect would probably (depending on what print
does) be to write the given text string into a specific file whose name depends on the value $ENV
.
Related:
In ksh93
(and zsh
), print
is a shell built-in utility that acts (loosely speaking) a bit like echo
. That shell also treats $ENV
in a special way in that it will source the file that $ENV
points to when starting an interactive shell. Using it (or upper-case variables in general) as a generic variable in a shell script is not advised.
Related to this:
You have to "parse" (interpret) the command line (divide into words):
PASSING=0 FAILED=0 >log_${ENV} print "Test Started at `date`\n"
\___ __/ \__ __/ \___ ____/ \_ _/ \_________ ____________/
\/ \/ \/ \/ \/
var1 var2 redirect cmd "Argument to command".
Those are the five words (divided on spaces) that make up the command line.
A var before a command, like in var=123 cmd
will be defined in the environment for (only) the duration of the command execution. Then, it will be discarded.
Several variables could be defined before the command name.
A redirection is used to send output to a file or read input from a file. A redirection could appear at any place of the command line and will be applied for the whole command line (command execution).
In this case, the redirection is to output to a file called as the concatenation of the word log_
and the value inside the variable ENV
(the $(ENV)
part).
Obviously, the command name print
in this case.
This command name exists (as builtin) in ksh, mksh,lksh and zsh, but not in ash, dash, csh, yash, bash (and others). In those shells, the command may exist as an external program to be found in the PATH.
Argument(s) is the list of parameters, text, or other information that is given to the command and available in c programs as argv[]
and int argc
or in shell as $@
and $#
.
In this case, the argument include one command expansion `date`
, equivalent to (in modern syntax and strongly recommended) $(date)
. That part will execute the command (in this case) date
and will replace the whole part with the output of the command. In short: will place the actual date at that place.
The whole command could also be written (to the exact same effect) as:
PASSING=0 FAILED=0 print "Test Started at `date`\n" >log_${ENV}
Which, not having enough information, seems to be printing one string into a log file. If it is indeed the built-in print mentioned above, the two variables will have no effect and could be erased to write the command as :
print "Test Started at `date`\n" >log_${ENV}
But that is just an educated guess, to actually know what is the print command doing, you need to dig a little deeper. You need to say which shell is running the command and (if valid for your shell), the output of type print
.
ksh93
? – Kusalananda Oct 10 '18 at 12:05print
may be a function, and that function usesPASSING
orFAILED
. – muru Oct 11 '18 at 07:30