This repository was archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateJavaClass.sh
More file actions
100 lines (44 loc) · 1.41 KB
/
Copy pathCreateJavaClass.sh
File metadata and controls
100 lines (44 loc) · 1.41 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
#
# A script to prebuild a Java class file
#
# Assume that this script accepts an argument, e.g.
# new_class ClassName
# and this argument becomes the name of the class
# First, let's make sure that the file name is not empty. If it is,
# display a diagnostic message and end the script.
if [ -z "$1" ]; then
echo "File name is empty! Script ends now."
exit 1
fi
if [[ $1 =~ @ ]]; then
echo "Character '@' is not allowed in the class name"
exit 2
fi
if [[ ! $1 =~ ^[A-Za-z] ]]; then
echo "$1 is not a valid Java class name"
exit 2
fi
# Now that we know the file name is not empty, assign to variable.
filename="$1.java" # The destination of the script's output
# Check if file already exists
if [ -f "$filename" ]; then
echo "File $filename already exists. Overwrite it? (y/n)"
read overwrite
if [ "$overwrite" == "n" ]; then
echo "OK, I will not overwrite the file. Script ends now."
exit 2
fi
fi
# Create a variable for the class name. This is the same as $1, so it is # not really necessary, but it makes the code more readable.
classname="$1" # Arguments passed are mapped to $1, $2, ...
cat << _EOF_ > $filename
/**
* Class $classname
*/
public class $classname {
public static void main(String[] args) {
} // method main
}
_EOF_
echo "File $filename successfully created."