Shell脚本常用语句

Read


 read -s                                    ###隐藏输入结果
 read -p                                    ###读到变量显示出的内容
 vim test.sh
 #!/bin/bash
 read -p "Please input username: " NAME
 USER=`getent passwd $NAME`
 [ -z "$USER" ]&&
 {
        useradd $NAME
        echo $PASSWD | passwd --stdin $NAME
 }||{
        exit
 }
 read -p "Please input password: " -s PASSWD
 echo " "

select


select 变量 in 选项(可多个,并且可以按123排序下去的)

 vim test.sh
 #!/bin/bash
 select value in delete create quit
 do
         echo "$value"
 done

这里写图片描述

建立文件和删除文件:

 vim test.sh
 #!/bin/bash
 SCRIPT()
 {
        select ACTION in delete create quit
        do
                [ "$ACTION" = "quit" ]&&{
                exit 1
                }||{
                read -p "please input filename:" FILENAME
                [ "$ACTION" = "delete" ]&&{
                rm -f $FILENAME
                SCRIPT
                [ "$ACTION" = "create" ]&&{
                touch $FILENAME
                SCRIPT
                }
                }

        }
        done
 }
 SCRIPT

while


判断IP能否ping通:

 vim test.sh
 #!/bin/bash
 while true
 do
        read -p "input ip address:" IP
        DREAM=`echo "$IP" |tr "a-z" "A-Z"`
        [ "$DREAM" = "QUIT" ]&&{
                exit 0
        }||{
                ping -w1 -c1 $IP &> /dev/null &&echo "$IP is up"||echo "$IP is down"
        }
 done

if


判断文件类型:
1.

 vim test.sh
 #!/bin/bash
 if [ -z "$1" ];then
 echo "please input file after command!!!"
 else
 {
        if [ -e "$1" ];then
        {
                if [ -L "$1" ];then
                        echo "$1 is a link file"
                elif [ -S "$1" ];then
                        echo "$1 is a socket file"
                elif [ -b "$1" ];then
                        echo "$1 is a block file"
                elif [ -d "$1" ];then
                        echo "$1 is a directory file"
                elif [ -f "$1" ];then
                        echo "$1 is a comman file"
                fi
        }
        else
        echo "$1 is not exist!!!"
        fi
 }
 fi 

2.

 vim test.sh
 #!/bin/bash
 SCRIPT()                                       
 {
        if [ "$2" "$1" ];then
        echo "$1 is a $3 file"
        exit
        fi
 }
 if [ -z "$1" ];then
 echo "please input file after command!!!"
 else
 {
        if [ -e $1 ];then
        {
                SCRIPT $1 -L link
                SCRIPT $1 -S socket
                SCRIPT $1 -b block
                SCRIPT $1 -d directory
                SCRIPT $1 -f comman
        }
        else
        echo "$1 is not exist!!!"
        fi
 }
 fi

case


脚本实现服务的控制

 #!/bin/bash
 case $1 in
        start)
        systemctl $1 $2
        ;;
        status)
        systemctl $1 $2
        ;;
        stop)
        systemctl $1 $2
        ;;
        restart)
        systemctl $1 $2
        ;;
        *)
        echo error
 esac

Expect


自动回答问题:

1.

 vim /mnt/answer.sh
 read -p "please input your name:" NAME
 read -p "please input your age:" AGE
 read -p "please input your class:" CLASS
 echo "$NAME is $AGE at $CLASS"

 vim /mnt/test.sh
 #!/bin/bash
 sh /mnt/answer.sh <<EOF
 dream
 22
 linux
 EOF

测试:

 sh /mnt/test.sh

我们可以发现能够回答我们的问题,但是如果我们把read -p “please input your age:” AGE这条给删除掉,会发现回答的顺序就发现了回答的顺序发生了错乱,因此就出现了expect的方法,它可以匹配关键字回答问题,从而不会发现回答错误。
2.

 chmod +x /mnt/answer.sh
 yum install expect -y 
 vim /mnt/test.exp
 #!/usr/bin/expect
 set NAME [ lindex $argv 0 ]                             ###0脚本后面跟的第一个字符串
 set AGE [ lindex $argv 1 ]
 set CLASS [ lindex $argv 2 ]
 spawn /mnt/answer.sh                                    ###执行的命令
 expect {
        "name" { send "$NAME\r";exp_continue }           ###name为关键字(我们只要找每行不同的字符串即可)
        "age" { send "$AGE\r";exp_continue }             ###exp_continue继续向下匹配
        "class" { send "$CLASS\r" }
 }
 expect eof                                              ###执行完就结束了
ssh连接自动回答:
 vim test.exp
 #!/usr/bin/expect
 set IP  [ lindex $argv 0 ]
 set PASSWD      [ lindex $argv 1 ]
 spawn ssh root@$IP
 expect {
        "yes/no" { send "yes\r";exp_continue }
        "password" { send "$PASSWD\r" }
 }
 interact                                                 ###保持连接不会中断

测试:

 chmod +x test.exp
 ./test.exp IP 密码
统计网段的hostname(假设密码都为dream):
 #!/bin/bash
 SCRIPT()
 {
        /usr/bin/expect <<EOF
        set timeout 10
        spawn ssh root@$1 hostname
        expect {
                "yes/no" { send "yes\r";exp_continue }
                "password" { send "$PASSWD\r" }
        }
        expect eof
 EOF
 }
 for i in `seq 1 254`
 do
 ping -w1 -c1 172.25.254.$i &>/dev/null &&{
        SCRIPT 172.25.254.$i |grep -E  "spawn|password|Permanently|connecting|Permission" -v
 }
 done

测试:

 sh /mnt/test.sh dream
找出文件中IP的hostname:
 vim test.sh
 #!/bin/bash
 SCRIPT()
 {
        /usr/bin/expect <<EOF
        set timeout 10
        spawn ssh root@$1 hostname
        expect {
                "yes/no" { send "yes\r";exp_continue }
                "password" { send "$PASSWD\r" }
        }
        expect eof
 EOF
 }
 num=`cat "$1" |wc -l`
 for i in `seq 1 $num`
 do
        IP=$(awk "NR==$i{print \$1}" $1)
        PASSWD=$(awk "NR==$i{print \$2}" $1)
        ping -w1 -c1 $IP &>/dev/null &&{
        SCRIPT $IP |grep -E "spawn|password|Permanently|connecting|Permission" -v
        }
 done

测试:

 vim dream
 IP 密码
 IP 密码

 sh test.sh dream                                  ###这样我们文件里面的IP的主机名便可以成功获得
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wielun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值