[Script] fonctions de modification de rc.conf

Autres projets et contributions
nicH2O
newbie
Messages : 1
Inscription : mer. 24 août 2011, 15:42

[Script] fonctions de modification de rc.conf

Message par nicH2O » mer. 24 août 2011, 16:38

Salut à tous,

Afin de me familiariser avec la fonction "sed" et pour que mon apprentissage soit constructif, j'ai écris un script bash qui permet de modifier les champs du fichier rc.conf, et ce à grands coups de "sed" donc. Avec la sortie toute récente des nouvelles images d'archlinux, et donc potentiellement de nombreuses (ré)installations (et ces mon cas), je vous livre ce script qui, peut-être utile pour automatiser la post-install de notre distrib préférée.

function_rc.sh (Attention: je n'ai pas fait de debogage intensif)

Code : Tout sélectionner

 #!/bin/bash

#
# functions_rc.sh for Archlinux 20110819
#

# Variables
RC_PATH="/etc/"
RC_FILENAME="rc.conf"
RC_FILE=$RC_PATH$RC_FILENAME


# -----------------------------------------------------------------------
# LOCALIZATION
# -----------------------------------------------------------------------

# Set locale
#  Usage: set_locale us_US.UTF-8
function set_locale() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes LOCALE $1
}

# Use locale daemon
#  Usage: set_locale_daemon yes
function set_locale_daemon() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes DAEMON_LOCALE $1
}

# Set hardware clock to LOCALE or UTC
#  Usage: set_hardwareclock UTC
function set_hardwareclock() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes HARDWARECLOCK $1
}

# Set timezone
#  Usage: set_timezone Europe Madrid
function set_timezone() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^TIMEZONE=.*/ s/=.*/=\"$1\/$2\"/" $RC_FILE
}

# Set keymap
#  Usage: set_keymap fr-latin9
function set_keymap() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes KEYMAP $1
}

# Set consolefont
#  Usage: set_consolefont lat9w-16
function set_consolefont() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes CONSOLEFONT $1
}

# Set consolemap
#  Usage: set_consolemap ???
function set_consolemap() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes CONSOLEMAP $1
}

# Use color
#  Usage: set_usecolor yes
function set_usecolor() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes USECOLOR $1
}



# -----------------------------------------------------------------------
# HARDWARE
# -----------------------------------------------------------------------

# Add module(s) to modules list (append mode)
#  Usage: add_module module1 module2 ... moduleX
function add_module() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		add_into_braquets MODULES $current
	done
}

# Remove module(s) to modules list
#  Usage: remove_module module1 module2 ... moduleX
function remove_module() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		remove_into_braquets MODULES $current
	done
}

# Set UDEV timeout
#  Usage: set_udev_timeout 20
function set_udev_timeout() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_simple UDEV_TIMEOUT $1
}

# Use FakeRAID (dmraid)
#  Usage: set_usedmraid no
function set_usedmraid() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes USEDMRAID $1
}

# Use BTRFS
#  Usage: set_usebtrfs no
function set_usebtrfs() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes USEBTRFS $1
}

# Use LVM volume groups
#  Usage: set_uselvm no
function set_uselvm() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes USELVM $1
}

# -----------------------------------------------------------------------
# NETWORKING
# -----------------------------------------------------------------------

# Set the hostname of machine
#  Usage: set_hostname mycomputer
function set_hostname() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes HOSTNAME $1
}

# Set the name of network device
#  Usage: set_interface eth0
function set_interface() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_simple interface $1
}

# Set IP address
#  Usage: set_address 192.168.0.1 (or for DHCP, set_address DHCP)
function set_address() {
	if [ $# -ne 1 ]; then
		exit 1
	fi
	
	if [ $1 = DHCP ]; then
		sed -i -e "/^address=.*/ s/=.*/=/" $RC_FILE
	else
		set_simple address $1
	fi
}

# Set subnet mask
#  Usage: set_netmask 255.255.255.0
function set_netmask() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_simple netmask $1
}

# Set broadcast address
#  Usage: set_broadcast 192.168.0.100
function set_broadcast() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_simple broadcast $1
}

# Set default route
#  Usage: set_gateway 192.168.0.200
function set_gateway() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_simple gateway $1
}

# Set network persist
#  Usage: set_network_persist no
function set_network_persist() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	set_into_quotes NETWORK_PERSIST $1
}

# Enable netcfg profiles (uncomment #NETWORKS=(...) )
#  Usage: enable_networks_profile
function enable_networks_profile() {
	if [ $# -gt 0 ]; then
		exit 1
	fi

	sed -i -e "/^#NETWORKS=(.*/ s/#//" $RC_FILE
}

# Disable netcfg profiles (comment NETWORKS=(...) )
#  Usage: disable_networks_profile
function disable_networks_profile() {
	if [ $# -gt 0 ]; then
		exit 1
	fi

	sed -i -e "/^NETWORKS=(.*/ s/.*/#&/" $RC_FILE
}

# Add netcfg profiles to list (append mode)
#  Usage: add_netcfg_profile menu
function add_netcfg_profile() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	enable_networks_profile
	for current in $*; do
		add_into_braquets NETWORKS $current
	done
}

# Remove netcfg profiles to list
#  Usage: remove_netcfg_profile menu
function remove_netcfg_profile() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		remove_into_braquets NETWORKS $current
	done
}

