天天看點

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--))

echo -ne "After  ${a}s  is GG "#-n 不換行;-e執行

2) 1MIN10S倒計時

MIN=1

for ((SEC=10;SEC>=0;SEC--))

echo -ne " After ${MIN}m - ${SEC}s is end "

while [ "$SEC" -le "0" -a "$MIN"-gt "0" ]

echo -ne "After ${MIN}:${SEC}s is end "

((MIN--))

SEC=60;

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 >=

for ((a=1;a<=10;a++))

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

        [ "$?" -ne "0" ]

        echo "172.25.254.${a} is down"

4).備份資料庫

for a in $(mysql -uroot -predhat -e "show databases;"   -NE | grep -E "^\*|schema$" -v )

mysqldump -uroot -predhat $a  > /mnt/abc

ping -c1 -w1 172.25.254.${a} &>/dev/null

[]數字運算比較符 -z 為空 -n 不為空

 -eq 等于-lt小于 -le小于等于 -gt 大于 -ge大于等于

檔案狀态運算符

-d 裝置 -c字元 -e是否可執行 -L軟連結 -d目錄 -f普通檔案

二進制檔案運算符

-ef 比較是否互為硬連結

-nt比較兩個檔案的時間戳哪個更新  或者-ot

邏輯運算符

-o或者

-a并且

!否

&&如果成立的話執行

||如果不成立的話執行

5).檢查檔案屬性

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

[ -n "$1" -a -n "$2" ]

[ -e "$1" -a -e "$2" ]

MAXUSER=`wc -l $1 | cut -d " " -f 1`

MAXPASS=`wc -l $2 | cut -d " " -f 1`

[ "$MAXUSER" -eq "$MAXPASS" ]

for NUM in $( seq 1 $MAXUSER )

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 !!"

else

echo $1 and $2 have different lines

elif

[ ! -e "$1" ]

echo "ERROR:$1 is not exsit"

echo "ERROR:$2 is not exsit"

echo "ERROR: Please input userfile and password-file after command !!"

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

本文轉自   Taxing祥   51CTO部落格,原文連結:http://blog.51cto.com/12118369/1883143

上一篇: keepalived
下一篇: apache