2.12. 2017-12-26-练习-脚本进阶3

2.12.1. 练习1

1、利用expect 实现FTP自动上传文件

#!/bin/bash
#================================================
#FileName   :test_expect_46_zhaojiedi.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-26 14:47:37
#Version    :V1.0
#Other      :
#================================================

# usage ./test_expect_46_zhaojiedi.sh 172.18.101.69 test test /etc/issue /ftp/issue
host=$1
username=$2
password=$3
file=$4
upload_dir=$5

expect <<EOF
set timeout 10
spawn ftp $host
expect {
        "Name" { send "$username\n"; exp_continue }
        "Password:" { send "$password\n";exp_continue }
        "ftp>" { send "put $file $upload_dir\n"; }
}
expect "ftp>" { send "bye\n" }
EOF

2、数组:实现杨辉三角(选做)

#!/bin/bash
#================================================
#FileName   :test_yanghuisanjiao_46_zhaojiedi.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-26 18:03:09
#Version    :V1.0
#Other      :
#================================================
line=7
count=$[line**2-1]
declare -a arr
declare -a arr_last

print_arr_last(){
        for i in `seq 0 $[line-1] ` ; do
                echo ${arr_last[$i]}
        done
}

# init arr_last
for i in `seq 0 $[line-1]`; do
        ii=$[i+1]
        arr_last[$i]=$[$ii ** 2 -1 ]
done

print_arr_last

get_location_1(){
        local loc=$1
        local row=0
        local col=0
        for i in `seq  $[line-1] -1 0` ; do
                #echo "${i}==>${arr_last[$i]}==>"
                if [ "$loc" -gt  ${arr_last[$i]} ] ; then
                        row=$[i+1]
                        col=$[ loc-row**2]
                        echo "$row,$col"
                        return 0
                fi
        done
        echo  "0,0"
}
# get_location_1 8
get_location_2(){
        local row=$1
        local col=$2
        echo "$[row ** 2 + col]"
}
#get_location_2 2 3
is_last_col(){
        local row=$1
        local col=$2
        if [ "$col" -eq "$[row * 2]" ] ;then
                return 0
        fi
        return 1
}
echo "count=$count"


for i in `seq 0 $count` ; do
        info=`get_location_1 $i`
        echo "$info ......"
        row=`echo $info |cut -d "," -f1`
        col=`echo $info |cut -d "," -f2`
        echo "$row ... $col "

        if [ "$col" -eq 0 ] ;then
                arr[$i]=1
                echo "set $i 1"
                continue
        fi
        if [ "$col" -eq "$[row *2]" ] ; then

                arr[$i]=1
                echo "set $i 1"
                continue
        fi
        if [ "$[col%2]" -eq 1 ] ; then
                continue
        fi
        loc_left=`get_location_2 $[row-1] $[col-2]`
        loc_right=`get_location_2 $[row-1] $col`
        echo "i,left,right==>$i ,$loc_left,$loc_right"
        v1=${arr[$loc_left]}
        v2=${arr[$loc_right]}
        echo "v1,v2=>$v1,$v2"
        arr[$i]=$[ v1 + v2 ]
done
print_space(){
        for i in `seq 1 $1`; do
                echo -n " "
        done
}

for i in `seq 0 $count` ; do
        info=`get_location_1 $i`
        #echo "$info ......"
        row=`echo $info |cut -d "," -f1`
        col=`echo $info |cut -d "," -f2`
        if [ "$col" -eq 0 ] ; then
                for j in `seq 1 $[2*line-2*row-2]` ; do
                        echo -n " "
                done
        fi
        if [ "$col" -ne  "$[row *2]" ] ; then
                echo -ne "${arr[$i]}  "
        else
                echo -e "${arr[$i]}  "
        fi
done

3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#!/bin/bash
#================================================
#FileName   :test_bubble_sort_46_zhaojiedi.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-26 17:49:51
#Version    :V1.0
#Other      :
#================================================

read  -p "enter some value "  -a arr

cnt=${#arr[*]}
echo "count="$cnt
my_print (){
        for i in `seq 0 $[cnt-1]`; do
                echo -ne "${arr[$i]}\t"
        done
        echo
}
echo "befor bubble sort"
my_print

for i in `seq 0 $[cnt-1]`; do
        for j in `seq 0 $[i-1]`;do
                if [ "${arr[$i]}" -lt "${arr[$j]}" ] ; then
                        tmp=${arr[$i]}
                        arr[$i]=${arr[$j]}
                        arr[$j]=$tmp
                fi
        done
done

echo "after bubble sort"
my_print