OSDN Git Service

fix monitor max resolution grep
[instantos/instantOS.git] / monitor.sh
1 #!/bin/bash
2
3 if [ $(whoami) = "root" ] || [ $(whoami) = "manjaro" ]; then
4     echo "user check successful"
5 else
6     echo "please run this as root"
7     exit 1
8 fi
9
10 command -v xrandr &>/dev/null ||
11     (echo "please install xrandr" && exit 1)
12
13 cd
14
15 mkdir -p /opt/instantos/monitor &>/dev/null
16 cd /opt/instantos/monitor
17
18 xrandr | grep '[^s]connected' | grep -o '[0-9]*x[0-9]*+[0-9]*' | grep -o '[0-9]*$' >positions.txt
19 AMOUNT=$(cat positions.txt | wc -l)
20
21 # get monitor with highest resolution
22 xrandr | grep '[^s]connected' | grep -Eo '[0-9]{1,}x[0-9]{1,}\+[0-9]{1,}\+[0-9]{1,}' |
23     grep -o '[0-9]*x[0-9]*' >resolutions.txt
24     
25 if [ $(cat resolutions.txt | sort -u | wc -l) = "1" ]; then
26     echo "resolutions identical"
27     cp resolutions.txt max.txt
28     exit
29 fi
30
31 let PIXELS1="$(head -1 resolutions.txt | grep -o '^[0-9]*') * $(cat resolutions.txt | head -1 | grep -o '[0-9]*$')"
32 let PIXELS2="$(tail -1 resolutions.txt | grep -o '^[0-9]*') * $(cat resolutions.txt | head -1 | grep -o '[0-9]*$')"
33
34 if [ "$PIXELS1" -gt "$PIXELS2" ]; then
35     head -1 resolutions.txt >max.txt
36 else
37     tail -1 resolutions.txt >max.txt
38 fi
39
40 if [ "$AMOUNT" = "1" ]; then
41     echo "only one monitor found, further setup not needed"
42     exit
43 else
44     if [ "$AMOUNT" -gt 2 ]; then
45         echo "only 2 monitors are supported"
46         exit
47     fi
48     echo "$AMOUNT monitors found"
49 fi
50
51 xrandr | grep '[^s]connected' | grep -o '^[^ ]*' >names.txt
52 MONITOR1=$(head -1 positions.txt)
53 MONITOR2=$(tail -1 positions.txt)
54
55 if [ "$MONITOR1" -gt "$MONITOR2" ]; then
56     echo "Monitor 1 is ${MONITOR1}px on the right"
57     echo "$MONITOR1" >right.txt
58 else
59     echo "Monitor 2 is ${MONITOR2}px on the right"
60     echo "$MONITOR1" >right.txt
61 fi