Ticket #2133: hostname.2

File hostname.2, 2.9 KB (added by DJ Lucas, 17 years ago)

Replaced sysctl with cat and echo commands

Line 
1#!/bin/sh
2# Begin /bin/hostname
3
4print_usage() {
5 echo "$0 - prints or sets the system hostname."
6 echo ""
7 echo "Arguments:"
8 echo " -h | --host | --hostname prints (or sets) the system host name"
9 echo " -f | --fqdn prints (or sets) the fully qualified"
10 echo " domain name"
11 echo " -d | --domain | --domainname prints (or sets) the system domain name"
12 echo ""
13 echo ""
14 echo "Example: $0 -f host.example.com"
15}
16
17check_extra_args() {
18 if test $( echo "$1" | grep "^-" )
19 then
20 print_usage
21 echo ""
22 echo -n "Error: incorrect argument:"
23 echo " '$1'."
24 echo ""
25 exit 1
26 fi
27
28 if test "$2" != ""
29 then
30 print_usage
31 echo ""
32 echo -n "Error: too many arugments:"
33 shift 1
34 echo " ${@}."
35 echo ""
36 exit 1
37 fi
38}
39
40mod_hostname() {
41 check_extra_args $@
42 # See if we are dealing with an FQDN
43 if test $( echo "$1" | grep '\.' )
44 then
45 mod_fqdn $1
46 fi
47
48 if test "$1" != ""
49 then
50 echo "$1" > /proc/sys/kernel/hostname
51 else
52 cat /proc/sys/kernel/hostname
53 fi
54 exit 0
55}
56
57mod_fqdn() {
58 check_extra_args $@
59
60 if test "$1" != ""
61 then
62 if test ! $( echo "$1" | grep '\.' )
63 then
64 # This is not a dot separated name
65 print_usage
66 echo ""
67 echo "Error: You must provide a fully qulified name."
68 echo ""
69 exit 1
70 fi
71
72 HN=$(echo "$1" | sed 's/\./\n/' | grep -m1 .)
73 DN=$(echo "$1" | sed "s/$HN\.//")
74 echo "$HN" > /proc/sys/kernel/hostname
75 echo "$DN" > /proc/sys/kernel/domainname
76 else
77 HN="$(cat /proc/sys/kernel/hostname)"
78 DN="$(cat /proc/sys/kernel/domainname)"
79 echo "$HN.$DN"
80 fi
81 #
82 exit 0
83}
84
85mod_domainname() {
86 check_extra_args $@
87 if test "$1" != ""
88 then
89 echo "$1" > /proc/sys/kernel/domainname
90 else
91 cat /proc/sys/kernel/domainname
92 fi
93 exit 0
94}
95
96# Parse arguements
97while test "$1" != ""
98do
99 case $1 in
100
101 -help|--help|-\?)
102 print_usage
103 exit 0
104 ;;
105
106 -d|--domain|--domainname)
107 shift 1
108 mod_domainname $@
109 ;;
110
111 -f|--fqdn)
112 shift 1
113 mod_fqdn $@
114 ;;
115
116 -h|--host|--hostname)
117 shift 1
118 mod_hostname $@
119 ;;
120
121 -*)
122 # This is an invalid arguemnt
123 print_usage
124 echo ""
125 echo -n "Error: incorrect argument:"
126 echo " '$1'."
127 echo ""
128 exit 1
129 ;;
130
131 *)
132 # Anything else could be a hostname
133 # error checking is done in mod_hostname
134 mod_hostname $@
135 ;;
136
137 esac
138
139done
140
141# If we got here, then no arugments were passed
142mod_hostname $@
143exit 0
144
145# End /bin/hostname