I am trying to understand Ethan's shell script from this post, used to discover the maximum and minimum Process IDs on an OS.
pid=0
for i in {1..100000}; do
: &
if [ $! -lt $pid ]; then
echo "Min pid: $!"
echo "Max pid: $pid"
break
fi
pid=$!
done
I know that $ substitutes a shell parameter, and ! is the parameter storing the PID of the "most recently executed background (asynchronous) command." Further, & is a control operator when used at the end of a command: whatever precedes is called in a background process. Thus, opening a new shell instance and running : &, followed by $!, we get:
% : &
[1] <PID>
[1] + done :
% $!
<PID>
where <PID> varies each call. What does if [ $! -lt $pid ]; then do? Why does the script successfully vary from the minimum to the maximum PID (i.e, consistently outputs 100 and 99998)? What is the final pid=$! for? Where does the counter i come into the script, if at all (other than repeating 100,000 times)?
echo {1..10}, runfor i in {1..10}; do echo $i; doneto get the basics of the loop.-ltbeing passed into$!. that said, i did need to learn about test constructs and their bracketing tokens.