6.linux基础-shell编程
1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
使用expect方式
[root@mdw shell]# cat 1.sh #!/usr/bin/expect set ip 192.168.188.134 set user root set password rootroot set timeout 10 spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } interact [root@mdw shell]# ./1.sh spawn ssh root@192.168.188.134 Last login: Wed May 4 21:19:15 2022 from mdw
shell方式如下:
[root@mdw shell]# cat 2.sh #!/bin/bash ip=192.168.188.134 user=root password=rootroot expect <<EOF set timeout 10 spawn ssh $user@$ip expect { "yes/no"{ send "yes\n";exp_continue } "password" { send "$password\n" } } expect eof EOF [root@mdw shell]# sh 2.sh spawn ssh root@192.168.188.134 Last login: Wed May 4 21:19:37 2022 from mdw [root@smdw ~]#
2、生成10个随机数保存于数组中,并找出其最大值和最小值
[root@mdw shell]# cat max_min.sh #!/bin/bash declare -i min max declare -a nums for ((i=0;i<10;i++));do nums[$i]=$RANDOM [ $i -eq 0 ] && min=${nums[$i]} && max=${nums[$i]}&& continue [ ${nums[$i]} -gt $max ] && max=${nums[$i]} [ ${nums[$i]} -lt $min ] && min=${nums[$i]} done echo “All numbers are ${nums[*]}” echo Max is $max echo Min is $min [root@mdw shell]# bash max_min.sh “All numbers are 24431 6476 21685 25722 13396 12428 30321 9792 28554 698” Max is 30321 Min is 698 [root@mdw shell]# bash max_min.sh “All numbers are 27200 19779 27567 14160 9155 27076 23898 31766 18176 27427” Max is 31766 Min is 9155 [root@mdw shell]#
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@mdw shell]# cat array.sh #!/bin/bash read -p "请输入数值个数:" COUNT declare -a nums for ((i=0;i<$COUNT;i++));do num[$i]=$RANDOM done echo "The initial array:" echo ${num[@]} declare -i n=$COUNT for (( i=0; i<n-1; i++ ));do for (( j=0; j<n-1-i; j++ ));do let x=$j+1 if (( ${num[$j]} < ${num[$x]} ));then #从大到小排列 tmp=${num[$x]} num[$x]=${num[$j]} num[$j]=$tmp fi done done echo "After sort:" echo ${num[*]} echo "the max integer is $num,the min integer is ${num[$((n-1))]}" [root@mdw shell]# sh array.sh 请输入数值个数:4 The initial array: 25939 13602 25446 31200 After sort: 31200 25939 25446 13602 the max integer is 31200,the min integer is 13602
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
[root@mdw shell]# top
top - 21:27:15 up 18 min, 2 users, load average: 0.02, 0.11, 0.20
Tasks: 202 total, 1 running, 201 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.2 us, 2.8 sy, 0.0 ni, 90.3 id, 3.7 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 7994320 total, 6695316 free, 439684 used, 859320 buff/cache KiB Swap: 8388604 total, 8388604 free, 0 used. 7259508 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3997 root 20 0 157716 2228 1508 R 6.7 0.0 0:00.01 top 1 root 20 0 190808 3812 2404 S 0.0 0.0 0:07.21 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.09 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:01.13 kworker/0:0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root rt 0 0 0 0 S 0.0 0.0 0:00.09 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 S 0.0 0.0 0:01.84 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 top命令栏信息简介 us:用户空间 sy:内核空间 ni:调整nice时间 id:空闲 wa:等待IO时间 hi:硬中断 si:软中断 st:虚拟机偷走的时间
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
[root@mdw shell]# cat for.sh #!/bin/bash net=192.168.1. for i in {1..254};do { if ping -c2 -w2 ${net}${i} &>/dev/null;then echo $net$i is success else echo $net$i is fail fi }& wait done [root@mdw shell]# sh for.sh 192.168.1.1 is success 192.168.1.2 is success 192.168.1.3 is success [root@mdw shell]# ping 192.168.1.2 PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. 64 bytes from 192.168.1.2: icmp_seq=1 ttl=128 time=4.88 ms 64 bytes from 192.168.1.2: icmp_seq=2 ttl=128 time=3.14 ms 64 bytes from 192.168.1.2: icmp_seq=3 ttl=128 time=4.15 ms
[root@mdw shell]# vim while.sh [root@mdw shell]# [root@mdw shell]# cat while.sh #!/bin/bash net=192.168.1. declare -i address=1 while [ $address -le 254 ];do { ping -c2 -w2 ${net}${address} &>/dev/null if [ $? -eq 0 ];then echo $net$address is success else echo $net$address is fail fi }& let address++ done wait [root@mdw shell]# sh while.sh 192.168.1.1 is success 192.168.1.2 is success 192.168.1.4 is success 192.168.1.3 is success 192.168.1.5 is fail 192.168.1.12 is fail 192.168.1.14 is fail 192.168.1.8 is fail 192.168.1.10 is fail 192.168.1.16 is fail
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@mdw shell]# crontab -l 30 1 * * 1-5 cp -a /etc /backup/etcbak-`date -d -1day +%F-%H`
posted on 2022-05-04 21:34 N64_849411472 阅读(24) 评论(0) 编辑 收藏 举报