微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell for 学习

#!/bin/bash 
for_1 () {
    echo "for 1 show item"
    for var in item1 item2 ... itemN
    do
        echo ${var};
    done
}

for_2 () {
    echo "for 2 show 1~5"
    for loop in 1 2 3 4 5
    do
        echo "The value is : ${loop}"
    done
}

for_3 () {
    echo "for 3 show string array"
    for str in 'This is a string' 'This is a number'
    do
        echo ${str}   
    done
}

for_4 () {
    echo "for 4 show string"
    for str in "'This is a string' 'This is a number'"
    do
        echo ${str}   
    done
}

for_5 () {
    echo "for 5 show ls "
    for file in `ls ./`
    do
        echo ${file}
    done
}

for_6 () {
    echo "for 6 show ()"
    for ((i=1;i<=5;i++))
    do
        echo "这是第 $i调用";
    done

}

for_1
for_2
for_3
for_4
for_5
for_6

# for 使用命令的两种写法
for file in $(ls); do echo ${file}; done
for file in `ls`; do echo ${file};done

结果:

for 1 show item
item1
item2
...
itemN
for 2 show 1~5
The value is : 1
The value is : 2
The value is : 3
The value is : 4
The value is : 5
for 3 show string array
This is a string
This is a number
for 4 show string
'This is a string' 'This is a number'
for 5 show ls 
array.sh
for.sh
function.sh
if.sh
op.sh
params.sh
read.sh
string.sh
trap.sh
varaiable_assignment.sh
while.sh
xaa
for 6 show ()
这是第 1调用
这是第 2调用
这是第 3调用
这是第 4调用
这是第 5调用
array.sh
for.sh
function.sh
if.sh
op.sh
params.sh
read.sh
string.sh
trap.sh
varaiable_assignment.sh
while.sh
xaa
array.sh
for.sh
function.sh
if.sh
op.sh
params.sh
read.sh
string.sh
trap.sh
varaiable_assignment.sh
while.sh
xaa

原文地址:https://www.jb51.cc/bash/388303.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