-
Notifications
You must be signed in to change notification settings - Fork 1
/
pick
executable file
·75 lines (70 loc) · 1.04 KB
/
pick
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
# pick: select arguments
# this file in public domain
quoting_style="${QUOTING_STYLE:-"literal"}"
delimiter='
'
usage() {
echo "usage: pick [-bnq] [-d delimiter] arg..." >&2
exit 1
}
pick() {
# print prompt to terminal and read response from terminal
printf "%s? " "$1" >/dev/tty
read -r response </dev/tty
case "$response" in
y*|s*)
if [ "$quoting_style" = "escape" ]
then
printf "%s" "$1" | sed "s/\\([ ]\\)/\\\\\\1/g; s/'/\\\\'/g"
elif [ "$quoting_style" = "shell" ]
then
printf "'"
printf "%s" "$1" | sed "s/'/'\\\\''/g"
printf "'"
else
printf "%s" "$1" | sed "s/'/\\\\'/g"
fi
printf "%s" "$delimiter"
;;
q*)
exit 0
;;
esac
}
while getopts 'bd:qn' c
do
case "$c" in
b)
quoting_style="escape"
;;
d)
delimiter="$OPTARG"
;;
q)
quoting_style="shell"
;;
n)
delimiter=" "
;;
*)
usage
;;
esac
done
shift $((OPTIND -1))
if test "$#" -eq 0
then
usage
elif test "$#" -eq 1 && test "$1" = '-'
then
while read -r arg
do
pick "$arg"
done
else
for arg
do
pick "$arg"
done
fi