脚本 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 $?

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 root@172.25.254.40

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.

root@172.25.254.40'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 root@172.25.254.40

root@172.25.254.40'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 root@172.25.254.139

root@172.25.254.139'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的别名 

 

 创建别名过程中,如果已经有同名的别名存在,那么原有的别名设置将被新的设置取代。

本文转自AELY木博客51CTO博客,原文链接http://blog.51cto.com/12768057/1940220如需转载请自行联系原作者


AELY木

相关文章
|
1月前
|
JavaScript 前端开发 Go
脚本
脚本
26 3
|
6月前
|
Shell
脚本解释器脚本
脚本解释器脚本
21 1
|
9月前
|
Shell Perl
杀死所有脚本
杀死所有脚本
31 1
|
XML JSON jenkins
OCLint静态代码检查脚本
OCLint是静态代码检查工具,用于检查代码质量
387 0
|
文字识别 搜索推荐 机器人
【分享 10 个日常使用的脚本】
【分享 10 个日常使用的脚本】
110 0
|
Shell Linux 网络安全