腳本
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 $?
0
[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
#!/bin/bash
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 $? ##塊設備
1
[root@server0 ~]# [ -c /dev/zero ]; echo $? ##字符設備
0
[root@server0 ~]# [ -e /etc/passwd ]; echo $? ##表示存在
0
[root@server0 ~]# [ -f /etc/passwd ]; echo $? ##常規文件
0
[root@server0 ~]# [ -d /etc/passwd ]; echo $? ##目錄
1
[root@server0 ~]# [ -L /etc/passwd ]; echo $? ##鏈接
1
-S ##套接字
二進制文件運算符:-ef、-nt、-ot
-ef ##硬鏈接
-nt ##哪個文件更新(左比右)
-ot ##哪個文件更舊(左比右)
[root@server0 bin]# [ /bin/mount -ef /usr/bin/mount ]; echo $?
0
[root@server0 bin]# [ /bin/mount -nt /usr/bin/mount ]; echo $?
1
[root@server0 bin]# [ /bin/mount -ot /usr/bin/mount ]; echo $?
1
邏輯運算符:-o 或者、-a 並且、! 否定、&& 存在(之前的條件成立)、|| 不存在(條件不成立)
[root@server0 bin]# [ 2 -gt 1 -a 1 -gt 2 ]; echo $?
1
[root@server0 bin]# [ 2 -gt 1 -o 1 -gt 2 ]; echo $?
0
[root@server0 bin]# [ ! 2 -gt 1 ]; echo $?
1
3)
測試:
輸入需要測試的文件,判斷文件類型
#!/bin/bash
if
[ -z "$*" ]
then
echo "USE: /mnt/check_file.sh file|directory"
elif ##繼續判斷
[ ! -e "$*" ]
then
echo "$* is not exit!!"
elif
[ -L "$*" ]
then
echo "$* is a soft link" ##是個軟鏈接
fi
同上,使用的相同的語法加入其他類型。
[root@desktop39 mnt]# /mnt/check_file.sh
USE: /mnt/check_file.sh file|directory
4)
測試:
查看用戶是否建立,並更改密碼爲westos
#!/bin/bash
if
[ -z "$1" ]
then
echo please give me a userfile
elif
[ ! -e "$1" ]
then
echo "$1 is not exist!!"
else
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
fi
[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.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
5)
當腳本後面接create時,建立/mnt/userfile的用戶,當腳本後面接delete,刪除/mnt/userfile的用戶。
#!/bin/bash
if
[ "$#" -lt "2" ]
then
echo "Usage: /mnt/ctrl_user.sh <create|delete> <userfile>"
fi
if
[ "$1" = "create" ]
then
if
[ -z "$2" ]
then
echo please give me a userfile
elif
[ ! -e "$2" ]
then
echo "$2 is not exist"
else
for NAME in `cat $2`
do
USER=`getent passwd $NAME`
[ -z $USER ]&&(
useradd $NAME &> /dev/null
echo westos | passwd --stdin $NAME &>/dev/null
)||(
echo "$NAME is exist"
)
done
fi
elif
[ "$1" = "delete" ]
then
if
[ -z "$2" ]
then
echo please give me a userfile
elif
[ ! -e "$2" ]
then
echo "$2 is not exist"
else
for NAME in `cat $2`
do
USER=`getent passwd $NAME`
[ -n $USER ]&&(
userdel -r $NAME &> /dev/null
)||(
echo "$NAME is not exist"
)
done
fi
fi
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
再次連接:
[root@localhost mnt]# expect autossh.exp 172.25.254.40
spawn ssh [email protected]
[email protected]'s password:
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
#!/bin/bash
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
#!/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#監控/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
spawn /mnt/ask.sh
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
1
[root@localhost ~]# vim file
[root@localhost ~]# sh file
[root@localhost ~]# export a=1
[root@localhost ~]# sh file
1
[root@localhost mnt]# exit
logout
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 ~]# echo $a
[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的別名
創建別名過程中,如果已經有同名的別名存在,那麼原有的別名設置將被新的設置取代。