shell2

實驗12.

case語句

[root@web1 ~]# vim 大哥是個apple

case $1 in

        apple)

        echo banana

        ;;

        banana)

        echo apple

        ;;

        *)

        echo error

esac

測試:

[root@web1 ~]# sh 大哥是個apple apple

banana

[root@web1 ~]# sh 大哥是個apple banana

apple

[root@web1 ~]# sh 大哥是個apple yz

error

 

實驗13.

expect自動應答腳本(輸入ip和密碼,制動遠程連接主機)

[root@web1 ~]# yum install expect -y

Loaded plugins: langpacks

rhel_dvd                                                 | 4.1 kB     00:00     

Resolving Dependencies

.....

[root@web1 mnt]# vim ty16.exp

#!/usr/bin/expect

set IP [ lindex $argv 0 ]

set PASS [ lindex $argv 1 ]

spawn ssh root@$IP

expect {

        "yes/no"

        {send "yes\r";exp_continue}

        "password:"

        {send "$PASS\r"}

        }

interact

 

[root@web1 mnt]# /mnt/ty16.exp 172.25.254.118 redhat

spawn ssh [email protected]

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

ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.

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

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

[email protected]'s password:

Last login: Wed Dec 14 21:58:09 2016 from 172.25.254.18

[root@server18 ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.25.254.118  netmask 255.255.255.0  broadcast 172.25.254.255

        inet6 fe80::5054:ff:fe00:120b  prefixlen 64  scopeid 0x20<link>

        ether 52:54:00:00:12:0b  txqueuelen 1000  (Ethernet)

        RX packets 16575  bytes 1458237 (1.3 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 9690  bytes 1204275 (1.1 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 

實驗14.

教室11-25主機開着的顯示主機名,未開就顯示關閉

[root@web1 mnt]# vim ty16.exp

#!/usr/bin/expect

set timeout 2

set IP [ lindex $argv 0 ]

set PASS [ lindex $argv 1 ]

set COMM [ lindex $argv 2 ]

spawn ssh root@$IP $COMM

expect {

        "yes/no"

        {send "yes\r";exp_continue}

        "password:"

        {send "$PASS\r"}

        {send "$COMM\r"}

        }

expect eof

[root@web1 mnt]# vim ty17.sh

#!/bin/bash

for NUM in {17..21}

do

ping -c1 -w1 172.25.254.$NUM &> /dev/null && (

        /mnt/ty16.exp 172.25.254.$NUM westos hostname | grep -E "spawn|root|denied" -v )

done

[root@web1 mnt]# sh ty17.sh

foundation20.ilt.example.com

實驗15.

用戶的密碼匹配實驗

[root@web1 mnt]# vim ty20.sh

#!/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 exist !!"

       done

                else

                echo $1 and $2 have different lines

                fi

        elif

        [ ! -e "$1" ]

        then

        echo "ERROR:$1 is not exist"

        else

        echo "ERROR:$2 is not exist"

        fi

else

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

fi

[root@web1 mnt]# sh ty20.sh  userfile passfile

Changing password for user westos.

passwd: all authentication tokens updated successfully.

Changing password for user redhat.

passwd: all authentication tokens updated successfully.

yz exist !!

 

 

##引用##

 

引用有三種類型

1.弱引用

將字符串放置在雙引號中,保留字符串中所有字符的文字值,$、`、\和!字符除外。

echo "can I have a $FRUIT"

echo "the current time is $(date+%)."

2.強引用

將字符串放置在單引號中,保留字符串中所有字符的文字值,同時禁用所有擴展。

echo "make$$$Fast"

rm'untitled folder'

3.轉義

非引用的\是轉義字符。它保留了下一個字符的文字值。如\$PATH是確切的字符串$PATH,而不是PATH變量的內容

echo make\$\$\$Fast\!

ls untitled\folder

##shell變量##

若要定義或 指定值:

FRUIT=apple

若要參考或使用變量:

$FRUIT

${FRUIT}

 

test 和中括號的作用一樣

-z爲空

-n不爲空


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章