版本,CPU型号,内存大小,硬盘大小
#!/bin/bash
YELLOW='\e[1;33m'
RED='\e[1;31m'
END='\e[0m'
echo -e "${YELLOW}++++++++++++++++++++++++++++++++++++++++++${END}"
echo -e "${RED}HOSTNAME: `hostname`${END}"
echo -e "${RED}IPADDR: `hostname -I`${END}"
echo -e "${RED}OS's version: `cat /etc/centos-release`${END}"
echo -e "${RED}Kernel version: `uname -r`${END}"
echo -e "${RED}CPU: `lscpu |egrep '^Model name'|tr -s ' '|cut -d: -f2`${END}"
echo -e "${RED}Memory size: `free -h|sed -rn '2p'|tr -s ' '|cut -d' ' -f2`${END}"
echo -e "${RED}Disk size: `lsblk |sed -n '2p'|egrep -o '([0-9]+)G'`${END}"
echo -e "${YELLOW}++++++++++++++++++++++++++++++++++++++++++${END}"
版本1:
#!/bin/bash
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
SOURCE="/etc"
DEST="/backup"
[ ! -d $DEST ] && { mkdir $DEST;echo -e "${RED}Start backup...${END}";sleep 3;cp -av $SOURCE $DEST/etc`date +%F`;echo -e "${GREEN}Back is finished.${END}"; } || { echo -e "${RED}Start backup...${END}";sleep 3;cp -av $SOURCE $DEST/etc`date +%F`;echo -e "${GREEN}Back is finished.${END}"; }
版本2:
#!/bin/bash
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
SOURCE="/etc"
DEST="/backup"
if [ ! -d $DEST ];then
mkdir $DEST
echo -e "${RED}Start backup...${END}"
sleep 3
cp -av $SOURCE $DEST/etc`date +%F`
echo -e "${GREEN}Back is finished.${END}"
else
echo -e "${RED}Start backup...${END}"
sleep 3
cp -av $SOURCE $DEST/etc`date +%F`
echo -e "${GREEN}Back is finished.${END}"
fi
#!/bin/bash
COLOR='\e[1;36m'
END='\e[0m'
MAX=`df|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'|sort -nr|head -1`
echo -e "${COLOR}The maximum value of space is $MAX in the hard disk partition.${END}"
#!/bin/bash
COLOR='\e[1;34m'
END='\e[0m'
RESULT=`netstat -tan|egrep 'ESTABLISHED'|tr -s ' ' :|cut -d: -f6|sort|uniq -c|sort -nr`
echo -e "${COLOR}Active Internet connections's status:\n${RESULT}${END}"
版本1:
#!/bin/bash
[ $# -lt 1 ] && { echo -e "\e[1;31mPlease at least give an argument.\e[0m";exit 2; }
[ $# -ge 1 ] && echo -e "\e[1;35mBlank lines is `grep '^$' $1 $2 $3|wc -l`.\e[0m"
版本2:
#!/bin/bash
if [ $# -lt 1 ];then
echo -e "\e[1;31mPlease at least input a filename as an argument.\e[0m"
exit 2
else
echo -e "\e[1;36mBlank line is `egrep '^$' $1 $2 $3 | wc -l`.\e[0m"
fi
版本1:
#!/bin/bash
IP=10.0.0.7
RED='\e[1;31m'
GREEN='\e[1;32m'
ping -c1 -W1 $IP &> /dev/null && echo -e "${GREEN}${IP} is up.${END}" || echo -e "${RED}${IP} is down.${END}"
版本2:
#!/bin/bash
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
IP=10.0.0.2
if ping -c1 -W1 $IP &> /dev/null;then
echo -e "${GREEN}${IP} is up.${END}"
else
echo -e "${RED}${IP} is down.${END}"
fi
版本3:
#!/bin/bash
IP=10.0.0
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
for i in `seq 254`;do
ping -c1 -W1 ${IP}.${i} &> /dev/null && echo -e "${GREEN}${IP}.${i} is up.${END}" || echo -e "${RED}${IP}.${i} is down.${END}"
done
版本1:
#!/bin/bash
WARN=20
GREEN='\e[1;32m'
END='\e[0m'
SPACE_USED=`df|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'|sort -nr|head -1`
INODE_USED=`df -i|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'|sort -nr|head -1`
[ $SPACE_USED -ge $WARN -o $INODE_USED -ge $WARN ] && echo "space will be full." | mail -s diskwarning [email protected] || echo -e "${GREEN}Disk space is enough.${END}"
版本2:
#!/bin/bash
WARN=20
YELLOW='\e[1;33m'
END='\e[0m'
DISK_USED=`df|egrep '/dev/sd'|egrep -o '([0-9]+)%'|tr -d '%'|sort -nr|head -1`
INODE_USED=`df -i|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'|sort -nr|head -1`
if [ $DISK_USED -ge $WARN -o $INODE_USED -ge $WARN ];then
echo "Disk space will be full." |mail -s Diskwarning [email protected]
else
echo -e "${YELLOW}Disk space is enough.${END}"
fi
版本3:
#!/bin/bash
WARN=10
RED='\e[1;31m'
BLUE='\e[1;34m'
END='\e[0m'
DISK_USED=`df|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'`
for i in $DISK_USED;do
[ $i -ge $WARN ] && echo -e "${RED}Disk space will be full.${END}" || echo -e "${BLUE}Disk space is enough.${END}"
done
版本1:
#!/bin/bash
read -p "Please input a filename: " FILE
[ ! -r $FILE -a ! -w $FILE ] && echo "$FILE is not readable and not writeable" || echo "$FILE is readable or writeable."
版本2:
#!/bin/bash
COLOR1='\e[1;33m'
COLOR2='\e[1;34m'
END='\e[0m'
if [ $# -ne 0 ];then
[ ! -r $1 -a ! -w $1 ] && echo -e "${COLOR1}$1 is not readable and not writeable.${END}" || echo -e "${COLOR2}$1 is readable or writeable.${END}"
else
echo -e "${COLOR1}Please input a file.${END}"
fi
版本1:
#!/bin/bash
RED='\e[1;31m'
YELLOW='\e[1;33m'
END='\e[0m'
read -p "Please input a filename: " FILE
[[ "$FILE" =~ ^.*\.sh$ ]] && { chmod a+x $FILE;echo -e "${YELLOW}${FILE}'s executeable is added.${END}"; } || echo -e "${RED}${FILE} is not a script file.${END}"
版本2:
#!/bin/bash
RED='\e[1;31m'
YELLOW='\e[1;33m'
END='\e[0m'
read -p "Please input a filename: " FILE
if [[ "$FILE" =~ ^.*\.sh$ ]];then
chmod a+x $FILE
echo -e "${YELLOW}${FILE}'s executeable permission is added.${END}"
else
echo -e "${RED}${FILE} is not a script file.${END}"
fi
版本1:
#!/bin/bash
YELLOW='\e[1;33m'
END='\e[0m'
FILE="/etc/nologin"
[ ! -e $FILE ] && { touch $FILE; echo -e "${YELLOW}Prohibit common user login.${END}"; } || echo -e "${YELLOW}Cmmon user can not login.${END}"
版本2:
#!/bin/bash
YELLOW='\e[1;33m'
RED='\e[1;31m'
END='\e[0m'
FILE="/etc/nologin"
if [ ! -e $FILE ];then
touch $FILE
echo -e "${YELLOW}Prohibit common user login.${END}"
else
echo -e "${RED}Cmmon user can not login.${END}"
fi
echo "export PATH=/usr/local/apache/bin:$PATH" >> /etc/profile
source /etc/profile
echo $PATH
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是
CentOS7)
echo PS1="\[\e[1;32m\][\[\e[0m\]\[\e[1;32m\]\u\[\e[32m\]@\h\[\e[1;32m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ " >> /etc/profile.d/env.sh
cat <> ~/.bashrc
>rm='rm -i'
>cdnet='cd /etc/sysconfig/network-scripts/'
>editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
>EOF
echo -e "\e[1;31mHi,dangerous!\e[0m" >> /etc/motd
vim ~/.vimrc
set ts=2
set expandtab
set ignorecase
set autoindent
autocmd BufNewFile *.sh exec ":.call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#***********************************************")
call setline(3,"#Author: Kingdom_xu")
call setline(4,"#Mail: [email protected]")
call setline(5,"#Version: 1.0")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#Description: The test script")
call setline(9,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(10,"#***********************************************")
call setline(11,"")
endif
endfunc
autocmd BufNewFile * normal G
练习
版本1:
#!/bin/bash
read -p "Please input a username: " NAME
[ -z $NAME ] && { echo -e "\e[1;33mPlease give a username.\e[0m";exit 2; }
id $NAME &> /dev/null && { echo -e "\e[1;36m$NAME is exist.\e[0m"; } || { useradd $NAME;echo $NAME:123456 | chpasswd $NAME;getent passwd $NAME;passwd -e $NAME &> /dev/null; }
版本2:
#!/bin/bash
read -p "Please input a username: " NAME
if [ -z $NAME ];then
echo -e "\e[1;37mInput a username.\e[0m"
exit 2
fi
if getent passwd $NAME;then
echo -e "\e[1;32m$NAME is exist.\e[0m"
else
useradd $NAME
echo $NAME:123456 | chpasswd $NAME
id $NAME
passwd -e $NAME &> /dev/null
fi
#!/bin/bash
read -p "Please input yes or no: " ANS
case $ANS in
[Yy]|[Yy][Ee][Ss])
echo -e "\e[1;34mYour answer is YES.\e[0m"
;;
[Nn]|[Nn][Oo])
echo -e "\e[1;35mYour answer is NO.\e[0m"
;;
*)
echo -e "\e[1;33mPlease input yse or no.\e[0m"
esac
#!/bin/bash
read -p "Input a filename: " FILE
TYPE=`ls -ld $FILE|egrep -o '^.'`
case $TYPE in
-)
echo -e "\e[1;31m$FILE is a normal file.\e[0m"
;;
d)
echo -e "\e[1;32m$FILE is a directory file.\e[0m"
;;
l)
echo -e "\e[1;33m$FILE is a link file.\e[0m"
;;
b)
echo -e "\e[1;34m$FILE is a block file.\e[0m"
;;
c)
echo -e "\e[1;35m$FILE is a character file.\e[0m"
;;
s)
echo -e "\e[1;36m$FILE is a socket file.\e[0m"
;;
p)
echo -e "\e[1;37m$FILE is a pipe file.\e[0m"
;;
*)
echo -e "\e[1;31m$FILE is other type file.\e[0m"
esac
#!/bin/bash
read -p "Please input a digit: " NUM
[[ "$NUM" =~ ^[0-9]+$ ]] && echo -e "\e[1;32m$NUM is a positive integer.\e[0m" || echo -e "\e[1;31m$NUM is not a positive integer.\e[0m"
#!/bin/bash
echo -e "\e[1;31mThis is a system initialization script.\e[0m"
echo -e "\e[1;$[RANDOM%7+31]m"
cat < /dev/null
setenforce 0 &> /dev/null
systemctl stop firewall &> /dev/null
systemctl disable --now firewall &> /dev/null
echo -e "\e[1;31mTurned off SElinux and firewalld finished.\e[0m"
;;
2)
echo -e "\e[1;32mStarting install many packages.\e[0m"
yum -y install wget vim net-tools
yum -y install psmisc tree bc
echo -e "\e[1;32mInstall packages finished.\e[0m"
;;
3)
echo -e "\e[1;33mStarting update yum resource.\e[0m"
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repio &> /dev/null
yum makecache &> /dev/null
echo -e "\e[1;33mUpdate yum resource finished.\e[0m"
;;
4)
DIR="/etc/sysconfig/network-scripts"
echo -e "\e[1;34mStarting modify network configure.\e[0m"
nmcli connection add type ethernet con-name eth0 ifname ens160 &> /dev/null
nmcli connection delete ens160 &> /dev/null
sed -i 's/ens160/eth0/g' $DIR/ifcfg-eth0 &> /dev/null
sed -i 's/dhcp/static/' $DIR/ifcfg-eth0 &> /dev/null
sed -i '/^GRUB_CMDLINE_LINUX=/s/"$/ net.ifnames=0"/' /etc/default/grub &> /dev/null
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "IPADDR=10.0.0.8\nPREFIX=24\nGATEWAY=10.0.0.2" >> $DIR/ifcfg-eth0
echo -e "\e[1;34mModified network configure finished.\e[0m"
;;
5)
echo -e "\e[1;35mStarting define alias name.\e[0m"
alias rm='cp -a /etc /data'
alias cdnet='cd /etc/sysconfig/network-scripts'
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
echo -e "\e[1;35mDefined alias name finished.\e[0m"
;;
6)
echo -e "\e[1;36mStarting configure ~/.vimrc file.\e[0m"
cat /data/vimrc.txt > ~/.vimrc
echo -e "\e[1;37mConfigure ~/.vimrc finished.\e[0m"
;;
*)
echo -e "\e[1;31mInvalid option,please choose again.\e[0m"
;;
esac
练习:用 for 实现
#!/bin/bash
for FILE in $(ls /var);do
TYPE=$(ls -ld /var/$FILE|egrep -o '^.')
case $TYPE in
-)
echo -e "\e[1;31m$FILE is normal file.\e[0m"
;;
d)
echo -e "\e[1;32m$FILE is directory file.\e[0m"
;;
b)
echo -e "\e[1;33m$FILE is block file.\e[0m"
;;
c)
echo -e "\e[1;34m$FILE is character file.\e[0m"
;;
l)
echo -e "\e[1;35m$FILE is link file.\e[0m"
;;
p)
echo -e "\e[1;36m$FILE is pipe file.\e[0m"
;;
S)
echo -e "\e[1;37m$FILE is socket file.\e[0m"
;;
*)
echo -e "\e[1;31m$FILE is other file.\e[0m"
esac
done
#!/bin/bash
for i in `seq 10`;do
if getent passwd user$i &> /dev/null;then
echo -e "\e[1;32muser$i is exist.\e[0m"
else
useradd user$i
PASSWD=`cat /dev/urandom|tr -dc [:alnum:]|head -c8`
echo user${i}:$PASSWD |chpasswd user$i
echo user${i}:$PASSWD >> /data/passwd.txt
echo -e "\e[1;35muser$i created successfully.\e[0m"
fi
done
#!/bin/bash
FILE=`ls /etc/rc.d/rc3.d/`
for i in FILE;do
find /etc/rc.d/rc3.d/S* -exec mv {} "{} start" \; &> /dev/null
find /etc/rc.d/rc3.d/K* -exec mv {} "{} stop" \; &> /dev/null
done
#!/bin/bash``
read -p "Please input a positive integer: " INT
for ((i=1,sum=0;i<=$INT;i++));do
let sum+=i
done
echo -e "\e[1;$[$RANDOM%7+31]mThe sum is $sum.\e[0m"
#!/bin/bash
for ((i=1,sum=0;i<=100;i++));do
MODULO=$[i%3]
[ $MODULO -eq 0 ] && let sum+=i
done
echo -e "\e[1;$[$RANDOM%7+31]mThe sum is $sum\e[0m"
版本1:
#!/bin/bash
IP=10.0.0
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
for i in `seq 254`;do
ping -c1 -W1 ${IP}.${i} &> /dev/null && echo -e "${GREEN}${IP}.${i} is up.${END}" || echo -e "${RED}${IP}.${i} is down.${END}"
done
版本2:
#!/bin/bash
IP=10.0.0
RED='\e[1;31m'
GREEN='\e[1;32m'
END='\e[0m'
for ((i=1;i<=254;i++));do
ping -c1 -W1 ${IP}.${i} &> /dev/null && echo -e "${GREEN}${IP}.${i} is up.${END}" || echo -e "${RED}${IP}.${i} is down.${END}"
done
版本1:
#!/bin/bash
for i in {1..9};do
for j in `seq $i`;do
echo -e "\e[1;$[RANDOM%7+31]m${j}x${i}=$[i*j]\t\c\e[0m"
done
echo
done
版本2:
#!/bin/bash
for ((i=1;i<=9;i++));do
for ((j=1;j<=i;j++));do
echo -e "\e[1;$[RANDOM%7+31]m${j}x${i}=$[i*j]\t\c\e[0m"
done
echo
done
#!/bin/bash
NAME=`cat /dev/urandom|tr -dc '[:alpha:]'|head -c8`
DIR=/data/testdir
for i in `seq 10`;do
if [ -d $DIR ];then
touch ${DIR}/${i}${NAME}.html
else
mkdir -p $DIR
touch ${DIR}/${i}${NAME}.html
fi
done
#!/bin/bash
read -p "请输入三角形的行数: " line
for((i=1;i<=line;i++));do
for((k=0;k<=line-i;k++));do
echo -e ' \c'
done
for((j=1;j<=2*i-1;j++));do
echo -e '*\c'
done
echo
done
#!/bin/bash
sum=1
for ((n=1;n<10;n++));do
sum=$[2*$[$sum+1]]
done
echo "所摘桃子数: $sum"``
练习:用while实现
#!/bin/bash
i=1
sum=0
while ((i<=100));do
let sum+=i
let i+=2
done
echo -e "\e[1;34mThe sum is $sum.\e[0m"
#!/bin/bash
IP=192.168.0
i=1
UP=0
DOWN=0
while [ $i -le 254 ] ;do
if ping -c1 -W1 ${IP}.$i &> /dev/null;then
echo -e "\e[1;32m${IP}.${i} is up.\e[0m"
let UP++
else
echo -e "\e[1;31m${IP}.${i} is down.\e[0m"
let DOWN++
fi
let i++
done
echo -e "\e[1;34mUP is $UP.\e[0m"
echo -e "\e[1;35mDOWN is $DOWN.\e[0m"
#!/bin/bash
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
echo -e "\e[1;$[RANDOM%7+31]m${j}x${i}=$[i*j]\t\c\e[0m"
let j++
done
let i++
echo
done
i=1
while true;do
NUM=$RANDOM
if [ "$i" -eq "1" ];then
MAX=$NUM
MIN=$NUM
else
if [ "$MAX" -lt "${num[$i]}" ];then
MAX=${num[$i]}
elif [ "$MIN" -gt "${num[$i]}" ];then
MIN=${num[$i]}
else
true
fi
fi
let i++
done
echo "num is : ${num[@]}"
echo "最大值MAX:$MAX 最小值MIN:$MIN"
#!/bin/bash
i=1
D_COLOR='\033[1;41m'
S_COLOR='\033[1;47m'
AND_COLOR='\033[0m'
while [ $i -le 8 ];do
if [ "$[$i%2]" -eq "0" ];then
j=1
while [ $j -le 8 ];do
if [ "$[$j%2]" -eq "0" ];then
echo -e "${S_COLOR} ${AND_COLOR}\c"
else
echo -e "${D_COLOR} ${AND_COLOR}\c"
fi
let j++
done
else
j=1
while [ $j -le 8 ];do
if [ "$[$j%2]" -eq "0" ];then
echo -e "${D_COLOR} ${AND_COLOR}\c"
else
echo -e "${S_COLOR} ${AND_COLOR}\c"
fi
let j++
done
fi
echo ""
let i++
done
#!/bin/bash
RAN=1
cat test.txt | while read CHESS;do
{ while true;do
MD=echo $RAN|md5sum|cut -c1-10
if [[ "$MD" == "$CHESS" ]];then
echo $RAN
break
else
let RAN++
fi
done }&
wait
done
练习
录于日志/var/log/login.log中,并退出脚本
#!/bin/bash
while :;do
who|grep 'hacker' >> /var/log/login.log
sleep 3
done
#!/bin/bash
NUM1=`cat /dev/urandom|tr -dc '[:digit:]'|head -c1`
while :;do
read -p "Please input a digit[0-9]: " NUM2
if [ $NUM2 -eq $NUM1 ];then
echo -e "\e[1;$[RANDOM%7+31]mCongratulations, got it right.\e[0m"
exit 2
elif [ $NUM2 -lt $NUM1 ];then
echo -e "\e[1;$[RANDOM%7+31]mSorry,is too lower.\e[0m"
else
echo -e "\e[1;$[RANDOM%7+31]mSorry,is too higher.\e[0m"
fi
done
#!/bin/bash
read -p "Please input a little filename: " FILES
while [ -n "$FILES" ];do
echo -e "\e[1;33mLines are `cat $FILES |wc -l`.\e[0m"
break
done
#!/bin/bash
if [ $# -lt 2 ];then
echo at least 2 argument
exit 1
fi
declare -i big
declare -i small
declare -i fir=$#
until [ $# = 0 ];do
if [[ ! "$1" =~ ^[0-9]+$ ]];then
echo wrong number $1
exit
fi
if [ $fir = $# ];then
let big=$1
let small=$1
else
if [ $1 -gt $big ];then
let big=$1
elif [ $1 -lt $small ];then
let small=$1
fi
fi
shift
done
echo the biggest number is : $big
echo the smallest unmber is: $small
练习
#!/bin/bash
os() {
VERSION=`sed -nr 's#.* ([0-9+])\..*#\1#p' /etc/centos-release`
}
os
case $VERSION in
6)
echo -e "\e[1;$[RANDOM%7+31]mCentOS Linux release is $VERSION.\e[0m"
;;
7)
echo -e "\e[1;$[RANDOM%7+31]mCentOS Linux release is $VERSION.\e[0m"
;;
8)
echo -e "\e[1;$[RANDOM%7+31]mCentOS Linux release is $VERSION.\e[0m"
;;
*)
echo -e "\e[1;$[RANDOM%7+31]mUnknown release.\e[0m"
esac
#!/bin/bash
get_ip() {
ip a show eth0|awk -F' +|/' '/\/{print $3}'
}
get_ip
#!/bin/bash
. /etc/init.d/functions
print() {
action "success" true
action "failed" false
}
print
#!/bin/bash
. /etc/init.d/functions
args() {
[ -z $1 ] && action "failed...please give a argument." false
}
args $1
#!/bin/bash
max() {
[ $1 -gt $2 ] && echo -e "\e[1;$[RANDOM%7+31]mmax is $1\e[0m" || echo -e "\e[1;$[RANDOM%7+31]mmax is $2\e[0m"
}
max $1 $2
(1) 脚本可接受参数:start, stop, restart, status
(2) 如果参数非此四者之一,提示使用格式后报错退出
(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功”
考虑:如果事先已经启动过一次,该如何处理?
(4) 如是stop:则删除/var/lock/subsys/SCRIPT_NAME, 并显示“停止完成”
考虑:如果事先已然停止过了,该如何处理?
(5) 如是restart,则先stop, 再start
考虑:如果本来没有start,如何处理?
(6) 如是status, 则如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME is
running…”,如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is
stopped…”
(7)在所有模式下禁止启动该服务,可用chkconfig 和 service命令管理
说明:SCRIPT_NAME为当前脚本名
#!/bin/bash
# chkconfig: - 96 03
# description: This is testsrv script.
DIR=/var/lock/subsys/testsrv.sh
. /etc/init.d/functions
[ -f /etc/rc.d/init.d/`basename $0` ]||cp /data/scripts/`basename $0` /etc/rc.d/init.d/`basename $0`&>/dev/null
chkconfig --add testsrv.sh
start() {
[ -e $DIR ] || { touch $DIR;action "Starting testsrv:" true; }
}
stop() {
[ -e $DIR ] && { rm -f $DIR;action "Stopping testsrv:" true; }
}
status() {
[ -e $DIR ] && echo -e "\e[1;32mtestsrv is running...\e[0m" || echo -e "\e[1;31mtestsrv is stopped...\e[0m"
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
if [ -e $DIR ];then
stop
start
else
start
fi
;;
status)
status
;;
*)
echo -e "\e[1;31mUsage: /data/scripts/testsrv.sh {start|stop|restart|status}\e[0m"
esac
(1) 提示用户输入一个可执行命令名称
(2) 获取此命令所依赖到的所有库文件列表
(3) 复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下
如:/bin/bash ==> /mnt/sysroot/bin/bash
/usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd
(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下: 如:/lib64/ld-linux-x86-
64.so.2 ==> /mnt/sysroot/lib64/ld-linux-x86-64.so.2
(5)每次复制完成一个命令后,不要退出,而是提示用户键入新的要复制的命令,并重复完成上述
功能;直到用户输入quit退出
#!/bin/bash
ch_root="/mnt/sysroot"
[ ! -d $ch_root ] && mkdir $ch_root
bincopy() {
if which $1 &>/dev/null; then
local cmd_path=`which --skip-alias $1`
local bin_dir=`dirname $cmd_path`
[ -d ${ch_root}${bin_dir} ] || mkdir -p ${ch_root}${bin_dir}
[ -f ${ch_root}${cmd_path} ] || cp $cmd_path ${ch_root}${bin_dir}
return 0
else
echo "Command not found."
return 1
fi
}
libcopy() {
local lib_list=$(ldd `which --skip-alias $1` | grep -Eo '/[^[:space:]]+')
for loop in $lib_list;do
local lib_dir=`dirname $loop`
[ -d ${ch_root}${lib_dir} ] || mkdir -p ${ch_root}${lib_dir}
[ -f ${ch_root}${loop} ] || cp $loop ${ch_root}${lib_dir}
done
}
read -p "Please input a command: " command
while [ "$command" != "quit" ];do
if bincopy $command ;then
libcopy $command
fi
read -p "Please input a command or quit: " command
done