欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux下shell脚本编写

程序员文章站 2022-07-05 20:02:07
...

if then fi 语句
语句的基本结构:

    if
    then
    elif
    then
    ……
    else
    fi

判断文件的类型(if then fi语句)?

    #!/bin/bash
    Check_file(){
            if
            [ "$1" "$2" ]
            then
            echo "$1" is $3
            exit 0
            fi
    }
    if
    [ "$#" -ne "1" ]
    then
            echo "please input a file follow script"
    elif
    [ ! -e "$1" ]
    then
            echo "$1 is not exist !!"
    else
            Check_file -L $1 link
            Check_file -f $1 "common file"
            Check_file -S $1 socket
            Check_file -b $1 block
            Check_file -c $1 character
            Check_file -d $1 directory
    fi

linux下shell脚本编写
文件创建用户?
要求:
1.文件数量不对报错
2.文件不存在
3.文件行数有差异
4.用户存在时显示用户存在,但不改变次用户密码
5.当用户不存在时建立用户并设定相应密码

    #!/bin/bash
    if
            [ "$#" != "2" ]
    then
            echo "please input filename and passwdfile after script!!"
            exit 1
    elif
            [ ! -e "$1" ]
    then
            echo "$1 is not exist"
            exit 1
    elif
            [ ! -e "$2" ]
    then
            echo "$2 is not exist"
            exit 1
    elif
            USER_COUNT=`awk 'BEGIN{n=0}{n++}END{print n}' $1`
            PASS_COUNT=`awk 'BEGIN{n=0}{n++}END{print n}' $2`
    then
            echo "用户和密码不匹配"
            exit 1
    fi

    for LINE_NUM in `seq 1 $USER_COUNT`
    do
            USERNAME=`sed -n "${LINE_NUM}p" $1`
            PASSWD=`sed -n "${LINE_NUM}p" $2`
            useradd $USERNAME && {
                    echo $PASSWD | passwd --stdin $USERNAME &>/dev/null && echo $USERNAME is created !!
            }
    done

linux下shell脚本编写
输入cat输出dog,输入dog输出cat,其他都输出error?

    if
            [ "$1" = "cat" ]
    then
            echo dog
    elif
            [ "$1" = "dog" ]
    then
            echo cat
    else
            echo error
    fi

linux下shell脚本编写
case语句

自动应答工具expect
ssh自动应答连接?

    #!/usr/bin/expect
    set timeout 5
    set IP   [ lindex $argv 0 ]
    set PASS [ lindex $argv 1 ]
    spawn ssh aaa@qq.com$IP
    expect {
            "yes/no" { send "yes\r";exp_continue }
            "password" { send "$PASS\r" }
    }
    interact

expect命令调用去执行 .exp文件
linux下shell脚本编写
ctrl+v ctrl+M 替换文件中的^M=回车键\r
不同作系统命令之间的差异
linux ssdewdw\n
windows safewdw\n\r
unix wqdewd\r

循环中不输出4?

for NUM in {1..10}
do
while [ "$NUM" = 4 ]
do
        continue 4
done
        echo $NUM
done

linux下shell脚本编写
10秒倒计时?

#!/bin/bash
for (( i=10;i>0;i-- ))
do
        echo -n  "$i's "
        echo -ne "\r"
        sleep 1
done

在执行之后脚本从10s开始倒计时,等倒计时结束之后,脚本执行自动结束
linux下shell脚本编写
1分10秒倒计时?
方法一:

#!/bin/bash
read -p "please input minute:" m
read -p "please input second:" s
i=$[ m*60+s ]
for (( t=$i;t>0;t-- ))
do
        A=$[ $t/60 ]
        B=$[ $t%60 ]
        echo -ne "\r $A:$B  \r"
        sleep 1
done

方法二:

#!/bin/bash
PRINT_MESSAGE()
{
        echo -n "$MIN:$SEC "
        sleep 1
        echo -ne "\r    \r"
}
MIN=1
SEC=10
for (( ;SEC>=0;SEC-- ))
do
        [ "$SEC" = "0" -a "$MIN" = "0" ]&& exit 0
        [ "$SEC" = "0" -a "$MIN" -gt "0" ]&&{
        PRINT_MESSAGE
        (( MIN-- ))
        SEC=59
        }
        PRINT_MESSAGE
done

linux下shell脚本编写
脚本实现计算器?
将脚本中的数据导入bc中进行计算,将计算结果输出,数字的运算在bc中运算。

#!/bin/bash
read -p "请输入您要操作的数字:" NUM1
read -p "请输入您要操作的运算:" OP
read -p "请输入要操作的第二个数字:" NUM2
bc <<EOF
$NUM1$OP$NUM2
EOF

linux下shell脚本编写

相关标签: shell 脚本