Last active
December 25, 2015 21:39
-
-
Save leesei/7044050 to your computer and use it in GitHub Desktop.
#bash #openN open the N-th line of piped input
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 | |
[[ ${COLORS_SOURCE} ]] && source ${COLORS_SOURCE} | |
EXEC_CMD= | |
SCRIPT=$(basename $0) | |
_BSCRIPT=${_BLD}${SCRIPT}${_RST_} | |
function _help() | |
{ | |
echo -e | |
echo -e "${_BSCRIPT} - open the N-th line from stream" | |
echo -e | |
echo -e "${_BU}Synopsis${_RST_}" | |
echo -e | |
echo -e " ${_BSCRIPT}" | |
echo -e " ${_BSCRIPT} --help" | |
echo -e " Display this help" | |
echo -e | |
echo -e " ${_UND}cmd${_RST_} | ${_BSCRIPT}" | |
echo -e " ${_BSCRIPT} < ${_UND}file${_RST_}" | |
echo -e " Prefix line number to each line (equivalent of \`cat -n\`)" | |
echo -e | |
echo -e " ${_UND}cmd${_RST_} | ${_BSCRIPT} [${_UND}option${_RST_}] ${_UND}N${_RST_}" | |
echo -e " ${_BSCRIPT} [${_UND}option${_RST_}] ${_UND}N${_RST_} < ${_UND}file${_RST_}" | |
echo -e " Opens the ${_UND}N${_RST_}-th line of input" | |
echo -e | |
echo -e "${_BU}Supported options${_RST_}" | |
echo -e | |
echo -e " -c, --exec ${_UND}EXEC_CMD${_RST_}:" | |
echo -e " Specify the command to be executed on ITEM" | |
echo -e | |
echo -e "${_BLD}Author:${_RST_} ${_UND}leesei@gmail.com${_RST_} ${_BLD}Licence:${_RST_} MIT" | |
echo -e "${_UND}https://gist.github.com/leesei/7044050${_RST_}" | |
echo -e | |
# error messages | |
if [[ $# -ne 0 ]]; then | |
echo # seperator | |
while [ -n "$*" ]; do | |
echo -e ${RED}"$1"${_RST_} | |
shift | |
done | |
echo | |
fi | |
} | |
if [ "$(tty)" != 'not a tty' ]; then | |
_help | |
exit | |
fi | |
case $(uname) in | |
# choose open command | |
Darwin) OPEN_CMD=open ;; | |
Linux) OPEN_CMD=xdg-open ;; # assuming X is present | |
CYGWIN*) OPEN_CMD=cygstart ;; | |
*) echo "Unknown OS"; exit 1 ;; | |
esac | |
# Parse arguments | |
TEMP=$(getopt -n ${SCRIPT} -o c:h \ | |
--long exec,\ | |
help \ | |
-- "$@") | |
if [ $? -ne 0 ]; then | |
_help "getopt error" | |
exit 2 | |
fi | |
eval set -- "$TEMP" | |
while true; do | |
case $1 in | |
-c|--exec) | |
EXEC_CMD="$2"; shift; | |
shift; continue | |
;; | |
-h|--help) | |
_help | |
exit 0 | |
;; | |
--) | |
# no more arguments to parse | |
break | |
;; | |
*) | |
printf "Unknown option %s\n" "$1" | |
exit 1 | |
;; | |
esac | |
done | |
# Remove the switches (including the added "--") we parsed above | |
shift `expr $OPTIND` | |
if [ $# -eq 0 ]; then | |
cat -n - | |
exit 0 | |
fi | |
# select the n-th line | |
ITEM=$(cat - | sed "$1q;d") | |
if [[ ${EXEC_CMD} ]]; then | |
echo "Exec \"${EXEC_CMD} ${ITEM}\"..." | |
${EXEC_CMD} "${ITEM}" | |
else | |
echo "Opening \"${ITEM}\"..." | |
# quite the output | |
${OPEN_CMD} "${ITEM}" > /dev/null 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment