Last active
December 14, 2015 21:29
-
-
Save kunst1080/5151980 to your computer and use it in GitHub Desktop.
シェルスクリプトで、複数コマンドの実行結果を結合(AND OR XOR)するシェル関数
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 | |
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} | |
} |
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 | |
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