版本,CPU型号,内存大小,硬盘大小
#!/bin/bash
CL="\033[1;32m"
END='\033[0m'
IPV4=`ifconfig eth0 |grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -n1`
VS=`uname -a`
KN=`uname -r`
CPU=`lscpu |grep 'Model name' |tr -s ' ' |cut -d: -f2`
MEMORYSIZE=`free -h |grep 'Mem' |tr -s ' ' : |cut -d: -f2`
DISKSIZE=`lsblk |grep '^sd' |tr -s ' ' |cut -d ' ' -f1,4 |paste -s -d, `
echo -e "Hostname is : ${CL}${HOSTNAME}${END}"
echo -e "IPV4 adress is : ${CL}${IPV4}${END}"
echo -e "system's version : ${CL}${VS}${END}"
echo -e "Kernel version is : ${CL}${KN}${END}"
echo -e "CPU version is : ${CL}${CPU}${END}"
echo -e "Memory's size is : ${CL}${MEMORYSIZE}${END}"
echo -e "Disk size is : ${CL}${DISKSIZE}${END}"
#!/bin/bash
CL="\033[1;32m"
END="\033[0m"
DT=`date +%F_%T`
cp -av /etc /data/backup/etc_back.${DT}
sleep 3
echo -e " ${CL}Done!${END} "
#!/bin/bash
CL="\033[1;33m"
END="\033[0m"
TLN=`df |tr -s ' ' % |cut -d% -f5 |sort -nr |head -n1`
echo -e "The largest rate used of disk partition is :${CL}${TLN}${END}"
~
TLN=`ss -nt |ss -nt |tail -n +2 |tr -s ' ' ':' |cut -d: -f4 |uniq -c |sort -nr`
ADDR=`ss -nt |ss -nt |tail -n +2 |tr -s ' ' ':' |cut -d: -f4 `
echo -e "Display the remote host connect's address and number of times:\n${ADDR} "
echo -e "sort positive sequence the num of address connect: ${TLN}"
~
[[ ]]
read -p "Please input the username :" NAME
INFO="echo `id ${NAME}`"
if
id ${NAME} &> /dev/null
then
echo -e "\033[1;32mThe user ${NAME} is exist,and ${NAME}'s information:\n${INFO}\033[0m"
else
echo -e "The user ${NAME} dont exist "
echo -e "\033[1;31mI will crate the account ${NAME},please choice "yes" or "no" \033[0m"
read -p "Please choice :" YN
if
[[ ${YN} =~ ^([Yy]|[Ee]|[Ss])$ ]];
then
useradd ${NAME};
echo "${NAME}:123456" |chpasswd
chage -d1 ${NAME}
echo "The default password is 123456 ,you must change your passwd next login (force)"
else
echo -e "\033[1;33mwill leave and exit!!!\033[0m"
fi
fi
1.读入
read -p "提示语" A
gaga
echo $A
gaga
2.变量赋值与输出
变量赋值A=“ ” A=" echo \`id ${NAME}\` "
NAME=a
echo $NAME
1
echo${NAME}
1
NAME="`cat /dev/urandom |tr -dc '[[:alnum:]]' |head -c7` "
echo $NAME
tyBThpi
echo ${NAME}
tyBThpi
赋值打印seq的结果
NAME=`seq 10`
echo $NAME
1 2 3 4 5 6 7 8 9 10
echo ${NAMR}
1 2 3 4 5 6 7 8 9 10
echo "$NAME"
1
2
3
4
5
6
7
8
9
10
echo "${NAME}"
1
2
3
4
5
6
7
8
9
10
3.命令静默并传回值
id ${NAME} &> /dev/null
插入(ls xxx > /dev/null 2&>1;echo $?)回传的值总是错误
4.条件性测试
[ ]
[[ ${YN} =~ ^([Yy]|[Ee]|[Ss])$ ]]支持摸估,==通配符摸估。=~正则表达式摸估
5.非交互式修改口令
echo “name:passwd ” |chpasswd
6.账户过期设置(下次登录必须修改口令)
chage -d1 ${NAME}
passwd -e ${NAME}
chage -d参数可以将到期时间修改为指定天数
passwd -e 参数可以强制用户下次登录强制修改口令
7.if,else嵌套语句结构
if
then
else
if
then
else
fi
fi
#!/bin/bash
WARNINFO="`df |grep -E '^/dev/sd*' |tr -s ' ' |cut -d ' ' -f1,5,6 |sort -nr -k2 |head -n3`"
#LARSET="`df |grep -E '^/dev/sd*' |tr -s ' ' |cut -d ' ' -f1,5,6 |sort -nr -k2 |head -n1`"
#LARSET="`echo ${WARNINF} |tr -s ' ' '%' |cut -d% -f2 |sort -nr|head -n1`"
VALURE="`echo $WARNINFO |tr -s '%' ' '|sort -nr |head -n1|cut -d ' ' -f2 `"
if
[[ $VALURE -ge 10 ]]
then
echo -e "Your host's disk rate is more than 10%\n${WARNINFO}" |mail -v -s "disk_rate " [email protected]
echo "The rate is ${VAlURE} "
else
echo "正常"
fi
修改环境变量
1.临时修改变量
PATH=$PATH:/usr/local/apache/bin
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin
2.永久修改环境变量
vim ~/.bashrc
PATH=$PATH:$HOME:usr/local/apache/bin
vim /etc/profile.d/~/.bashrc
PS1=\033[1;31m \033[0m
rm="rm-i"
cdnet="cd /etc/sysconfig/network-scripts/"
登录前信息显示:
vim /etc/issue.d/*.sh
Hi,dangerous!
登陆后信息显示:
vim /etc/motd
welcome to liujiajun's computer!!!
vim /etc/vimrc
vim ~/.vimrc
请参考此链接内的题目1
read -p "please input the adress" ADDRESS
if
ping -c2 -w2 ${ADDRESS} &> /dev/null
then
echo "${ADDRESS} is up"
elif
grep -q "${ADDRESS}" ~/maintenance.txt
then
echo "${ADDRESS} is undergoing maintenance"
else
echo "station is unexpecedly DOWN!"
fi
read -p "Please input the username :" NAME
INFO="echo `id ${NAME}`"
if
id ${NAME} &> /dev/null
then
echo -e "\033[1;32mThe user ${NAME} is exist,and ${NAME}'s information:\n${INFO}\033[0m"
else
echo -e "The user ${NAME} dont exist "
echo -e "\033[1;31mI will crate the account ${NAME},please choice "yes" or "no" \033[0m"
read -p "Please choice :" YN
if
[[ ${YN} =~ ^([Yy]|[Ee]|[Ss])$ ]];
then
useradd ${NAME};
echo "${NAME}:123456" |chpasswd
chage -d1 ${NAME}
echo "The default password is 123456 ,you must change your passwd next login (force)"
else
echo -e "\033[1;33mwill leave and exit!!!\033[0m"
fi
fi
read -p "Do you agree(yes/no)?" INPUT
VALUE=`echo $INPUT |tr -s 'A-Z' 'a-z'`
case ${INPUT} in
yes|ye|y)
echo "you input is YES"
;;
no|n)
echo "you input is NO"
;;
*)
echo "you input is others"
esac
read -p "Please input the file name what you want to test:" FILE
if
[ -f ${FILE} ]
then
echo "Its a normal file!"
elif
[ -d ${FILE} ]
then
echo "Its a directory!"
elif
[ -l ${FILE} ]
then
echo"Its a link file!"
elif
[ -b ${FILE} ]
then
echo "Its a block file!"
elif
[ -c ${FILE} ]
then
echo "Its a character file!"
elif
[ -s ${FILE} ]
then
echo "Its a socket file!"
elif
[ -p ${FILE} ]
then
echo "Its a pipe file!"
fi
read -p "Please input the number:" NUM
if
# [ -n "`echo ${NUM} |sed 's/[0-9]/g/'`" ] && [ ${NUM} -gt 0 ]
[[ ${NUM} =~ ^([0-9]+)$ ]] && [ ${NUM} -gt 0 ]
then
echo "The number is positive int"
else
echo "The number is not positive int"
fi
~
正在完善中。。。
方法一、
if
[ $? -ne 0 ]
then
return $?
方法二、
set -e 加set -e参数,但凡脚本出现非0返回值,中断整个脚本
read -p "please input the directory : " DIR
for i in `ls ${DIR}`
do
if
[ -b ${DIR}/${i} ]
then
echo "${i} is a block file"
elif
[ -c ${DIR}/${i} ]
then
echo "${i} is a character file"
elif
[ -f ${DIR}/${i} ]
then
echo "${i} is a normal file"
elif
[ -h ${DIR}/${i} ]
then
echo "${i} is a link file"
elif
[ -p ${DIR}/${i} ]
then
echo "${i} is a pipe file"
elif
[ -d ${DIR}/${i} ]
then
echo "${i} is a directory!"
elif
[ -s ${DIR}/${i} ]
then
echo "${i} is a socket file"
fi
done
#!/bin/bash
for i in {1..10}
do
useradd user${i}
pass=`cat /dev/urandom |tr -dc '[[:alnum:]]' |head -c 8 `
echo ${pass} |passwd --stdin
echo ${pass} >> /data/passwd.txt
done
echo "the user user{i} is created!"
for i in `ls /etc/rc.d/rc3.d`
do
if
[ "$(echo ${i} |cut -c1)" == "S" ]
then
echo -e "${i} start"
else
echo -e "${i} stop"
fi
done
#!/bin/bash
read -p "please input positive int n" N
for((sum=0,i=1;i<=${N};sum+=i,i++))
do
true
done
echo "the result is ${sum}"
#!/bin/bash
sum=0
for i in {1..100}
do
if
[ $[${i}%3] -eq 0 ]
then
sum=$[$sum+$i]
fi
done
echo "the result is $sum"
read -p "please input the adress" ADDRESS
if
ping -c2 -w2 ${ADDRESS} &> /dev/null
then
echo "${ADDRESS} is up"
elif
grep -q "${ADDRESS}" ~/maintenance.txt
then
echo "${ADDRESS} is undergoing maintenance"
else
echo "station is unexpecedly DOWN!"
fi
mkdir -p /data/num_uradom
for i in {1..10}
do
cd /data/num_urandom
touch ${i}`cat /dev/urandom |tr -dc '[[:alnum:]]' |head -c8`.html
done
for((i=1;i<=9;i++))
do
for((j=9;j>=i;j--))
do
echo -n " "
done
for((j=2;j<=i;j++))
do
echo -n "*"
for((j=1;j<=i;j++))
do
echo -n "*"
done
done
done
#!/bin/bash
sum=1
for ((n=1;n<10;n++));do
??sum=$[2*$[$sum+1]]
done
echo "所摘桃子数: $sum"``
for((i=1;i<=8;i++));do
for((j=1;j<=8;j++));do
let sum=("$i"+"$j")%2
if [ "${sum}" -eq 1 ];then
echo -ne "\033[1;43m \033[0m"
else
echo -ne "\033[1;47m \033[0m"
fi
done
echo
done
until who |grep -q '^panda' ;do
sleep 3
done
who |grep '^hacker' |tr -s ' ' |cut -d ' ' -f1,3,5 >>/var/log/login.log
echo "日志信息已记录"
read -p "Please input a little filename: " FILES
while [ -n "$FILES" ];do
echo -e "\033[1;33m lines are `cat $FILES |wc -l` \e[0m"
break
done
两个数字比较:
if [ $# -ne 2 ];then
echo -e "\033[1;31m input two number at least!!! \033[0m"
elif [[ $1 =~ ^[0-9]+$ ]] && [[ $2 =~ ^[0-9]+$ ]];then
if [ $1 -lt $2 ];then
echo "the largest number is \$2:$2 ,the less number is \$1:$1"
elif [ $1 -gt $2 ];then
echo "the largest number is \$1:$1 ,the less number is \$2:$2"
else
echo "two number is queal"a
fi
else
echo -e "\033[1,31m please input number!!!\33[0m"
fi
大于两个数字以上:
#!/bin/bash
declare -i max min
declare -a nums
sum=$#
nums=($@)
for((i=0;i<=$sum;i++));do
[ $i -eq 0 ] && max=${nums[0]} && min=${nums[0]} && continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
[ ${num[$i]} -lt $min ] && min=${nums[$i]}
done
echo "all the parameter are : $@ "
echo "the max parameter is : $max"
echo "the min parameter is : $min"
注意:此处写成case模式更为繁琐,是为了实现更高级架构而设计成此
version=`cat /etc/redhat-release | grep -o '[[:digit:]]' |head -c1`
case ${version} in
4)
echo "the version is :CentOS${version}"
;;
5)
echo "the version is :CentOS${version}"
;;
6)
echo "the version is :CentOS${version}"
;;
7)
echo "the version is :CentOS${version}"
;;
8)
echo "the version is :CentOS${version}"
esac
#ifconfig eth0 |sed -nr '2s/[^0-9]+([0-9.]+).*/\1/p'
#ifconfig eth0 |head -n2 |tail -n1 |tr -s ' ' |cut -d ' ' -f3
eth0(){
ifconfig eth0 |grep -w 'inet' |tr -s ' ' |cut -d ' ' -f3
}
eth0
okorfailed(){
if [ $? = 0 ];then
echo -e "\033[32m [ OK ]\033[0m"
else
echo -e "\033[1;31m [ Failed ] \033[0m"
fi
}
okorfailed
方法二:调用/etc/init.d/functions
#!/bin/bash
. /etc/init.d/functions
function ok(){
echo_success
}
function failure(){
echo_failure
}
function arg (){
if [ $# -eq 0 ];then
echo "please input a arg!"
else
echo "total $# arg"
fi
}
arg 1 2 3 4
~
#!/bin/bash
declare -i max min
declare -a nums
nums=($*)
for((i=0;i<=$#;i++));do
[ $i -eq 0 ] && max=${nums[0]} && min=${nums[0]} && continue
[ ${nums[$i]} -gt $max ] && max=${sums[$i]} && continue
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "the max parameter is $max"
(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/locksubsys/SCRIPT_NAME文件不存在,则线回收“SCRIPT_NAME is stopped…”
(7)在所有模式下禁止启动该服务,可用chkconfig和service
命令管理
说明:SCRIPT_NAME为当前脚本
#!/bin/bash
name=$0
dir=/var/lock/subsys/
function judge(){
if [[ ! $arg =~ "start"|"stop"|"restart"|"status" ]];then
echo "please in put start、stop、restart、status"
fi
exit
}
function start1(){
mkdir -p ${dir}
touch ${dir}${name}
echo "launch successful!"
}
function stop1(){
if [ -e ${dir}${name} ];then
rm -rf ${dir}${name}
echo "stop success!"
else
echo "already stop!"
fi
}
function restart1(){
if [ ! -e ${dir}${name} ];then
stop1
start1
fi
}
function status1(){
if [ -e ${dir}${name} ];then
echo "${dir}${name} is running!"
else
echo "${dir}${name} is stopped..."
fi
}
cat<<EOF
input the parameter:
start)
stop)
restart)
status)
EOF
read -p "please input :" arg
judge
case arg in
start)
start1
;;
stop)
stop1
;;
restart)
restart1
;;
status)
status1
;;
*)
echo "please input the right parameter!"
exit 1
esac
(1)提示用户输入一个可执行命令名称
(2)获取此命令所依赖的所有库文件列表
(3)复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下
如: /bin/bash==>/mnt/sysroot/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退出。
知识点:
1.读取用户输入read命令,-p参数加上提示语句
2.获取命令所在文件所依赖的库列表,ldd (后面跟命令文件位置)
3.which +cmd 可以查出命令文件所在的位置。但是如果命令有别名,也会显示命令alias,可以加参数 --skip-alias 跳过别命名输出。
PASS=abc123
NET=10.0.0.
rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
ssh-keygen -P "" -f /root/.ssh/id_rsa
for i in {3..254};do
{
sshpass -p ${PASS} ssh-copy-id -i /root/.ssh/id_rsa ${NET}${i} -o StrictHostKeyChecking=no &> /dev/null
}&
done
wait
###########################################################
NET=10.0.0.
rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
ssh-keygen -P "" -f /root/.ssh/id_rsa
for i in {3..254};do
sshpass -f /data/passwd.txt ssh-copy-id -i /root/.ssh/id_rsa ${NET}${i} -o StrictHostKeyChecking=no &> /de
v/null
done
wait
########################################################
rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
while read ip pass ;do
sshpass -p ${pass} ssh-copy-id -i /root/.ssh/id_rsa ${IP} -O StrictHostKeyChecking=no &>/dev/null
done</data/account.txt
wait