Project

General

Profile

Feature #13975 ยป stty.sh

Joshua M. Clulow, 2021-08-25 11:17 PM

 
1
#!/bin/bash
2

    
3
speeds='
4
    50
5
    1200
6
    4800
7
    9600
8
    921600
9
    1000000
10
    1152000
11
    1500000
12
    2000000
13
    2500000
14
    3000000
15
    3500000
16
    4000000
17
'
18

    
19
function getbaud {
20
	stty -a | tr ';' '\n' | awk -v t="$1" '$1 == t { print $2 }'
21
}
22

    
23
function confirm {
24
	[[ "$(getbaud $1)" == "$2" ]]
25
}
26

    
27
#
28
# Return to defaults on exit:
29
#
30
trap 'stty 38400' ERR EXIT
31

    
32
for speed in $speeds; do
33
	#
34
	# Set speed (i.e., the combination of ispeed and ospeed):
35
	#
36
	if confirm speed $speed; then
37
		printf 'ERROR: did not expect speed %s already\n' "$speed"
38
		exit 1
39
	fi
40
	if ! stty $speed; then
41
		printf 'ERROR: could not set speed %s\n' "$speed"
42
		exit 1
43
	fi
44
	if ! confirm speed $speed; then
45
		printf 'ERROR: expected speed %s to be set\n' "$speed"
46
		exit 1
47
	fi
48
	printf 'speed %s ok\n' "$speed"
49

    
50
	#
51
	# Set ispeed but not ospeed:
52
	#
53
	if ! stty 600 || ! stty ispeed $speed; then
54
		printf 'ERROR: could not set ispeed to %s\n' "$speed"
55
		exit 1
56
	fi
57
	if ! confirm ospeed 600 || ! confirm ispeed $speed; then
58
		printf 'ERROR: expected ispeed %s to be set\n' "$speed"
59
		exit 1
60
	fi
61
	printf 'ispeed %s ok\n' "$speed"
62

    
63
	#
64
	# Set ospeed but not ispeed:
65
	#
66
	if ! stty 75 || ! stty ospeed $speed; then
67
		printf 'ERROR: could not set ospeed to %s\n' "$speed"
68
		exit 1
69
	fi
70
	if ! confirm ospeed $speed || ! confirm ispeed 75; then
71
		printf 'ERROR: expected ospeed %s to be set\n' "$speed"
72
		exit 1
73
	fi
74
	printf 'ospeed %s ok\n' "$speed"
75
done
    (1-1/1)