天天看點

腳本 2

腳本

test條件判斷

test指令可用于評估bash腳本中的表達式。它評估其參數所指定的表達式,如果表達式為true,傳回零退出狀态,如果表達式為false,則傳回非零退出狀态。test具有替代文法,使用方括号"[]"将表達式括起來,這樣更易于閱讀。

文法:test EXPRESSION 或 [EXPRESSION]

非零或零長度字元串運算符:test -{n|z} STRING

test檢測指令

 -n     不為空

 -z     為空

[root@server0 ~]# [ -n westos ]; echo $?

0    ##條件為空,與-n成立。(退出狀态為0,表示前面條件成立)

[root@server0 ~]# [ -z westos ]; echo $?

1    ##條件不成立

[root@desktop39 ~]# a=1

[root@desktop39 ~]# [ -n a ];echo $?

[root@desktop39 ~]# [ -z a ];echo $?

1

1)

實驗:

輸入運作腳本,加ip時判斷是否能ping通,不加時請求輸入ip

[root@desktop39 ~]# cat /mnt/check_ip.sh

#!/bin/bash

[ -n "$*" ] &&(

 ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down

)||(

 echo "Please input an ipaddress:"

)

[root@desktop39 ~]# /mnt/check_ip.sh

Please input an ipaddress:

[root@desktop39 ~]# /mnt/check_ip.sh 172.25.254.39

172.25.254.39 is up

數字比較運算符:-eq 等于、-ne 不等于、-lt 小于、-le 小于等于、-gt 大于 、-ge 大于等于

2)

輸入兩個數,和10比較大小

[root@desktop39 ~]# cat /mnt/check_num.sh

while

[ -z "$1" -o -z "$2"]

do

echo "Please give me two num after /mnt/check_num.sh"

exit 1

done

 NUM=$[ $1 + $2 ]

if [ "$NUM" -ge "10" ]

then

echo "the result is over 10"

else

echo "the result is lower than 10"

fi

[root@desktop39 ~]# /mnt/check_num.sh 2 18

the result is over 10

[root@desktop39 ~]# /mnt/check_num.sh 2 7

the result is lower than 10

檔案狀态運算符:test -{b|c|e|f|d|r|w|x|s|L} FILE/DIRECTORY

          -                ##檔案

[root@server0 ~]# [ -b /dev/sda ]; echo $?    ##塊裝置

[root@server0 ~]# [ -c /dev/zero ]; echo $?    ##字元裝置

[root@server0 ~]# [ -e /etc/passwd ]; echo $?    ##表示存在

[root@server0 ~]# [ -f /etc/passwd ]; echo $?    ##正常檔案

[root@server0 ~]# [ -d /etc/passwd ]; echo $?     ##目錄

[root@server0 ~]# [ -L /etc/passwd ]; echo $?    ##連結

    -S    ##套接字

二進制檔案運算符:-ef、-nt、-ot

-ef    ##硬連結

-nt    ##哪個檔案更新(左比右)

-ot    ##哪個檔案更舊(左比右)

[root@server0 bin]# [ /bin/mount -ef /usr/bin/mount ]; echo $?

[root@server0 bin]# [ /bin/mount -nt /usr/bin/mount ]; echo $?

[root@server0 bin]# [ /bin/mount -ot /usr/bin/mount ]; echo $?

邏輯運算符:-o 或者、-a 并且、! 否定、&& 存在(之前的條件成立)、|| 不存在(條件不成立)

[root@server0 bin]# [ 2 -gt 1 -a 1 -gt 2 ]; echo $?

[root@server0 bin]# [ 2 -gt 1 -o 1 -gt 2 ]; echo $?

[root@server0 bin]# [ ! 2 -gt 1 ]; echo $?

3)

測試:

輸入需要測試的檔案,判斷檔案類型

if

[ -z "$*" ]

echo "USE: /mnt/check_file.sh file|directory"

elif    ##繼續判斷

[ ! -e "$*" ]

echo "$* is not exit!!"

elif

[ -L "$*" ]

echo "$* is a soft link"    ##是個軟連結

同上,使用的相同的文法加入其他類型。

[root@desktop39 mnt]# /mnt/check_file.sh

USE: /mnt/check_file.sh file|directory

4)

檢視使用者是否建立,并更改密碼為westos

[ -z "$1" ]

        echo please give me a userfile

[ ! -e "$1" ]

        echo "$1 is not exist!!"

        for NAME in `cat $1`

        do

                USER=`getent passwd $NAME`

                if

                [ -z "$USER" ]

                then

                        useradd $NAME

                        echo westos | passwd --stdin $NAME

                else

                        echo $NAME is exist!!

                fi

        done

[root@desktop39 mnt]# /mnt/user.sh

please give me a userfile

[root@desktop39 mnt]# /mnt/user.sh /mnt/userfile

Changing password for user user1.

passwd: all authentication tokens updated successfully.

Changing password for user user2.

Changing password for user user3.

5)

當腳本後面接create時,建立/mnt/userfile的使用者,當腳本後面接delete,删除/mnt/userfile的使用者。

[ "$#" -lt "2" ]

echo "Usage: /mnt/ctrl_user.sh <create|delete> <userfile>"

[ "$1" = "create" ]

[ -z "$2" ]

[ ! -e "$2" ]

        echo "$2 is not exist"

        for NAME in `cat $2`

        USER=`getent passwd $NAME`

        [ -z $USER ]&&(

        useradd $NAME &> /dev/null

        echo westos | passwd --stdin $NAME &>/dev/null

        )||(

        echo "$NAME is exist"

        )

