-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.sh
More file actions
53 lines (44 loc) · 1.02 KB
/
Copy pathscript.sh
File metadata and controls
53 lines (44 loc) · 1.02 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
#!/bin/bash
linesCount="0"
loc="$1"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
selfLoc="$DIR/"
moveTo() {
head -n $3 $selfLoc$1 >> $selfLoc$2 # Move to a new file
sed -i -e "1,$3 d" $selfLoc$1 # remove from the old file
}
count() {
linesCount=$(wc -l < $selfLoc$1)
}
download() {
url=$(head -1 $selfLoc"inQueue.txt" | tr -d '\r')
echo "downloading: ${url##*/}"
wget -c $url --directory-prefix=$loc
if [ $? -ne 0 ]
then
moveTo "inQueue.txt" "failed.txt" 1
else
mv "${url##*/}" $loc
moveTo "inQueue.txt" "downloaded.txt" 1
fi
}
# check if script is already running
dupe_script=$(ps -ef | grep $selfLoc"script.sh" | grep -v grep | wc -l)
if [ $dupe_script -gt 3 ]
then
echo -e "The script was already running."
exit 0
fi
count "inQueue.txt"
if [ $linesCount -gt 0 ]
then
download
else
count "toDownload.txt"
if [ $linesCount -gt 0 ]
then
moveTo "toDownload.txt" "inQueue.txt" 1
download
fi
fi
exit