Lucid Machinery A blog by Eric Slenk

Menu
  • Home
  • Projects
  • Resume
  • Wishlist
  • Archives
  • Tags
  • RSS
July 18, 2020

Bash Tip: Read From File or STDIN

# Usage:
#   script.sh foo.txt > foo_but_better.txt
#   cat bar.txt | tr '[:lower:]' '[:upper:]' | script.sh > BAR_BUT_BETTER.txt
  
cat ${1:--} \
  | sed -e 's/pipe/line/g'

Sometimes I write a bash script that needs to play nicely in the middle of a pipeline, but it would also be nice to use it at the front of the pipeline or on its own. This snippet says, "cat whatever the first parameter is, or if there's no first parameter, then cat the content of STDIN".

DONE.


Tags: bash stdin file


« Getting Started with the NodeMcu, ESP8266, and arduino-cli Installing Bar-End Shifters »

Copyright © 2024 Eric Slenk

Powered by Cryogen