zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    Bluetooth篇 开发实例之八 匹配
    Bluetooth篇 开发实例之七 匹配&UUID
    Bluetooth篇 开发实例之六 蓝牙RSSI计算距离
    Bluetooth篇 开发实例之五 为什么无线信号(RSSI)是负值?
    Android Developer -- Bluetooth篇 开发实例之四 API详解
    Android Developer -- Bluetooth篇 开发实例之三 管理连接
    LoadRunner监控Window/Unix系统资源的配置
    性能测试方法对比
    HTTP状态代码含义
    百度输入法
  • 原文地址:https://www.cnblogs.com/wanyp/p/7526721.html
Copyright © 2011-2022 走看看