# Disable netcfg profiles
#  Usage: disable_netcfg_profile menu
function disable_netcfg_profile() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		disable_value NETWORKS $current
	done
}

# Enable netcfg profiles
#  Usage: enable_netcfg_profile menu
function enable_netcfg_profile() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		enable_value NETWORKS $current
	done
}


# -----------------------------------------------------------------------
# DAEMONS
# -----------------------------------------------------------------------

# Add daemon(s) to daemons list (append mode)
#  Usage: add_daemon daemon1 ... daemonX
function add_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		add_into_braquets DAEMONS $current
	done
}

# Remove daemon(s) to daemons list
#  Usage: remove_daemon daemon1 ... daemonX
function remove_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		remove_into_braquets DAEMONS $current
	done
}

# Disable daemon(s) to daemons list (prefix it with "!")
#  Usage: disable_daemon daemon1 ... daemonX
function disable_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		disable_value DAEMONS $current
	done
}

# Enable daemon(s) to daemons list (remove prefix "!")
#  Usage: enable_daemon daemon1 ... daemonX
function enable_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		enable_value DAEMONS $current
	done
}

# Background daemon(s) to daemons list (prefix it with "@")
#  Usage: background_daemon daemon1 ... daemonX
function background_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		background_value DAEMONS $current
	done
}

# Foreground daemon(s) to daemons list (remove prefix "@")
#  Usage: foreground_daemon daemon1 ... daemonX
function foreground_daemon() {
	if [ $# -lt 1 ]; then
		exit 1
	fi

	for current in $*; do
		foreground_value DAEMONS $current
	done
}



# -----------------------------------------------------------------------
# META FUNCTIONS
# -----------------------------------------------------------------------

# Add value into braquets
#  Usage: add_into_braquets DAEMONS dbus (to add dbus to DAEMONS list)
function add_into_braquets() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/[!\|@]*$2//" $RC_FILE
	sed -i -e "/^$1=(.*/ s/)/ $2)/" $RC_FILE
	remove_unnecessary_characters $1
}

# Remove value into braquets
#  Usage: remove_into_braquets DAEMONS dbus (to remove dbus to DAEMONS list)
function remove_into_braquets() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/[!\|@]*$2//" $RC_FILE
	remove_unnecessary_characters $1
}

# Enable value (remove the prefix ! if existe)
#  Usage: enable_value DAEMONS hwclock
function enable_value() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/[!\|@]*$2/$2/" $RC_FILE
	remove_unnecessary_characters $1
}

# Disable value (add the prefix ! if not existe)
#  Usage: disable_value DAEMONS hwclock
function disable_value() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/[@]*$2/!$2/" $RC_FILE
	remove_unnecessary_characters $1
}

# Background value (add the prefix @ if not existe)
#  Usage: background_value DAEMONS hwclock
function background_value() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/[!]*$2/@$2/" $RC_FILE
	remove_unnecessary_characters $1
}

# Foreground value (remove the prefix @ if existe)
#  Usage: foreground_value DAEMONS hwclock
function foreground_value() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/@$2/$2/" $RC_FILE
	remove_unnecessary_characters $1
}

# Set value into quotes
#  Usage: set_into_quotes HOSTNAME mycomputer (to set mycomputer to HOSTNAME)
function set_into_quotes() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=.*/ s/=.*/=\"$2\"/" $RC_FILE
}

# Set value after equal sigle 
#  Usage: set_simple interface eth0 (to set eth0 to interface)
function set_simple() {
	if [ $# -ne 2 ]; then
		exit 1
	fi

	sed -i -e "/^$1=.*/ s/=.*/=$2/" $RC_FILE
}

# Remove unnecessary characters (unnecessary whitespace, double ! and double @)
#  Usage: remove_unnecessary_characters DAEMONS
function remove_unnecessary_characters() {
	if [ $# -ne 1 ]; then
		exit 1
	fi

	sed -i -e "/^$1=(.*/ s/!!/!/" $RC_FILE
	sed -i -e "/^$1=(.*/ s/@@/@/" $RC_FILE
	sed -i -e "/^$1=(.*/ s/([ ]*/(/" $RC_FILE
	sed -i -e "/^$1=(.*/ s/[ ]*)/)/" $RC_FILE
	sed -i -e "/^$1=(.*/ s/  / /" $RC_FILE
} 
et un exemple d'utilisation standard, à lancer en root bien sûr:
test.sh

Code : Tout sélectionner

 #!/bin/bash

. ./function_rc.sh

set_locale fr_FR.UTF-8
set_timezone Europe Paris
set_consolefont lat9w-16

set_hostname mycomputer
set_interface eth0

add_daemon dbus ntpd networkmanager
disable_daemon hwclock network
background_daemon netfs crond 
Je me suis mis (récemment) à commenter mes codes en anglais, je trouve ça plus cohérent avec le code, mais pas forcément toujours dans un anglais très juste (désolé :( ).

J’espère avoir utilisé correctement la fonction "sed", sinon je suis ouvert à toutes remarques.
J'espère aussi que ce script vous sera utile.

Répondre