-
Notifications
You must be signed in to change notification settings - Fork 1
/
cRun
executable file
·119 lines (100 loc) · 2 KB
/
cRun
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
#to kill child process on ctrl+c
trap "kill 0" SIGINT
srcHash=0
inpHash=1
tempInpHash=1
tempSrcHash=1
inputFile=""
file=""
extension=""
#holds pid of childprocess which runs the executable file
outputChild=""
#
runExecutable () {
rerun=""
cat $inputFile | "./$fileName"
echo
echo "(Type rs to rerun)"
read rerun </dev/tty
if [ "$rerun" == "rs" ]
then
reset
runExecutable
fi
}
#
file="$1"
if [ -z "$file" ]
then
echo "Please specify source file.Error Exiting..."
exit
fi
if [ ! -e "$file" ]
then
echo "Source file doesn't exist.Error Exiting..."
exit
fi
#handling fileExtension for make
fileName=`echo $file | cut -d "." -f 1`
extension=`echo $file | cut -d "." -f 2`
#check if extension is present, second check is required if no '.' is present
if [ -z "$extension" ] || [ "$extension" == "$fileName" ]
then
echo "No file extension present?Exiting..."
exit
fi
#iterate through arguments and set options
while [ -n "$2" ]
do
case $2 in
"-i")
shift
if [ -e "$2" ]
then
inputFile="$2"
else
echo "The input file doesn't exist.Exiting.."
exit
fi
;;
esac
shift
done
while [ true ]
do
#tempSrcHash and temoInpHash to hold the intermediate hash to compare with previous hash of the file to detect change in src and input
tempSrcHash=`shasum $file`
if [ ! -z "$inputFile" ]
then
tempInpHash=`shasum $inputFile`
fi
#checks for change either in source or input files
if [ "$srcHash" != "$tempSrcHash" ] || [ "$inpHash" != "$tempInpHash" ]
then
if [ ! -z "$outputChild" ]
then
kill "$outputChild"
outputChild=""
fi
#clears screen for output
reset
#remove output file , if present
rm $fileName
#compiles according to fileExtension
make $fileName
echo "Compiled at `date`"
#if no input file is given , inpHash == tempInpHash == 1 holds forever
if [ ! -z "$inputFile" ]
then
inpHash="$tempInpHash"
fi
if [ -e "$fileName" ]
then
(runExecutable)&
outputChild=$!
fi
srcHash="$tempSrcHash"
fi
sleep 2
done