首页 > 代码库 > shell
shell
1.vim .bash_profile
export PATH
export a=1
export PATH=$PATH:/
source .bash_profile#使更新生效
基本运算
++自加 -- 自减 **幂运算 /除法 %余数 +=加等 -=减等
倒计时
2.for;do;done 语句
[root@mail ~]# for NAME in tom jack westos;do echo This is $NAME;done
This is tom
This is jack
This is westos
for循环
[root@mail ~]# for ((i=1;i<=10;i++));do echo $i;done
1
2
3
4
5
6
7
8
9
10
1) 10S倒计时
#!/bin/bash
for ((b=10;b>0;b--))
do
echo -ne "After 1M${b}s is GG "
echo -ne "\r \r"
sleep 1
done
for ((a=60;a>0;a--))
do
echo -ne "After ${a}s is GG "#-n 不换行;-e执行
echo -ne "\r \r"
sleep 1
done
2) 1MIN10S倒计时
#!/bin/bash
MIN=1
for ((SEC=10;SEC>=0;SEC--))
do
echo -ne " After ${MIN}m - ${SEC}s is end "
echo -ne "\r \r"
sleep 1
while [ "$SEC" -le "0" -a "$MIN"-gt "0" ]
do
echo -ne "After ${MIN}:${SEC}s is end "
echo -ne "\r \r"
((MIN--))
SEC=60;
done
done
3)ping 机器
#!/bin/bash
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM &>/dev/null && echo 172.25.254.$NUM is UP || ec ho 172.25.254.$NUM is down
done
while
le <=
a 并且
gt >=
#!/bin/bash
for ((a=1;a<=10;a++))
do
ping -c1 -w1 172.25.254.${a} &>/dev/null#c1 ping 1 次 w1 等一秒
while
[ "$?" -eq "0" ]
do
echo "172.25.254.${a} is up"
break
done
while
[ "$?" -ne "0" ]
do
echo "172.25.254.${a} is down"
break
done
done
4).备份数据库
#!/bin/bash
for a in $(mysql -uroot -predhat -e "show databases;" -NE | grep -E "^\*|schema$" -v )
do
mysqldump -uroot -predhat $a > /mnt/abc
done
#!/bin/bash
for ((a=1;a<=10;a++))
do
ping -c1 -w1 172.25.254.${a} &>/dev/null
while
[ "$?" -eq "0" ]
do
echo "172.25.254.${a} is up"
break
done
while
[ "$?" -ne "0" ]
do
echo "172.25.254.${a} is down"
break
done
done
[]数字运算比较符 -z 为空 -n 不为空
-eq 等于-lt小于 -le小于等于 -gt 大于 -ge大于等于
文件状态运算符
-d 设备 -c字符 -e是否可执行 -L软链接 -d目录 -f普通文件
二进制文件运算符
-ef 比较是否互为硬链接
-nt比较两个文件的时间戳哪个更新 或者-ot
逻辑运算符
-o或者
-a并且
!否
&&如果成立的话执行
||如果不成立的话执行
5).检查文件属性
#!/bin/bash
if
[ -e "$1" ]
then
[ -f "$!" -a ! -L "$1" ] && echo $1 is a file
[ -b "$1" ] && echo $1 is a block
[ -c "$1" ] && echo $1 is a c-block
[ -n "$1" ] && echo $1 is not exist || echo "Please give me a file "
fi
6).创建用户
#!/bin/bash
if
[ -n "$1" -a -n "$2" ]
then
if
[ -e "$1" -a -e "$2" ]
then
MAXUSER=`wc -l $1 | cut -d " " -f 1`
MAXPASS=`wc -l $2 | cut -d " " -f 1`
if
[ "$MAXUSER" -eq "$MAXPASS" ]
then
for NUM in $( seq 1 $MAXUSER )
do
USERNAME=`sed -n ${NUM}p $1`
PASSWORD=`sed -n ${NUM}p $2`
CKUSER=`getent passwd $USERNAME`
[ -z "$CKUSER" ]&&(
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
)||echo "$USERNAME exit !!"
done
else
echo $1 and $2 have different lines
fi
elif
[ ! -e "$1" ]
then
echo "ERROR:$1 is not exsit"
else
echo "ERROR:$2 is not exsit"
fi
else
echo "ERROR: Please input userfile and password-file after command !!"
fi
6.case语句
case $1 in
apple)
echo banana
;;
banana)
echo apple
;;
*)
echo error
esac
7.expect
yum install expect -y
#!/usr/bin/expect
set name [ lindex $argv 0 ]
set age [ lindex $argv 1 ]
set class [ lindex $argv 2 ]
set feel [ lindex $argv 3 ]
spawn /mnt/ask.sh
expect "name:"
send "$name\r"
expect "old"
send "$age\r"
expect "class"
send "#class\r"
expect "happy"
send "$fil\r"
expect eof
shell