• shell数组


    chapter 13.shell数组的应用实践
    
    1.介绍
    shell数组是元素的集合,数据的一种格式;
    
    2.定义
    array=(value1 value2 value3...)
    	对于元素较长的:竖着写 静态的!
    array=($(命令)) array(`命令`)
    	动态的
    
    3.打印
    打印数组个数:echo ${#array[*]} echo ${#array[@]}
    打印数组元素:所有元素:echo ${array[*]} echo ${array[@]} 单个元素: echo ${array[0]}
    
    4.删除
    unset array ##删除整个数组
    unset array[0] ##删除第一个元素
    
    5.示例
    array=(1 2 3 array123 bistu)
    
    ##普通for循环
    for i in  ${array[*]}; do
    	#statements
    	echo $i
    done
    
    ##C语言的for循环
    for (( i = 0; i < ${#array[*]}; i++ )); do
    	#statements
    	echo ${array[i]}
    done
    
    ##while循环
    
    
    6.面试题实战及案例
    6.1打印下面这句话中字母数不大于6的单词
    I am oldboy teacher welcome to oldboy training class
    
    C语言的for循环打印
    方法1:
    string=(I am oldboy teacher welcome to oldboy training class)
    for (( i = 0; i < ${#string[*]}; i++ ))
    do
    	if [ ${#string[i]} -gt 6 ]; then
    		#statements
    		sleep 1
    	else
    		echo "${string[i]}"
    	fi
    done
    
    方法2:
    string=(I am oldboy teacher welcome to oldboy training class)
    for (( i = 0; i < ${#string[*]}; i++ ))
    do
    	if [ `expr length ${string[i]}` -gt 6 ]; then
    		#statements
    		sleep 1
    	else
    		echo "${string[i]}"
    	fi
    done
    
    方法3:
    string=(I am oldboy teacher welcome to oldboy training class)
    for (( i = 0; i < ${#string[*]}; i++ ))
    do
    	if [ `echo ${string[i]} | wc -L` -gt 6 ]; then
    		#statements
    		echo ""
    	else
    		echo "${string[i]}"
    	fi
    done
    
    方法4:
    string=(I am oldboy teacher welcome to oldboy training class)
    for (( i = 0; i < ${#string[*]}; i++ ))
    do
    	if [ `echo ${string[i]} | awk '{print length()}'` -gt 6 ]; then
    		#statements
    		echo ""
    	else
    		echo "${string[i]}"
    	fi
    done
    
    使用for循环列举值列表法
    for word in I am oldboy teacher welcome to oldboy training class
    do
    	sth //wc -L awk的length()函数 expr的length函数  字符串本身的${#word} 均可进行判断
    done
    
    
    awk循环
    [root@bogon ~]# echo $char | awk '{for(i=1;i<=NF;i++) if(length($i)<=6) print $i}'
    
    
    6.2批量检查多个网站地址是否正常
    url=(
    	http://blog.oldboy.com
    	http://www.baidu.com
    	http://192.168.142.180
    	http://127.0.0.1
    )
    方法1:
    urls=(
            http://blog.oldboy.com
            http://www.baidu.com
            http://192.168.142.180
            http://127.0.0.1
    )
    function check_url(){
            for url in ${urls[*]}; do
                    #statements
                    #echo $url
                    curl -s -o /dev/null $url
                    RETRVAL=$?
                    if [[ $RETRVAL -eq 0  ]]; then
                            #statements
                            echo "$url is ok"
                    else
                            echo "$url is not ok"
                    fi
            done
    }
    function main(){
            while true; do
                    #statements
                    check_url
                    sleep 10
            done
    
    }
    main
    
    
    如何调试脚本:
    1)echo
    2)sh -x ***.sh
    

      

  • 相关阅读:
    Unit Vector Compression
    PT, BPT, VCM
    Major Performance Impacts

    SAH Benchmarks Of Natural History Museum Scene
    图标变换图片---轮播切换
    弹出层--弹框
    Git for windows 中文乱码解决方案
    在CentOS上安装Git
    Git 的基本配置
  • 原文地址:https://www.cnblogs.com/wanyp/p/7526721.html
Copyright © 2020-2023  润新知