Shell函数的简单应用

                      Shell函数的简单应用                    

在脚本内给函数传参:

#!/bin/bash . /etc/init.d/functions CheckUrl (){ curl -I -s $1 | head -1 } CheckUrl joy4you.com CheckUrl www.baidu.com

 

练习,取出数组中的三个元素:
方法一:
#!/bin/bash
n=0
array=(
freddy1
freddy2
freddy3
)
#for ((i=0;i<${#array[*]};i++));do
for i in ${array[*]};do
        if [ $n -lt 3 ];then
        echo "This is num $n,then content is $i"
        let n+=1;
        fi
done
echo "------------------"
echo "array len:${#array[*]}"

方法二:
#!/bin/bash
array=(
freddy1
freddy2
freddy3
)
for ((i=0;i<${#array[*]};i++));do
        echo "This is num $i,then content is ${array[$i]}"
done
echo "------------------"
echo "array len:${#array[*]}"
使用数组,打印出当前目录下的所有文件:
方法一:
#!/bin/bash
DIR=($(ls))
for ((i=0;i<`echo ${#DIR[*]}`;i++));do
        echo ${DIR[$i]}
done
方法二: #
!/bin/bash DIR=($(ls)) for ((i=0;i<${#DIR[*]};i++));do echo ${DIR[$i]} done

 

posted @ 2016-04-25 15:58  唐胜伟  阅读(242)  评论(0编辑  收藏  举报