Linux system startup process and scripting (3)
1. Special variable values
${variable argument #* character} means left to right, with "character" as the delimiter, taking all strings after the first delimiter
${variable argument ##* character} means left to right, with "character" as the delimiter, taking all strings following the last delimiter
${variable parameter% character *} means right-to-left, with "character" as the delimiter, taking all strings after the first delimiter
${variable parameter the percent character *} means right-to-left, with "character" as the delimiter, taking all strings following the last delimiter
${#变量名} means the numeric value of a string in the calculation variable
For example: Suppose the variable a= "10 23 45 56", with a blank delimiter, take out the following different values
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7F/9E/wKioL1clyWfBzFUNAAFPtGjcv8s680.jpg "title=" aa.jpg "alt=" Wkiol1clywfbzfunaafptgjcv8s680.jpg "/>
The basic sequence of script calls for 2.linux systems is as follows:
2.1:init master process, read/etc/inittab configuration file
2.2. From the/etc/inittab configuration file, set the default run level
2.3. From the/etc/inittab configuration file, execute the/etc/rc.d/rc.sysinit script file
2.4. From the/etc/inittab configuration file, execute a script that specifies the run level
such as:/etc/rc.d/rc->/etc/rc.d/rc[0-6].d/*->/etc/rc.d/init.d/*, the layers are called sequentially
2.5. Finally execute the/etc/rc.d/rc.local script
3. Customize an external command, such as Ls,useradd,mount commands, and each command relies on its own library file,
Write a script like this to automate the copy of the external command from the existing system and copy the corresponding library file
#!/bin/bash
Dest=/mnt/sysroot
LIBCP () {
libpath=${1%/*}
[!-D $DEST $libpath] && mkdir-p $DEST $libpath
[!-e $DEST ${1}] && cp $DEST $libpath && echo "is coped successfully"
}
BINCP () {
cmdpath=${1%/*}
[!-D $DEST $cmdpath] && mkdir-p $DEST $cmdpath
[!-e $DEST ${1}] && cp $DEST $cmdpath
For LIB in ' LDD $ | Grep-o "/.*lib\ (64\) \{0,1\}/[^[:space:]]\{1,\}"; Do
LIBCP $LIB
Done
}
Read-p "Please Input need add command:" CMD
until [$CMD = = ' Q ']; Do
if! which $CMD &>/dev/null; Then
Ehco "Your command is not exist ..."
Read-p "Please Input need add Command,again:" CMD
Continue
Fi
Command= ' which $CMD | Grep-v "^alias" | Grep-o "[^[:space:]]\{1,\}" '
BINCP $COMMAND
echo "$COMMAND is copied OK"
Read-p "Please Input need add command:" CMD
Done
4. Customize a switch machine script, name:/etc/rc.d/init.d/halt
#!/bin/bash
Case $ in
*reboot)
Command= '/sbin/reboot ';;
*halt)
Command= '/sbin/halt-p ';;
*)
echo "by *halt | *reboot "
;;
Esac
Case $ in
Start
;;
Stop
;;
*)
echo "Usage: ' basename $ ' {start|stop}"
;;
Esac
EXEC $COMMAND
~
5. Custom-made one in the system switch or service switch when the display OK is green, failed is a red script
Script Name:/etc/rc.d/init.d/functions
#!/bin/bash
screen= ' stty-f/dev/console size 2>/dev/null '
columns=${screen#*}
[-Z $COLUMNS] && columns=80
spa_col=$[$COLUMNS-14]
Red= ' \033[31m '
Green= ' \033[32m '
yellow= ' \033[33m '
Blue= ' \033[34m '
Normal= ' \033[0m '
Success () {
String=$1
rt_col=$[$SPA _col-${#string}]
Echo-n "$string"
For I in ' seq 1 $RT _col '; Do
Echo-n ""
Done
Echo-e "[${green}ok${normal}]"
}
Failure () {
String=$1
rt_col=$[$SPA _col-${#string}]
Echo-n "$string"
For I in ' seq 1 $RT _col '; Do
Echo-n ""
Done
Echo-e "[${red}failed${normal}]"
}
~
Note: This script can also be set up as a configuration file for other script calls to use
6. Customize a network startup script, name:/etc/rc.d/init.d/network
#!/bin/bash
# CHKCONFIG:35 10 89
# Description:netwrok Service
Prog=network
. The service color script file is displayed above the/etc/rc.d/init.d/functions call (note that the first row is deleted when called)
Conf=/etc/sysconfig/network-scripts/ifcfg-eth0
. $CONF
Start () {
Ifconfig eth0 $IPADDR/$NETMASK up
[-Z $GATEWAY] && route add default GW $GATEWAY
}
Stop () {
Ifconfig eth0 Down
}
Status () {
Ifconfig eth0
}
Usage () {
echo "Usage: $prog: {start|stop|restart|status}"
}
Case $ in
Start
Start
Success "Config Network Eth0"
;;
Stop
Stop
Success "Stop Network Eth0"
;;
Restart
Stop
Start
Success "Restart Network Eth0"
;;
Status
Status
;;
*)
Usage
Exit 5
;;
Esac
7. Customize a system initialization script/etc/rc.d/rc.sysinit
[Email protected] sysroot]# vim Etc/rc.d/rc.sysinit
#!/bin/bash
. Etc/rc.d/init.d/functions
Echo-e "\ t Welcome to \033[31mwillow person\033[0m Linux ..."
echo "Remount rootfs ..."
Mount-n-O REMOUNT,RW/
Mount-a
[$?-eq 0] && Success "mount other system files" | | Failure "Mount Other system files"
echo "Set the hostname ..."
#[-f/etc/sysconfig/network] && source/etc/sysconfig/network
[-f/etc/sysconfig/network] &&. /etc/sysconfig/network
[-Z $HOSTNAME-o $HOSTNAME = = ' (none) '] && hostname=localhost
/bin/hostname $HOSTNAME
Insmod/lib/modules/mii.ko
[$?-eq 0] && Success "Loading modules Mii" | | Failure "Loading Modules Mii"
Insmod/lib/modules/pcnet32.ko
[$?-eq 0] && Success "Loading modules Pcnet32" | | Failure "Loading Modules Pcnet32"
#ifconfig eth0 1.1.1.100
Ifconfig Lo 127.0.0.1
#/bin/bash
/sbin/sysctl-p
7. Customize a basic Service script: Name:/etc/rc.d/init.d/xserver
#!/bin/bash
# CHKCONFIG:35 68 31
# Description:tserve Script Service
. /etc/rc.d/init.d/functions
#prog = ' basename '
Prog=tserver
lockfile=/var/lock/subsys/$prog
Start () {
# echo "Starting $prog ..."
Touch $lockfile
[$?-eq 0] && Success "Starting $prog" | | Failure "Starting $prog"
}
Stop () {
# echo "Stopping $prog ..."
[$?-eq 0] && Success "Stopping $prog" | | Failure "Stopping $prog"
RM-RF $lockfile
}
Status () {
If [-f $lockfile]; Then
echo "$prog is Running ..."
Else
echo "$prog is Stopped ..."
Fi
}
Usage () {
echo "Usage: $prog {Start|stop|status|restart}"
}
Case $ in
Start
start;;
Stop
stop;;
Restart
Stop
Start
;;
Status
Status
;;
*)
Usage
Exit 6
;;
Esac
This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1769355
Linux system startup process and scripting (3)