一、定義關聯數組 磁盤分區(鍵) 利用率(值);鍵值對先存放的後取出數組
file=/app/disk if [ ! -e $file ];then file=`touch /app/disk` fi echo $file df | grep "/dev/sd" >> $file while read line ;do index=`echo "$line" | cut -d " " -f1` value=`echo "$line" | sed -r 's/.* ([0-9]+)%.*/\1/'` declare -A dUse dUse["$index"]="$value" done < ${file} echo ${dUse[*]}
二、將下圖所示,實現轉置矩陣matrix.sh
1 2 3 1 4 7
4 5 6 ===> 2 5 8
7 8 9 3 6 9app
juzheng[01]=2 juzheng[02]=3 juzheng[10]=4 juzheng[11]=5 juzheng[12]=6 juzheng[20]=7 juzheng[21]=8 juzheng[22]=9 display() { for((i=0;i<size;i++));do for((j=0;j<size;j++));do echo -n "${juzheng[${i}${j}]} " done echo done } #display before matrix echo "display before matrix" display #matrix for((i=0;i<size;i++));do for((j=i+1;j<size;j++));do temp=${juzheng[$i$j]} juzheng[$i$j]=${juzheng[$j$i]} juzheng[$j$i]=$temp done done #after matrix echo "after matrix" display
三、使用expect 工具讓客戶端自動登陸ssh服務的遠程主機並建立haha用戶和重置密碼ssh
ip=$1 user=$2 passwd=$3 echo "-----" echo $passwd expect <<EOF set timeout 10 #auto login spawn ssh $user@$ip expect { "yes/no"{ send "yes\n";exp_continue } "password" { send "${passwd}\n" } } #create user and change password after login expect "]#" { send "useradd haha\n" } expect "]#" { send "echo yangguang | passwd --stdin haha\n" } #logout expect "]#" { send "exit\n" } #quit expect expect eof EOF