<?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/%e3%82%b7%e3%82%a7%e3%83%ab%e3%82%b9%e3%82%af%e3%83%aa%e3%83%97%e3%83%88-%e8%b6%b3%e3%81%97%e7%ae%97/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Fri, 10 Nov 2023 21:12:19 +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>【シェルスクリプト基礎】足し算をする方法！エラー対処法あり。</title>
		<link>https://sugublog.org/shell-script-addition/</link>
					<comments>https://sugublog.org/shell-script-addition/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Mon, 10 Apr 2023 14:18:13 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[シェルスクリプト 足し算]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5479</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-2-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトで足し算したいけど、上手くできなかったという方はいないでしょうか？ 簡単に書けると思っていたコードでも、初心者では間違ってしまいます。 なので今回はシェルスクリプトで足し算をするコードを、実践を踏まえてご [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-2-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトで足し算</span></strong>したいけど、上手くできなかったという方はいないでしょうか？</p>



<p>簡単に書けると思っていたコードでも、初心者では間違ってしまいます。</p>



<p>なので今回は<strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-marker mark_orange">シェルスクリプトで足し算をするコード</span></span></strong>を、実践を踏まえてご紹介します。</p>



<p>実際に使用したコードやその実行結果も全て書いているので、わかりやすいです。</p>


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


<p><span class="swl-marker mark_orange">この記事を読めば<strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトで足し算する方法</span></strong>がより理解できます。</span></p>



<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">シェルスクリプトで足し算する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="509" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-5-1024x509.jpg" alt="足し算" class="wp-image-4091" srcset="https://sugublog.org/wp-content/uploads/2021/08/school_08-5-1024x509.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5-300x149.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5-768x382.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトで足し算する方法</span></strong>は以下です。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトで足し算する方法</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>「((a=数字+数字))」を使用する</li>



<li>「declare -i 変数」を使用する</li>



<li>「$((数字+数字))」を使用する</li>
</ul>
</div></div>



<h3 class="wp-block-heading">足し算する方法１、「((a=数字+数字))」を使用する</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>((a=1+1))
echo $a</code></pre></div>



<p><span class="swl-marker mark_orange">(())で囲む事で<strong>シェルスクリプトで足し算</strong>する事が出来ます。</span></p>



<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="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>(( b = 2 + 2 ))
echo $b</code></pre></div>



<p class="is-style-icon_pen">(())で囲っていない場合はスペースがあればエラーとなります。</p>



<p>また以下のように変数も＄無しで展開する事が出来ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>x=3
y=3
(( c=x+y ))
echo $c</code></pre></div>


<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>今までのシェルスクリプトを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-file="addition.sh" data-lang="Bash"><code>#!/bin/bash

((a=1+1))
echo $a

(( b = 2 + 2 ))
echo $b

x=3
y=3
(( c=x+y ))
echo $c</code></pre></div>



<p class="is-style-icon_pen">シェルスクリプトを実行する方法は以下で詳しく説明しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -external" 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.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/" target="_blank" rel="noopener noreferrer">【シェルスクリプト基礎】コマンドを実行する様々な方法をご紹介！ | 【初心者向け】エラー対処法あり！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトを実行する時に困った事はありませんか？ シェルスクリプトのコマンド実行での失敗メッセージ一覧 「実行権限がありません」 「コマンドが見つかりません&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>シェルスクリプトを実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
2
4
6</code></pre></div>



<p>エラー無く成功しています。</p>



<h3 class="wp-block-heading">足し算する方法２、「declare -i　変数」を使用する</h3>



<p>しかし<span class="swl-marker mark_yellow">上記で行った<strong><span class="swl-inline-color has-swl-deep-01-color">足し算</span></strong>はその場限りで、<strong>変数に入り文字列</strong>になってしまいます。</span></p>



<p>更に足し算をしたい場合、思った結果になりません。</p>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

((a=1+1))
echo $a
a+=1
echo $a</code></pre></div>



<p>すると結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
21</code></pre></div>



<p>先程足し算した「2」という文字列と「1」という文字列が足し算されて「21」という文字列になりました。</p>



<p>ここで使用するのが<strong><span class="swl-inline-color has-swl-deep-01-color">「declare -i　変数」を使った構文</span></strong>です。</p>



<p>これは<span class="swl-marker mark_orange">変数を数値として見てくれます。</span></p>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

declare -i d
d=5+5
echo $d</code></pre></div>



<p>実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
10</code></pre></div>



<p>シェルスクリプトで足し算が出来ています。</p>



<p>引き続き計算してみましょう。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

declare -i d
d=5+5
echo $d

d+=1
echo $d</code></pre></div>



<p>先程足し算した「10」に１を足し算して「11」になっています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
10
11</code></pre></div>



<p class="is-style-icon_announce">繰り返しの足し算などに使用できそうですね。</p>



<h3 class="wp-block-heading">足し算する方法３、「$((数字+数字))」を使用する</h3>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトで足し算してそのまま表示する</span></strong>には、以下のようにコードを書きます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>echo $((4+4))</code></pre></div>



<p>これでも足し算が出来ます。</p>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

echo $((4+4))</code></pre></div>



<p>シェルスクリプトを実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
8</code></pre></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="683" src="https://sugublog.org/wp-content/uploads/2021/06/shutterstock_1433996435-min-1024x683.jpg" alt="エラー" class="wp-image-2885"/></figure>
</div>


<p>シェルスクリプトで足し算するコードは紹介しました。</p>



<p>以下のようにすると<strong><span class="swl-bg-color has-swl-deep-01-background-color">足し算できずにエラー</span></strong>となってしまいます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

e=((6+6))
echo $e</code></pre></div>



<p>エラーが出るシェルスクリプトを実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash addition.sh
addition.sh: 行 3: 予期しないトークン `(&#39; 周辺に構文エラーがあります
addition.sh: 行 3: `e=((6+6))&#39;</code></pre></div>



<p>変数ごと(())の中に入れてあげる様にしましょう！</p>



<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">シェルスクリプトで足し算する方法のまとめ</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="566" src="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-5-1024x566.jpg" alt="script" class="wp-image-3968" srcset="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-5-1024x566.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-5-300x166.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-5-768x424.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-marker mark_orange">シェルスクリプト内で足し算する方法</span></span></strong>は以下です</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトで足し算する方法</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>「((a=数字+数字))」を使用する</li>



<li>「declare -i 変数」を使用する</li>



<li>「$((数字+数字))」を使用する</li>
</ul>
</div></div>



<p>よくやってしまいがちな<span class="swl-bg-color has-swl-deep-01-background-color">エラーになる足し算のコード</span>は以下です。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトで足し算したらエラーになるコード</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>e=((6+6))</li>
</ul>
</div></div>



<p>シェルスクリプトで足し算する時は、普通にすると文字列と文字列の足し算になるので気を付けてください。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shell-script-addition/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
