我是靠谱客的博主 大方宝马,这篇文章主要介绍Shell使用C语言风格的for循环编写,现在分享给大家,希望可以做个参考。

1、C语言中的for循环:

#include <stdio.h>

int main()
{
   int i=0;
   for(i = 0; i < 5; i++)
   {
	printf("The next number id %dn", i);
	}
}

# 运行结果:
The next number id 0
The next number id 1
The next number id 2
The next number id 3
The next number id 4
The next number id 5

2、 Shell中C语言风格的for循环语法:

      for((a = 1; a <10; a++))

      do

          commands

      done

#!/bin/bash

for ((i=5; i<10; i++))
do
	echo "Next number is: $i"
done

# 计算1到100和
for ((i=1; i<=100; i++))
do
	(( sum+=$i ))
done
echo "1+2+...+100=$sum"

# 运行结果
% sh 15.for_each4.sh
Next number is: 5
Next number is: 6
Next number is: 7
Next number is: 8
Next number is: 9
1+2+...+100=5050

  说明:使用双括号把C语言风格代码包括起来就可以让shell识别。

 

最后

以上就是大方宝马最近收集整理的关于Shell使用C语言风格的for循环编写的全部内容,更多相关Shell使用C语言风格内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部