-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_bash_script.sh
executable file
·79 lines (62 loc) · 1011 Bytes
/
gen_bash_script.sh
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
76
77
78
79
#! /bin/bash
#
# gen_bash_script - generate bash script according a templete
#
# Usage:
# gen_bash_script NAME
#
# Arguments:
# NAME: begining with 'a-zA-Z' and '0-9a-zA-Z_'
#
PROGRAM=$(basename "$0")
usage ()
{
echo "usage: $PROGRAM NAME"
return 0
}
if echo $1 | grep -qvE '^[a-zA-Z_][a-zA-Z0-9_]*$'; then
echo "$PROGRAM: Unavailable name!"
usage
exit 1
fi
if [[ -e $1.sh ]]; then
echo "$PROGRAM: File exists!"
usage
exit 2
fi
# Generate script
cat << EOF > $1.sh
#! /bin/bash
#
# Automatically generated by gen_bash_script.sh
#
# $1 - TODO
#
$1 ()
{
# TAG
local TAG=\${TAG:-\${FUNCNAME[0]}}
# TODO
return \$?
}
# Script can be used as a program or a function
if echo \$0 | grep -qvE '^[-]?bash'; then
# Global VARs
PRO=\$(basename "\$0")
TAG=\$PRO
# Show usage
usage ()
{
cat << USAGE_END
usage: \$PRO
# TODO
Options:
Arguments:
USAGE_END
return 0
}
# Main
$1 "\$@"
fi
EOF
echo "$PROGRAM: Bash script $1.sh generated."