-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_script
More file actions
executable file
·73 lines (61 loc) · 1.11 KB
/
new_script
File metadata and controls
executable file
·73 lines (61 loc) · 1.11 KB
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
#!/usr/bin/env bash
usage="NAME
new_script - create new script in tools dir and edit it in vim
SYNOPSIS
new_script [-h] -n|--name
ARGUMENTS
-n name (required)
Name of the script you would like to create
Example:
./new_script -n my_script
-p path (optional) (default: $HOME/.local/bin/tools)
Location to store path in. Pass in '.' for current directory
"
while test $# -gt 0; do
case "$1" in
-h | --help | -\?)
echo "$usage" >&2
exit
;;
-n | --name)
name=$2
shift
;;
-p | --path)
if [[ "${2}" == "." ]]; then
path=$PWD
else
path=$2
fi
shift
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
*)
break
;;
esac
shift
done
if [[ -z "${name}" ]]; then
echo "${usage}"
exit 1
fi
if [[ -z "${path}" ]]; then
path=$HOME/code/scripts
fi
full=${path}/$name
if [[ -f "${full}" ]]; then
echo "Error: ${name} already exists in ${path}"
echo "Edit $name? y/n"
read answer
if [[ "${answer}" != "n" ]]; then
$EDITOR "${full}"
fi
exit 0
fi
echo "#!/usr/bin/env bash" >"${full}"
chmod +x "${full}"
$EDITOR "${full}"