码迷,mamicode.com
首页 > 系统相关 > 详细

Linux系统启动流程及脚本编程(3)

时间:2016-05-02 00:46:15      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:linux系统启动流程及脚本编程(3)

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

Linux系统启动流程及脚本编程(3)

标签:linux系统启动流程及脚本编程(3)

原文地址:http://willow.blog.51cto.com/6574604/1769355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!