-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateFile.sh
More file actions
executable file
·52 lines (44 loc) · 1.38 KB
/
createFile.sh
File metadata and controls
executable file
·52 lines (44 loc) · 1.38 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
# !/bin/bash
# a bash file to generate empty file
# cmd is ./createFile.sh {questionNumber} {filename} {extension}
# or ./createFile.sh {questionNumber} {extension}
extension=' c cpp java py sh js sql '
if [[ ! $1 =~ ^[0-9]+$ ]]; then
echo Error: \"$1\" is not a valid number
exit 1
fi
if [[ ! $extension =~ (.* ${@: -1} .*) ]]; then
echo Error: \"${@: -1} \" is not a valid extension
exit 1
fi
INDEX=$(($1-1)) # questions 1-10 have index 0-9
START100=$(($INDEX/100*100+1)) # directory start_index
START10=$(($INDEX/10*10+1))
DIRECTORY=`find $START100-*/$START10-*/* -name "$1-*"`
if [[ $# == 2 ]]; then
if [[ -z $DIRECTORY ]]; then
echo Error: \"$1\" does not exist in directory
else
FILENAME=${DIRECTORY##*/$1-}
FILEPATH=$DIRECTORY/$FILENAME.$2
if [[ ! -e $FILEPATH ]]; then
touch "$FILENAME.$2"
mv "$FILENAME.$2" $DIRECTORY
echo Question \"$1-$FILENAME.$2\" created in Directory \"$DIRECTORY\"
else
echo Question \"$1-$FILENAME\" has $2 version existed
fi
fi
elif [[ $# == 3 ]]; then
FILENAME="$2.$3"
if [[ ! -e $FILENAME ]]; then
touch $FILENAME
echo create File \"$FILENAME\"
fi
if [[ -z $DIRECTORY ]]; then
DIRECTORY=$1-$2
mkdir $DIRECTORY
echo creating Directory \"$DIRECTORY\"
fi
mv $FILENAME $DIRECTORY
fi