1833877 Members
1584 Online
110063 Solutions
New Discussion

Scripting

 
SOLVED
Go to solution
SAM_24
Frequent Advisor

Scripting

Hi,

Can anyone explain the following code?

jobs -l | while IFS="${IFS}[]()" read w job w pid w rc w
do
((rc)) && { #particularly this line
print -u2 "Job failed"
print -u2 "PID=$pid RC=$rc"
}
done

Thanks.

Never quit
2 REPLIES 2
Tim Adamson_1
Honored Contributor

Re: Scripting

Hi,

The code is getting a list of background jobs and assigning the output to variables.

If the result of ((rc)) is true/successful, then it prints the message "Job failed" to stderr.

Refer to the sh-posix(1) man page and the Shell Users Guide for more information.


Tim.

Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
curt larson_1
Honored Contributor
Solution

Re: Scripting

jobs -l displays process id's after the job number, in addition to the usual information. it displays the job number in [], with a + in front of curent job number, and - in front of previous job number, the status (running, stopped, done, terminated, etc), a number in () after Done is the return value of the job. If there is no () after Done, the return value is true. and lastly the command line. the command line is obtained from the history file. if the history file can not be accessed then it does not display the command line and displays instead.

jobs -l
+[4] 139 running cc -c hello.c
-[3] 465 stopped mail jack
[2] 888 Done(1) rm junk
etc

| #pipeline command

the standard output of each command execpt the last one, is connected to the standard input of the next command. return value is the return value of the last specified command

the output of jobs is piped into a while do done loop.

ifs #internal field seperator
one of the many things the ifs is used for is to split into fields the characters the the shell reads with read.

the script is adding [] and () as field seperators for the following read.

uses the read to read a line and split it into fields, using the character in the IFS variable as delimiters. each variable name must be a vaild name. the first field is assigned to the first named var, the second to the second named var, etc.

((word)) #arithmetic command

the shell does command substitution, parameter expansion, arithmethic expansion, and quote removal for each word to generate an arithmetic expression that is evaluated. the return value is true if the arithmetic expression evaluates to nonzero; otherwise false

conditional commands
test-expression && test-expression
evaluates to true if both test-expressions are true. the second test-expression is expanded and evaluated only if the first expression is true

{ compound-list }
this is a brace grouping. the shell runs the compound-list in the current environment
the two print commands are group together so there are both evaluated if ((rc)) is true.

print -u2
prints the message to standard error instead of standard output