Skip to content

Instantly share code, notes, and snippets.

@bf4
Last active February 12, 2016 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/d549b5a4be3466f9569c to your computer and use it in GitHub Desktop.
Save bf4/d549b5a4be3466f9569c to your computer and use it in GitHub Desktop.
pipes streams and echo codes
#!/usr/bin/env bash
# 2>&1 # stderr to stdout
# 1>&2 # stdout to stderr
# 2> # stderr
# > # stdout
# see http://www.tldp.org/LDP/abs/html/io-redirection.html
# exec 3>&1; bundle-audit -v >&3 3>&- | tee reports/bundle-audit.txt 3>&- ; exec 3>&-
# https://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status
# <command> | tee out.txt ; test "${PIPESTATUS[0]} -eq 0"
# ( set -o pipefail; command | tee out.txt ); ST=$?
# works with `sh`:
# mkfifo pipe
# tee out.txt < pipe &
# command > pipe
# echo $?
set -o pipefail
gem uninstall -aIx bundler-audit
gem install bundler-audit
bundle-audit check -v | tee reports/bundle-audit.txt
#!/usr/bin/env bash
# http://www.tldp.org/LDP/abs/html/io-redirection.html
# fd 0 stdin, 1 stdout, 2 stderr
# 1>&2 # stdout to stderr
# 2>&1 # stderr to stdout
# |& # was added to Bash 4 as an abbreviation for 2>&1
# &> # both stdout and stderr
# 2> filename # stderr to a file
# > filename # stdout to a file
# : > filename # truncate file to zero length or creates zero length (same as touch)
# 0< filename # accepts input from a file, e.g. grep search-word <filename
# < filename
# [j]<>filename # open file [rw] and assign fd j. create file if not exist. j is 0 (stdin)if not given.
# An application of this is writing at a specified place in a file.
# echo 1234567890 > File # Write string to "File".
# exec 3<> File # Open "File" and assign fd 3 to it.
# read -n 4 <&3 # Read only 4 characters.
# echo -n . >&3 # Write a decimal point there.
# exec 3>&- # Close fd 3.
# cat File # ==> 1234.67890
# # Random access, by golly.
# command < input-file > output-file # Multiple instances of input and output redirection and/or pipes can be combined in a single command line.
# # same as '< input-file command > output-file' # Although this is non-standard.
# n<&- Close input file descriptor n.
# 0<&-, <&- Close stdin.
# n>&- Close output file descriptor n.
# 1>&-, >&- Close stdout.
# | # Pipe. General purpose process and command chaining tool.
# Similar to ">", but more general in effect.
# Useful for chaining commands, scripts, files, and programs together.
# Child processes inherit open file descriptors.
# This is why pipes work.
# To prevent an fd from being inherited, close it.
# To learn more, see http://www.tldp.org/LDP/abs/html/ioredirintro.html
# http://www.tldp.org/LDP/abs/html/filearchiv.html#DERPM
exec 3>&1 # Saves current 'value' of stdout in fd 3
some_command 2>&1 # redirect stderr to stdout
>&3 # redirects, by default, fd 1 (stdout) to fd 3^
3>&- | # close fd 3^^, pipe does not inherit it,
grep ERROR # fd 3 is closed for 'grep' (but not 'some_command')
3>&- # close fd 3^ for 'some command'
exec 3>&- # Now close fd 3 for the remainder of the script
#!/usr/bin/env bash
npm prune
result=0
output=$(npm install --no-progress 2>&1);
if [ -n "$output" ]; then # only check if non-empty output
echo $output | grep -v 'requires a peer';
if [ "$?" -ne "0" ]; then # is non-zero when string is present
echo "Missing peer dependency: $output"
result=1
fi;
fi
exit $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment