-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogWrite
74 lines (61 loc) · 1.7 KB
/
logWrite
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
#!/bin/sh
#run as bash log pippos
motherFolder='/home/enricojr/cernbox/logbook'
EXITLINE='ciao'
ATTACHMENTLINE='atch'
##############################
#cheking that logName is given
##############################
logName=$1
if [ -z "$logName" ]
then
echo "Usage: logWrite <logName>"
exit 1
fi
#########################################
# defining logbook and attachments folder
#########################################
logFolder="$motherFolder/logbooks/$logName"
logfileName="$logFolder/logbook.txt"
attachmentsFolderName="$logFolder/attachments/"
################
# if new, create
################
if [ ! -d "$logFolder" ]; then
echo -n "logbook $logName does not exist yet. Create new log? [y/n] "
read answer
if [ "$answer" = "y" ]; then
mkdir $logFolder
mkdir $attachmentsFolderName
echo "created new logbook named $logName"
else
exit 2;
fi
fi
###############
# infinite loop
###############
while [ true ]
do
#reading line
read -e -p "$logName => " line
#if string is empty, continue
if [ -z "$line" ]; then
continue;
#if line is EXITLINE, break
elif [ "$line" = "$EXITLINE" ]; then
break;
#if line is ATTACHMENTLINE, ask for attachment
elif [ "$line" = "$ATTACHMENTLINE" ]; then
read -e -p "attachment: " attachment
cp -r $attachment $attachmentsFolderName
attachmentName=`ls -1t $attachmentsFolderName | head -1`
attachmentDate=`date +"%Y-%m-%d-%H-%M-%S_"`
attachmentFull=$attachmentDate$attachmentName
mv $attachmentsFolderName$attachmentName $attachmentsFolderName$attachmentFull
echo `date +"%Y-%m-%d %a @ %H:%M:%S"` "| ATTACHMENT: $attachmentFull" >> $logfileName
#else write
else
echo `date +"%Y-%m-%d %a @ %H:%M:%S"` "|" $line >> $logfileName
fi
done