Not many people knows about the HP-UX tool ch_rc. This is a command line tool for people who are not so familiar with sed (stream editor), awk and pattern matching.
SYNOPSIS /usr/sbin/ch_rc -a|-r|-l [-v] [-A] [-R root] [-p {parameter|parameter=value}...] [file...]
If you want to query a variable in one of a config file under /etc/rc.config.d, you can do it easily with ch_rc like this:
# ch_rc -l -p NIS_MASTER_SERVER /etc/rc.config.d/namesvrs 0 #
This is equivalent to a searching with grep, but it gives you only the value of the parameter, while you get the whole line with grep:
# grep ^NIS_MASTER_SERVER /etc/rc.config.d/namesvrs NIS_MASTER_SERVER=0 #
Note that I used the “^” character to match only the lines that are starting with the string “NIS_MASTER_SERVER”. But the real strength of ch_rc is that it is also capable of modifying the variable – just as you would do it with sed. Let’s set our box as a NIS master, and confirm it!
# ch_rc -a -p NIS_MASTER_SERVER=1 /etc/rc.config.d/namesvrs # ch_rc -l -p NIS_MASTER_SERVER /etc/rc.config.d/namesvrs 1 #
The good thing is, that you shouldn’t even specify the name of the config file: it will find the appropriate file. You only need to give a filename if you want to declare a new, non-existing variable which cannot be found yet. It will search for the variable under /etc/rc.config.d and also looks into /etc/TIMEZONE per default.
The tool comes also handy if you suspect that a variable is set more than once in your config. Normally it would mess up your config, and would cause that the last sourced variable would be in effect. Let’s say I mistakenly set a variable like this:
# echo "NIS_MASTER_SERVER=1" >> /etc/rc.config.d/netconf #
or like this:
# echo "NIS_MASTER_SERVER=0" >> /etc/rc.config.d/nameservers #
Note that there isn’t any “nameservers” config file under rc.config.d normally, I just simulated a typo. So you can easily search for the variable among the config files which will be sourced at next boot, looking for duplicate entries:
# ch_rc -v -l -p NIS_MASTER_SERVER /etc/rc.config.d/namesvrs: NIS_MASTER_SERVER=1 /etc/rc.config.d/netconf: NIS_MASTER_SERVER=1 /etc/rc.config.d/nameservers: NIS_MASTER_SERVER=0 #
For this I used the “-v” switch which tells ch_rc to also output the name of the files. So after we found the problem, we can erase the erroneous instances and confirm the changes:
# ch_rc -r -p NIS_MASTER_SERVER /etc/rc.config.d/nameservers # ch_rc -r -p NIS_MASTER_SERVER /etc/rc.config.d/netconf # ch_rc -v -l -p NIS_MASTER_SERVER /etc/rc.config.d/namesvrs: NIS_MASTER_SERVER=1 #
ch_rc cannot interpret the variables and their values, it is just a means for easily mathing pattern and working with config files. It can also handle arrays with the “-A” switch:
# ch_rc -A -lv -pSUBNET_MASK /etc/rc.config.d/netconf: SUBNET_MASK[0]="255.255.255.0" /etc/rc.config.d/netconf: SUBNET_MASK[1]="255.255.255.0" #
You can find some more useful information related to ch_rc in the man page. As I feel myself more comfortable with sed, I think this tool is most useful for those who don’t. But in some scripting tasks, it could be useful to me, too. Who knows?