Shell函数的简单应用
在脚本内给函数传参:
#!/bin/bash
. /etc/init.d/functions
CheckUrl (){
curl -I -s $ | head -
}
CheckUrl joy4you.com
CheckUrl www.baidu.com
练习,取出数组中的三个元素:
方法一:
#!/bin/bash
n=
array=(
freddy1
freddy2
freddy3
)
#for ((i=;i<${#array[*]};i++));do
for i in ${array[*]};do
if [ $n -lt ];then
echo "This is num $n,then content is $i"
let n+=;
fi
done
echo "------------------"
echo "array len:${#array[*]}"
方法二:
#!/bin/bash
array=(
freddy1
freddy2
freddy3
)
for ((i=;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=;i<`echo ${#DIR[*]}`;i++));do
echo ${DIR[$i]}
done
方法二:
#!/bin/bash
DIR=($(ls))
for ((i=;i<${#DIR[*]};i++));do
echo ${DIR[$i]}
done