Skip to content

Instantly share code, notes, and snippets.

@kunst1080
Last active December 14, 2015 21:29
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 kunst1080/5151980 to your computer and use it in GitHub Desktop.
Save kunst1080/5151980 to your computer and use it in GitHub Desktop.
シェルスクリプトで、複数コマンドの実行結果を結合(AND OR XOR)するシェル関数
#!/bin/bash
soniq(){
sort | uniq $*
}
concat_commands() {
# $1 … method ( and | or | xor )
# $2 … command
# $3 … command
local _arg=""
if [ "$1" = "and" ] ; then
_arg="-d"
fi
if [ "$1" = "xor" ] ; then
_arg="-u"
fi
((eval "$2" | soniq) && (eval "$3" | soniq)) | soniq ${_arg}
}
#!/bin/bash
source `dirname $0`/def-concat_commands.sh
# Simplae Test
cat > test1.txt <<++EOD
test1
test2
test3
test4
++EOD
cat > test2.txt <<++EOD
test2
test4
test5
test6
++EOD
echo [--- TEST DATA ---]
echo [IN1]
cat test1.txt
echo
echo [IN2]
cat test2.txt
echo
# ①論理和
echo [--- OR ---]
concat_commands or "cat test1.txt" "cat test2.txt"
echo count:`concat_commands or "cat test1.txt" "cat test2.txt" | wc -l`
echo
# ②論理積
echo [--- AND ---]
concat_commands and "cat test1.txt" "cat test2.txt"
echo count:`concat_commands and "cat test1.txt" "cat test2.txt" | wc -l`
echo
# ③排他的論理和
echo [--- XOR ---]
concat_commands xor "cat test1.txt" "cat test2.txt"
echo count:`concat_commands xor "cat test1.txt" "cat test2.txt" | wc -l`
echo
# ④左側ONLY
echo [--- IN1 ONLY ---]
concat_commands xor "cat test1.txt" 'concat_commands and "cat test1.txt" "cat test2.txt"'
echo count:`concat_commands xor "cat test1.txt" 'concat_commands and "cat test1.txt" "cat test2.txt"' | wc -l`
echo
rm test1.txt test2.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment