Linux系統啟動流程和特殊變量及腳本編程(3)1.特殊變量值
${變量參數#*字符} 意思是從左至右,以“字符”為分隔符,取第一個分隔符後面的所有字符串
${變量參數##*字符} 意思是從左至右,以“字符”為分隔符,取最後一個分隔符後面的所有字符串
${變量參數%字符*} 意思是從右至左,以“字符”為分隔符,取第一個分隔符後面的所有字符串
${變量參數%%字符*} 意思是從右至左,以“字符”為分隔符,取最後一個分隔符後面的所有字符串
${#變量名} 意思是計算變量裡的字符串個數值
例如:假設變量A=“10 23 45 56”,以空白為分隔符,取出以下不同值
${變量名:-字符串值}:如果變量為空或未定義,則變量展開為“字符串值”;
否則,展開為變量的值;
${變量名:+字符串值}:如果變量為空或未定義,不做任何操作;否則,則展開為“字符串值”;
${變量名:=字符串值}:如果變量為空或未定義,則變量展開為“字符串值”,
並將展開後的值賦值給變量;
${變量名:偏移:長度值}:取子串,從"偏移"處的後一個字符開始,取長度值的子串;
${變量名:偏移}: 取子串,從"偏移"處的後一個字符至末尾所有字符串
2.linux系統時腳本調用基本順序如下:
2.1:init主進程,讀取/etc/inittab配置文件
2.2.從/etc/inittab配置文件中,設定默認運行別級
2.3.從/etc/inittab配置文件中,執行/etc/rc.d/rc.sysinit腳本文件
2.4.從/etc/inittab配置文件中,執行指定運行級別的腳本
如:/etc/rc.d/rc ->/etc/rc.d/rc[0-6].d/*->/etc/rc.d/init.d/*,層層按序調用
2.5.最後執行/etc/rc.d/rc.local腳本
3.訂制一個外部命令,如,ls,useradd,mount等命令,而每個命令依賴自己的庫文件,
寫一個如下腳本,實現自動copy現有系統中的外部命令並且copy對應的庫文件
#!/bin/bash
DEST=/mnt/sysroot
libcp() {
LIBPATH=${1%/*}
[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "$1 is coped successfully "
}
bincp() {
CMDPATH=${1%/*}
[ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH
for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do
libcp $LIB
done
}
read -p "Please Input You need add command: " CMD
until [ $CMD == 'q' ]; do
if ! which $CMD &> /dev/null; then
ehco "Your command is not exist..."
read -p "Please Input You 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 You need add command: " CMD
done
4.訂制一個開關機腳本,名稱:/etc/rc.d/init.d/halt
#!/bin/bash
case $0 in
*reboot)
COMMAND='/sbin/reboot' ;;
*halt)
COMMAND='/sbin/halt -p' ;;
*)
echo "Only call by *halt | *reboot "
;;
esac
case $1 in
start)
;;
stop)
;;
*)
echo"Usage: `basename $0` {start|stop}"
;;
esac
exec $COMMAND
~
5.訂制一個在系統開關機或服務開關時顯示OK為綠色,Failed為紅色腳本
腳本名稱:/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} ]"
}
~
注意:此腳本也可以設成一個配置文件供其他腳本調用使用
6.訂制一個網絡啟動腳本,名稱:/etc/rc.d/init.d/network
#!/bin/bash
# chkconfig: 35 10 89
# description: netwrok service
prog=network
. /etc/rc.d/init.d/functions 調用上面顯示服務顏色腳本文件(注意調用時將第一行刪除)
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 $1 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.訂制一個系統初始化腳本/etc/rc.d/rc.sysinit
root@localhost 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.訂制一個基本服務腳本:名稱:/etc/rc.d/init.d/Xserver
#!/bin/bash
# chkconfig: 35 68 31
# description: tserve script service
. /etc/rc.d/init.d/functions
#prog=`basename $0`
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 $1 in
start)
start ;;
stop)
stop ;;
restart)
stop
start
;;
status)
status
;;
*)
usage
exit 6
;;
esac
本文出自 “夏維柳” 博客,請務必保留此出處http://willow.blog.51cto.com/6574604/1769355