Created
November 23, 2019 03:04
-
-
Save helamonster/f4cf96363b68478ce57557580f0dd572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BASH Gist: | |
# Get the PID, standard out, standard error, and the exit code of a process | |
# Here we simulate a single program by running a subprocess with () in the background, | |
# just long enough to get the PID and start waiting on it to finish. | |
# The PID is nice to have available to query the status of the program or to send it a SIGNAL | |
# | |
# Step 1: Run the process in the background | |
# This set of commands simulates a single process that writes to stderr, stdout, and returns | |
# exit code 42 | |
# | |
# '>' redirects stadard out | |
# '2>' redirects standard err | |
# '&' puts the process in the background at the next step | |
# '#' | |
( echo Sleeping... ; sleep 10 ; echo "Good morning" >&2 ; exit 42) > stdout.txt 2> stderr.txt & | |
# Step 2: Collect PID, wait, and collect results | |
# This set of commands: | |
# * Uses the spcial variable $! to access the PID of the previous process placed in the background | |
# * Calls the 'wait' command with the PID as the argument, which in turn waits for the process with that PID to complete | |
# * Uses the special variable $? to access the exit code of the process | |
# * Uses 'wait' to get the PID and to wait for the process to complete | |
echo -e "\nPID : $!" ; wait $! ; echo "EXIT: $?" ; echo "Standatd Out:" ; cat stdout.txt ; echo "Standard Error:" ; cat stderr.txt ; | |
PID : 60974 | |
[1]+ Exit 42 ( echo Sleeping...; sleep 10; echo "Good morning" 1>&2; exit 42 ) > stdout.txt 2> stderr.txt | |
EXIT: 42 | |
Standatd Out: | |
Sleeping... | |
Standard Error: | |
Good morning | |
# Here's a more realistic looking example that captures a few other details of the process... | |
# Let's say we have a text file created with: | |
echo -e "one\ntwo\nThree\nfour\nFIVE\nsix\nSeven\neight\nine\nTeN" | |
# | |
# So, contents are... | |
Zero | |
One | |
Two | |
Three | |
Four | |
Five | |
Six | |
Seven | |
Eight | |
Nine | |
teN | |
# ...And we want to know how many files are in the system's | |
# home directories with the digital representation of the numbers as their file names... | |
# The same standard out and error redirection, plus | |
# * Save the exit code to a file | |
# * Save the command line to a file | |
# Let's pipe a few commands in order to: | |
# 1. Find all lines in the file containing the names of digits. | |
# 2. Sort that list so we can ... | |
# 3. ...Reduce that list to a list of unique digits found. | |
# 3. Then send those digits as arguments to 'xargs' ... | |
# 4. Which, in turn, executes a 'find' command to locate all files under the /home/ directory whose names match those digits | |
# In the process, it also: | |
# * Saves the search results to search-results.log | |
# * Saves any error messages to search-errors.log | |
# * Saves the exit codes of all progams in the pipe stream | |
# | |
sed 's/^Z.*/0/;s/^O.*/1/gI;s/^Tw.*/2/gI;s/^Th.*/3/gI;s/^Fo.*/4/gI;s/^Fi.*/5/gI;s/^Si.*/6/gI;s/^Se.*/7/gI;s/^E.*/8/gI;s/^n.*/9/gI;s/^te.*/10/gI' < NumberedFiles.txt | sort | uniq | xargs -n1 -I\; find /home/ -xdev -name \; > search-results.txt 2> search-errors.txt & | |
while ps -p $! >/dev/null ; do printf "%20s bytes and counting...\n" $(printf "%'d" $(stat search-results.txt --printf '%s')) ; sleep 1 ; done & | |
wait $! | |
PS="${PIPESTATUS[@]}" | |
echo !! > /var/run/program.cmdline | |
env > /var/run/program.env | |
echo $! > /var/run/program.pid | |
wait $! | |
echo $? > mysearch.exit | |
echo "Results: ${PIPESTATUS[@]}" >search-exit-codes.log | |
# Bonus: Match and get upper cased versions | |
while read NUM ; do echo "${NUM^^}" ; done<<<$(grep -iE '^z|^o|^tw|^th|^fo|^fi|^si|^se|^e|^ni|^te' < NumberedFiles.txt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment