<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>回数 &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/%E5%9B%9E%E6%95%B0/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Mon, 03 Jun 2024 13:50:50 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>回数 &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【シェルスクリプト】for文で回数の指定方法！エラー対処法あり</title>
		<link>https://sugublog.org/shellscript-for-number-of-times/</link>
					<comments>https://sugublog.org/shellscript-for-number-of-times/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Mon, 20 Nov 2023 12:40:59 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[回数]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5924</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/html-007-5-1024x513.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトの繰り返しでforを使用する時に、ループする回数の指定方法をまとめます。 そういった疑問に答えます。 シェルスクリプトのfor文で回数の指定方法 シェルスクリプトのfor文の基本構文と使い方 シェルスクリ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/html-007-5-1024x513.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong>シェルスクリプト</strong>の繰り返しで<strong>for</strong>を使用する時に、<strong><span class="swl-inline-color has-swl-deep-01-color">ループする回数の指定方法</span></strong>をまとめます。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="blue"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">シェルスクリプトのforで繰り返し回数の指定方法を知りたい犬</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>シェルスクリプトでfor文を使いたいけど、回数の指定方法がわかりません。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p>そういった疑問に答えます。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">シェルスクリプトのfor文で回数の指定方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="676" src="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1024x676.jpg" alt="回数指定方法" class="wp-image-4185" srcset="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1024x676.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-300x198.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-768x507.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトのfor文の基本構文と使い方</h3>



<p><strong>シェルスクリプト</strong>の<strong>for</strong>文を使った<strong>繰り返し</strong>の基礎は以下になります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in 変数に入れるリストなど
 do
  繰り返す処理
 done</code></pre></div>



<h3 class="wp-block-heading">for文を使ったカウントアップの方法</h3>



<p>for文の<strong><span class="swl-inline-color has-swl-deep-01-color">回数指定</span></strong>の基本は以下です</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in {1..9}
 do
  繰り返す処理
 done</code></pre></div>



<p>｛1..9｝は1から9まで順番に変数に入れていきます。この場合の回数は9になります。</p>



<h3 class="wp-block-heading">forの回数指定の基本実践</h3>



<p>以下のfor文のシェルスクリプトを記述してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 echo 回数指定で$i回繰り返しています
done</code></pre></div>



<p>＄iで変数iに入った値を展開しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-3-300x232.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-dollar-mean/">【シェルスクリプト】「$」の意味を詳しく解説！実例あり</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで「$」はよく出てきますが、どういう意味なのかわからないと言った事はないでしょうか？ シェルスクリプトで「$」はよく出てきますが、どういう意味な&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>echoで文字列を表示します。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-3-300x188.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-echo/">【シェルスクリプト基礎】echoコマンドの使い方！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのechoコマンドの使い方で疑問に思っている事などないでしょうか？ 思い通りに文字列を表示できなかったりしないでしょうか？ シェルスクリプトでecho&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>変数や文字列などを結合する場合は、特に何もせず文字などをつなげれば問題ありません。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-4-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-screipt-base-combine-strings/">【シェルスクリプト基礎】文字列を結合するには？改行、スペースなども結合</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで文字列を結合したい場合などのやり方をまとめます。 変数と文字列の結合や改行、スペース、空白などを含めた結合、また引数と文字列なども結合してい&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">forの回数指定の基本実践結果</h3>



<p>ではこのシェルスクリプトを実行してみます。今回は「. シェルスクリプト名」で実行します。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-execution/">【シェルスクリプト基礎】コマンドを実行する様々な方法をご紹介！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトを実行する時に困った事はありませんか？ シェルスクリプトのコマンド実行での失敗メッセージ一覧 「実行権限がありません」 「コマンドが見つかりませ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>結果は9回実行されています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="599" height="165" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9.png" alt="shellscript-forの実行結果" class="wp-image-5928" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9.png 599w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-300x83.png 300w" sizes="(max-width: 599px) 100vw, 599px" /><figcaption class="wp-element-caption">shellscript-for-1-9の実行結果</figcaption></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトで日本語が文字化けする場合</h3>



<p class="is-style-big_icon_caution">シェルスクリプトを実行した結果、文字化けする場合があります。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="580" height="363" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け.png" alt="shellscriptで日本語文字化けした結果" class="wp-image-5936" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け.png 580w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け-300x188.png 300w" sizes="(max-width: 580px) 100vw, 580px" /><figcaption class="wp-element-caption">shellscriptで日本語文字化けした結果</figcaption></figure>
</div>


<p>この場合は文字コードが<span class="swl-bg-color has-swl-deep-01-background-color">「SJIS」</span>などになっています。<span class="swl-bg-color has-swl-deep-03-background-color">「UTF-8」</span>に変更しましょう。</p>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法</h3>



<p><strong>「seq」</strong>でも<strong>回数指定可能</strong>です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `seq 1 9`
do
 echo seqで$i回繰り返しています
done</code></pre></div>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法実践</h3>



<p>seqを使用して実行してみます。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="481" height="162" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1.png" alt="shellscript-for-seqの実行結果" class="wp-image-5933" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1.png 481w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1-300x101.png 300w" sizes="(max-width: 481px) 100vw, 481px" /><figcaption class="wp-element-caption">shellscript-for-seqの実行結果</figcaption></figure>
</div>


<h3 class="wp-block-heading">seqコマンドを使った時のエラー結果</h3>



<p>seqを使用する時のよくある<span class="swl-bg-color has-swl-deep-01-background-color">エラー</span>を紹介しておきます。</p>



<p>以下のシェルスクリプトを実行して見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in seq 1 9
do
 echo seqで$i回繰り返しています
done</code></pre></div>



<p>「`」が抜けていて「seq」「1」「9」と3つの文字を順番にいれています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="399" height="58" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー.png" alt="" class="wp-image-5934" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー.png 399w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー-300x44.png 300w" sizes="(max-width: 399px) 100vw, 399px" /><figcaption class="wp-element-caption">shellscript-for-seq-1-9-エラーの実行結果</figcaption></figure>
</div>


<p>意図しない結果です。<span class="swl-marker mark_yellow">「`」をつけ忘れないようにしましょう！</span></p>



<p class="is-style-icon_pen">「`」はキーボードのShift＋＠で入力できます。</p>



<h3 class="wp-block-heading">回数指定を(())で行う方法</h3>



<p>最後は(())を使用して<strong>回数</strong>を指定します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for ((i=1; i&lt;9; i++))
do
 echo &quot;(())&quot;で$i回繰り返しています
done</code></pre></div>



<p>このシェルスクリプトは((初期値; 条件; 次の値))で指定しており、初期値が1で「i++」がiに1を加算していき、iが9になれば終了となります。</p>



<p>これで回数を8回にすることができます。ではシェルスクリプトを実行します。</p>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="450" height="146" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1.png" alt="shellscript-for" class="wp-image-5946" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1.png 450w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1-300x97.png 300w" sizes="(max-width: 450px) 100vw, 450px" /></figure>
</div>


<p>結果回数は想定通り８回となっています。</p>



<h2 class="wp-block-heading">回数指定を配列で行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="576" src="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-1024x576.jpg" alt="配列" class="wp-image-3959" srcset="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">配列の使い方と書き方</h3>



<p>配列の基本的な書き方は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(1 2 3 4 5 6 7 8 9)</code></pre></div>



<p>配列名に「=()」をつけて、その中に配列に入れたい文字などを順に入れていきます。</p>



<h3 class="wp-block-heading">回数指定を配列で行う時の構文</h3>



<p><strong>for</strong>で<strong>回数を指定</strong>するのに<strong><span class="swl-inline-color has-swl-deep-01-color">配列</span></strong>を使用する時の構文</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in ${array[@]}</code></pre></div>



<p class="is-style-icon_book">シェルスクリプトのfor文で配列を使用する方法は、下の記事で詳しく説明しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/10/table_20211010-1-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shellscript-for-array/">【シェルスクリプト】for文のループ処理を配列で行う方法</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのループでfor文を使用しますが、順に入れる文字列が複雑なので配列で指定したい事はないでしょうか？ そういった場合の処理のやり方を、実践していき&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">回数指定を配列で行う実践</h3>



<p>では<strong>回数指定</strong>を<strong><span class="swl-inline-color has-swl-deep-01-color">配列</span></strong>で行う以下のシェルスクリプトを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(1 2 3 4 5 6 7 8 9)
for i in ${array[@]}
do
 echo 配列で$i回繰り返しています
done</code></pre></div>



<h3 class="wp-block-heading">回数指定を配列で行った結果</h3>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="582" height="168" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9.png" alt="shellscript-for-配列-1-9の実行結果" class="wp-image-5930" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9.png 582w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9-300x87.png 300w" sizes="(max-width: 582px) 100vw, 582px" /></figure>
</div>


<p>回数指定を配列で行い成功しています。</p>



<h2 class="wp-block-heading">回数指定をファイルを使用して行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="768" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg" alt="ファイル" class="wp-image-3840" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-300x225.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-768x576.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong>ファイルの中身</strong>を取得して<strong>回数指定</strong>してみます。以下のシェルスクリプトを記述します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `cat shellscript-for-count.txt`
do
 echo ファイルから取得$i回目
done</code></pre></div>



<p>同階層に「shellscript-for-count.txt」を作成して中身を以下のように記述します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>1
2
3
4
5
6
7
8
9</code></pre></div>



<p>これで先程のシェルスクリプトを実行して見ます。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="415" height="164" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file.png" alt="シェルスクリプトでforの回数指定をファイルに記述した結果" class="wp-image-5939" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file.png 415w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file-300x119.png 300w" sizes="(max-width: 415px) 100vw, 415px" /><figcaption class="wp-element-caption">シェルスクリプトでforの回数指定をファイルに記述した結果</figcaption></figure>
</div>


<p>きちんと回数指定をファイルから取得しています。</p>



<h2 class="wp-block-heading">応用編：回数をループの制御で行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/pixta_77773496_M-1024x791.jpg" alt="選択する" class="wp-image-3635"/><figcaption class="wp-element-caption">Business person stand on road fork with hard and easy directions. Choice way isometric</figcaption></figure>
</div>


<p>では回数を指定するが途中で制御を行ってみます。</p>



<h3 class="wp-block-heading">continueやbreakコマンドでループをスキップしたり終了する方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 if [[ $i -eq 5 ]]; then
  continue
 elif [[ $i -eq 8 ]]; then
  break
 fi
 echo $i回目：5は飛ばす、8なら終了
done</code></pre></div>



<p>forの回数指定で9回繰り返します。その中でif文により場合分けしています。</p>



<p>5回目の時は<span class="swl-inline-color has-swl-deep-02-color">「continue」</span>で<span class="swl-bg-color has-swl-deep-02-background-color">スキップ</span>し、8回目の時は<span class="swl-inline-color has-swl-deep-03-color">「break」</span>で<span class="swl-bg-color has-swl-deep-03-background-color">処理を抜け</span>ています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="476" height="110" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue.png" alt="shellscript-for-break-continueの実行結果" class="wp-image-5929" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue.png 476w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue-300x69.png 300w" sizes="(max-width: 476px) 100vw, 476px" /><figcaption class="wp-element-caption">shellscript-for-break-continueの実行結果</figcaption></figure>
</div>


<p>実行した結果、5番目がスキップされ、7番目までで終了しています。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">まとめ：シェルスクリプトのfor文で回数の指定方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="522" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg" alt="繰り返し回数を指定する" class="wp-image-4417" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-300x153.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-768x392.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトのfor文の基本構文と使い方</h3>



<p>シェルスクリプトの<strong><span class="swl-inline-color has-swl-deep-01-color">for文を使った繰り返しの基礎</span></strong>は以下になります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in 変数に入れるリストなど
 do
  繰り返す処理
 done</code></pre></div>



<h3 class="wp-block-heading">for文を使ったカウントアップの方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in {1..9}</code></pre></div>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `seq 1 9`</code></pre></div>



<p class="is-style-icon_pen">「`」をつけ忘れないよう気を付ける。</p>



<h3 class="wp-block-heading">回数指定を(())で行う方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for ((i=1; i&lt;9; i++))</code></pre></div>



<h3 class="wp-block-heading">回数指定を配列で行う時の構文</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in ${array[@]}</code></pre></div>



<h3 class="wp-block-heading">回数指定をファイルを使用して行う方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `cat shellscript-for-count.txt`</code></pre></div>



<h3 class="wp-block-heading">continueやbreakコマンドでループをスキップしたり終了する方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 if [[ $i -eq 5 ]]; then
  continue
 elif [[ $i -eq 8 ]]; then
  break
 fi
 echo $i回目：5は飛ばす、8なら終了
done</code></pre></div>



<p>シェルスクリプトのforで繰り返し回数を指定する方法を、実践し画像を使用してわかりやすく説明しました！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shellscript-for-number-of-times/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