[ "$1" = "delete" ]

        [ -n $USER ]&&(

        userdel -r $NAME &> /dev/null

        echo "$NAME is not  exist"

6)非互動連接配接指定ip

[root@localhost mnt]# cat autossh.exp

#!/usr/bin/expect

set ip [ lindex $argv 0 ]

spawn ssh root@$ip

expect {

"(yes/no)" {send "yes\r";exp_continue}    ##exp_continue表示如果有這個問題,就回答,沒有往下繼續

"password: " {send "westos\r"}

}

interact

第一次連接配接:

[root@localhost mnt]# expect autossh.exp 172.25.254.40

spawn ssh [email protected]

The authenticity of host '172.25.254.40 (172.25.254.40)' can't be established.

ECDSA key fingerprint is 7d:08:66:0c:75:5c:8e:5b:26:53:3c:a0:df:28:63:ef.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.25.254.40' (ECDSA) to the list of known hosts.

[email protected]'s password:

Last login: Sun Jun 18 07:25:57 2017 from foundation41.ilt.example.com

再次連接配接:

Last login: Sun Jun 18 07:47:08 2017 from foundation139.ilt.example.com

[root@foundation40 ~]# ls

anaconda-ks.cfg             rht-ks-post.log

foundation-config-post.log  rht-ks-pre.log

[root@foundation40 ~]# exit

logout

Connection to 172.25.254.40 closed.

例7:expect的使用

yum install expect -y

[root@desktop38 ~]# vim /mnt/ask.sh

read -p "Who are you: " NAME

read -p "How old  are you: " AGE

read -p "what's you class: " CLASS

read -p "Are you happy: " FEEL

echo $NAME is $AGE old study $CLASS and feel $FEEL

[root@desktop38 ~]# vim answer.exp

set NAME  [ lindex $argv 0 ]    ##變量

set AGE   [ lindex $argv 1 ]

set CLASS [ lindex $argv 2 ]

set FEEL  [ lindex $argv 3 ]

spawn /mnt/ask.sh#監控/mnt/ask.sh

expect "Who"##\r換行

send "$NAME\r"

expect "How"

send "$AGE\r"

expect "class"

send "$CLASS\r"

expect "happy"

send "$FEEL\r:]"

expect eof    ##退出expect

[root@desktop38 ~]# expect answer.exp

spawn /mnt/ask.sh

Who are you:

How old  are you:

what's you class:

Are you happy:

is old study and feel

[root@desktop38 ~]# expect answer.exp lee 18 linux happy

Who are you: lee

How old  are you: 18

what's you class: linux

Are you happy: happy

lee is 18 old study linux and feel happy

變量的種類:

使用者級變量:隻針對使用者生效~/.bash_profile

系統級變量(先讀):所有使用者都生效/etc/profile

環境級變量(一次性):隻在目前環境生效

使用者自定義變量檔案/mnt/.bash_profile去初始化它們的環境變量

環境變量

shell和腳本使用變量來存儲資料 ,有些變量可以連同它們的内容傳遞給子程序,這些變量我們稱之為環境變量。

在使用者登入的時候,會運作全局變量檔案/etc/profile,和使用者自定義變量檔案~/.bash_profile去初始化它們的環境變量。

例:使用export a=1,能夠使bash下的子bash也識别該變量,但是,當該bash關閉後,再次登入後就識别不了

[root@localhost ~]# a=1

[root@localhost ~]# echo $a

[root@localhost ~]# vim file

[root@localhost ~]# sh file

[root@localhost ~]# export a=1

[root@localhost mnt]# exit

Connection to 172.25.254.139 closed.

[root@foundation39 ~]# ssh [email protected]

[email protected]'s password:

Last login: Sat Jun 17 19:36:23 2017 from 172.25.254.39

[root@localhost ~]#

使用env指令顯示所有環境變量

[root@localhost mnt]# env

XDG_SESSION_ID=52

HOSTNAME=localhost.localdomain

TERM=xterm-256color

SHELL=/bin/bash

HISTSIZE=1000

SSH_CLIENT=172.25.254.39 59619 22

SSH_TTY=/dev/pts/2

USER=root

LS_COLORS=rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:

MAIL=/var/spool/mail/root

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

PWD=/mnt

LANG=en_US.UTF-8

HISTCONTROL=ignoredups

SHLVL=1

HOME=/root

LOGNAME=root

SSH_CONNECTION=172.25.254.39 59619 172.25.254.139 22

LESSOPEN=||/usr/bin/lesspipe.sh %s

XDG_RUNTIME_DIR=/run/user/0

OLDPWD=/root

_=/usr/bin/env

使用别名

别名就是一直能夠便捷的方式,以省去使用者輸入一長串指令序列的麻煩。 

alias指令可以用來自定義屬于自己的系統指令,寫入~/.bashrc 檔案永久生效。

設定别名:

# alias mycom='echo hello;hostname'

# mycomm

hello

server0.example.com

删除别名: 1.unalias mycomm

     2.将其對應語句(如果有的話)從~/.bashrc中删除

     3.或者使用指令alias example = ,這會取消名為example的别名 

 建立别名過程中,如果已經有同名的别名存在,那麼原有的别名設定将被新的設定取代。

本文轉自 yab109 51CTO部落格,原文連結:http://blog.51cto.com/12768057/1940220,如需轉載請自行聯系原作者

上一篇: pxe