Last active
October 13, 2017 03:14
-
-
Save takaram/d86d7bc06dad8d7d75dbbb9b50cbc950 to your computer and use it in GitHub Desktop.
show the contents of multiple files with file names
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
#!/bin/bash | |
set -eu | |
: ${FILENAME_COLOR:="\e[35m"} | |
if [ -t 1 ]; then | |
color=$FILENAME_COLOR | |
else | |
color= | |
fi | |
for opt in "$@"; do | |
case "$opt" in | |
--color) | |
color=$FILENAME_COLOR | |
shift | |
;; | |
--color=*) | |
coloropt="${opt#--color=}" | |
case "$coloropt" in | |
yes | true) | |
color="$FILENAME_COLOR" | |
;; | |
no | none | false) | |
color="" | |
;; | |
*) | |
echo "wrong option for color" 1>&2 | |
exit 1 | |
;; | |
esac | |
shift | |
;; | |
--) | |
param+=("$@") | |
break | |
;; | |
*) | |
param+=("$1") | |
shift | |
;; | |
esac | |
done | |
set -- "${param[@]}" | |
while [ "$#" -gt 0 ]; do | |
if [ ! -e "$1" ]; then | |
echo "$1: no such file or directory" 1>&2 | |
exit 1 | |
fi | |
printf "$color$1:\e[m\n" | |
cat "$1" | |
shift | |
[ "$#" -gt 0 ] && echo | |
done |
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
#!/usr/bin/env ruby | |
# ruby版。ライブラリのおかげでbash版よりかなり短く書ける | |
require 'optparse' | |
FILENAME_COLOR = (ENV["FILENAME_COLOR"] || "\e[35m").sub(/\\e/, "\e") | |
color = $stdout.tty? ? FILENAME_COLOR : "" | |
parser = OptionParser.new | |
parser.on("--color [VAL]", TrueClass) {|col| color = col ? FILENAME_COLOR : ""} | |
parser.parse!(ARGV) | |
last_index = ARGV.size - 1 | |
ARGV.each_with_index do |file, index| | |
unless File.exists?(file) | |
$stderr.puts "#{file}: no such file or directory" | |
exit 1 | |
end | |
puts "#{color}#{file}:\e[m" | |
print File.read(file) | |
puts if index < last_index | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment