Skip to content

Instantly share code, notes, and snippets.

@takaram
Last active October 13, 2017 03:14
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 takaram/d86d7bc06dad8d7d75dbbb9b50cbc950 to your computer and use it in GitHub Desktop.
Save takaram/d86d7bc06dad8d7d75dbbb9b50cbc950 to your computer and use it in GitHub Desktop.
show the contents of multiple files with file names
#!/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
#!/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