-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbingimg.sh
executable file
·67 lines (60 loc) · 1.25 KB
/
bingimg.sh
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
#!/usr/bin/env bash
help_msg() {
echo "Usage: $0 [-h] [-a dir] [-d path] [-n name]"
echo "Options:"
echo " -a <dir> archive picture in given directory"
echo " -d <path> destination of picture (default's current directory)"
echo " -h display this message"
echo " -n <name> choose name for picture"
}
while getopts ":a:d:hn:" opt; do
case $opt in
a)
ARCHIVE=$OPTARG
if [[ ! $ARCHIVE =~ ^.*\/$ ]]; then
ARCHIVE=$ARCHIVE"/"
fi
if [ ! -d "$ARCHIVE" ]; then
echo "No such directory: $ARCHIVE"
exit 1
fi
;;
d)
DEST="$OPTARG"
if [[ ! $DEST =~ ^.*\/$ ]]; then
DEST=$DEST"/"
fi
if [ ! -d "$DEST" ]; then
echo "No such directory: $DEST"
exit 1
fi
;;
h)
help_msg
exit 0
;;
n)
FILE=$OPTARG
;;
\?)
echo "Invalid option -$opt" >&2
help_msg
exit 1
;;
:)
echo "Option -$opt requires an argument" >&2
help_msg
exit 1
;;
esac
done
URL="www.bing.com"
ADDRESS=$URL$(curl -s $URL | grep g_img={url: | sed -e s/.*g_img={url:\'// | sed 's/\.jpg.*//')".jpg"
NAME=$(echo $ADDRESS | awk -F/ '{print $(NF)}' | cut -d _ -f 1)".jpg"
curl -so $DEST$NAME $ADDRESS
if [ -n "$ARCHIVE" ]; then
cp $DEST$NAME $ARCHIVE
fi
if [ -n "$FILE" ]; then
mv $DEST$NAME $DEST$FILE
fi