1 Star 0 Fork 3

朱良才/useful-scripts

forked from _KK/useful-scripts 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
xpl 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
oldratlee 提交于 2019-02-28 19:32 . extract functon
#!/bin/bash
# @Function
# Open file in file explorer.
#
# @Usage
# $ ./xpf [file [file ...] ]
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#-xpl-and-xpf
# @author Jerry Lee (oldratlee at gmail dot com)
PROG=`basename $0`
usage() {
[ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout
[ $# -gt 1 ] && { echo "$2"; echo; } > $out
cat <<EOF
Usage: ${PROG} [OPTION] [FILE]...
Open file in file explorer.
Example: ${PROG} file.txt
Options:
-s, --selected select the file or dir
-h, --help display this help and exit
EOF
exit $1
}
# if program name is xpf, set option selected!
[ "xpf" == "${PROG}" ] && selected=true
################################################################################
# parse options
################################################################################
declare -a args=()
while [ $# -gt 0 ]; do
case "$1" in
-s|--selected)
selected=true
shift
;;
-h|--help)
usage
;;
--)
shift
args=("${args[@]}" "$@")
break
;;
-*)
usage 2 "${PROG}: unrecognized option '$1'"
;;
*)
args=("${args[@]}" "$1")
shift
;;
esac
done
################################################################################
# biz options
################################################################################
# open one file
openOneFile() {
local file="$1"
case "$(uname)" in
Darwin*)
[ -f "${file}" ] && selected=true
open ${selected:+-R} "$file"
;;
CYGWIN*)
[ -f "${file}" ] && selected=true
explorer ${selected:+/select,} "$(cygpath -w "${file}")"
;;
*)
if [ -d "${file}" ] ; then
nautilus "$(dirname "${file}")"
else
if [ -z "${selected}" ] ; then
nautilus "$(dirname "${file}")"
else
nautilus "${file}"
fi
fi
;;
esac
}
[ "${#args[@]}" == 0 ] && files=( . ) || files=( "${args[@]}" )
for file in "${files[@]}" ; do
[ ! -e "$file" ] && { echo "$file not exsited!"; continue; }
openOneFile "$file"
echo "$file opened${selected:+ with selection}!"
done
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhuliangcai/useful-scripts.git
git@gitee.com:zhuliangcai/useful-scripts.git
zhuliangcai
useful-scripts
useful-scripts
master

搜索帮助