<?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%ae%9f%e8%a1%8c/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Tue, 27 Feb 2024 14:15:54 +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-execution/</link>
					<comments>https://sugublog.org/shell-script-execution/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Thu, 06 Apr 2023 13:48:36 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[実行]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5461</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトを実行する時に困った事はありませんか？ 上記のようなメッセージが出て、シェルスクリプトを実行するときに戸惑った人も多いと思います。 そこで今回はシェルスクリプトを実行する色々な方法を記事にしました。 それ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>
<p><span class="swl-marker mark_yellow"><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトを実行</span></strong>する時に困った事はありませんか？</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>「実行権限がありません」</li>



<li>「コマンドが見つかりません」</li>
</ul>
</div></div>



<p>上記のようなメッセージが出て、シェルスクリプトを実行するときに戸惑った人も多いと思います。</p>



<p>そこで今回は<strong><span class="swl-marker mark_orange"><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトを実行する色々な方法</span></span></strong>を記事にしました。</p>



<p class="is-style-big_icon_point">それぞれの特徴と掴んで最適なシェルスクリプト実行方法を選べば、効率よく作業する事が可能になります。</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">シェルスクリプトを実行する方法</h2>



<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>実行権を与えて相対パスでシェルスクリプトを実行する</li>



<li>bashコマンドでシェルスクリプトを実行する</li>



<li>sourceコマンドでシェルスクリプトを実行する</li>
</ul>
</div></div>



<p>一つ一つ見ていきます。</p>



<h2 class="wp-block-heading">実行権を与えて相対パスでシェルスクリプトを実行する</h2>



<div class="wp-block-cover aligncenter"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img decoding="async" width="1024" height="640" class="wp-block-cover__image-background wp-image-3848" alt="実行権を与えて相対パスでシェルスクリプトを実行" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1-1024x640.jpg" data-object-fit="cover" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1-1024x640.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1-300x188.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1-768x480.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /><div class="wp-block-cover__inner-container is-layout-flow wp-block-cover-is-layout-flow">
<p class="has-text-align-center has-large-font-size">実行権を与えて相対パスでシェルスクリプトを実行する</p>
</div></div>



<p>一番は本来の<strong><span class="swl-marker mark_orange">シェルスクリプトに実行権限を与えて相対パスで指定する</span></strong>事です。</p>



<h3 class="wp-block-heading">相対パスでシェルスクリプトを実行する手順</h3>



<p>手順は以下の通りです</p>



<div class="swell-block-step" data-num-style="circle">
<div class="swell-block-step__item"><div class="swell-block-step__number u-bg-main"><span class="__label">STEP</span></div><div class="swell-block-step__title u-fz-l">シェルスクリプトを作成します。</div><div class="swell-block-step__body">
<p>以下のシェルスクリプトを作成してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>vi シェルスクリプト</code></pre></div>
</div></div>



<div class="swell-block-step__item"><div class="swell-block-step__number u-bg-main"><span class="__label">STEP</span></div><div class="swell-block-step__title u-fz-l">シェルスクリプトに実行権限を与えます</div><div class="swell-block-step__body">
<p>以下のコマンドでシェルスクリプトに実行権限を与えます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>chmod +x シェルスクリプト</code></pre></div>
</div></div>



<div class="swell-block-step__item"><div class="swell-block-step__number u-bg-main"><span class="__label">STEP</span></div><div class="swell-block-step__title u-fz-l">相対パスでシェルスクリプトを実行します</div><div class="swell-block-step__body">
<p>以下のコマンドでシェルスクリプトを実行します</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>./シェルスクリプト</code></pre></div>
</div></div>
</div>



<h3 class="wp-block-heading">相対パスでシェルスクリプトを実行してみる</h3>



<p>実際にシェルスクリプトを作成して実行してみます。</p>



<p>以下のシェルスクリプトを作成しました。</p>



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

message=&#39;シェルスクリプト実行しました！&#39;

echo $message</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/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>「ls -l」コマンドで現ディレクトリをみてみます。</p>



<p>ここで一度相対パスで実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# ./shell_script_execution.sh
-bash: ./shell_script_execution.sh: 許可がありません</code></pre></div>



<p><span class="swl-bg-color has-swl-deep-01-background-color">「許可がありません」と出て実行できません。</span></p>



<h3 class="wp-block-heading">シェルスクリプトで実行できない場合の対処法</h3>



<p>「ls -l」コマンドで確認してみましょう。</p>


<div class="wp-block-image is-style-photo_frame">
<figure class="aligncenter size-full"><img decoding="async" width="619" height="59" src="https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_before.png" alt="シェルスクリプト作成" class="wp-image-5466" srcset="https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_before.png 619w, https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_before-300x29.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></figure>
</div>


<p>「x」がないので実行権限がありません。</p>



<p>実行権限を付けてみましょう。</p>



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



<p>もう一度見てみましょう。</p>


<div class="wp-block-image is-style-photo_frame">
<figure class="aligncenter size-full"><img decoding="async" width="641" height="57" src="https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_after.png" alt="シェルスクリプトの実行権限付与" class="wp-image-5465" srcset="https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_after.png 641w, https://sugublog.org/wp-content/uploads/2023/04/shell_script_execution_after-300x27.png 300w" sizes="(max-width: 641px) 100vw, 641px" /></figure>
</div>


<p><span class="swl-bg-color has-swl-deep-03-background-color">すべてに「x」(実行権限)が付与されて、ファイルが緑色になりました。</span></p>



<p>シェルスクリプトを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# ./shell_script_execution.sh
シェルスクリプト実行しました！</code></pre></div>



<p><span class="swl-bg-color has-swl-deep-02-background-color">シェルスクリプトを相対パスで実行する事が出来ました。</span></p>



<p>これが一般的なやり方になります。</p>



<p class="is-style-big_icon_caution">しかし<span class="swl-marker mark_yellow">シェルスクリプトを試したりするのに、実行権限などをいちいちつけるのは面倒</span>です。</p>



<h2 class="wp-block-heading">bashコマンドでシェルスクリプトを実行する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="560" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg" alt="シェルスクリプトを実行" class="wp-image-4561" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-300x164.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-768x420.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>次は<strong><span class="swl-inline-color has-swl-deep-01-color">「bash」コマンドの後にシェルスクリプトを書く方法</span></strong>です。</p>



<h3 class="wp-block-heading">bashコマンドで実際にシェルスクリプトを実行してみる</h3>



<p>もう一度同じ内容のシェルスクリプトを作成します。</p>



<p>シェルスクリプト名を「shell_script_execution2.sh」としました。実行権限はありません。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# ls -l
合計 8
-rwxr-xr-x. 1 root root 84  4月  6 21:57 shell_script_execution.sh
-rw-r--r--. 1 root root 84  4月  6 22:28 shell_script_execution2.sh</code></pre></div>



<p>bashコマンドでシェルスクリプトを実行します</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash shell_script_execution2.sh
シェルスクリプト実行しました！</code></pre></div>



<p>実行できました。</p>



<p class="is-style-big_icon_check"><span class="swl-marker mark_orange">bashコマンドでは権限が不要で実行できるので、<strong>テストや開発</strong>ではこちらがオススメです。</span></p>



<h2 class="wp-block-heading">sourceコマンドでシェルスクリプトを実行する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="682" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" alt="シェルスクリプトを実行" class="wp-image-4502" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-768x511.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>今度は<strong><span class="swl-inline-color has-swl-deep-01-color">「source」コマンドでシェルスクリプトを実行</span></strong>してみます。</p>



<p>前と同じく実行権のないシェルスクリプトを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# source shell_script_execution2.sh
シェルスクリプト実行しました！</code></pre></div>



<p>こちらでも実行できました。</p>



<p>また「source」コマンドは「.」に省略できます。実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# . shell_script_execution2.sh
シェルスクリプト実行しました！</code></pre></div>



<p>こちらでも実行できています。</p>



<h3 class="wp-block-heading">sourceコマンドで実行する時の注意点</h3>



<p><span class="swl-inline-color has-swl-deep-01-color">「bash」コマンドとの違い</span>は現bashシェルで動くので、<span class="swl-bg-color has-swl-deep-01-background-color">変数や関数などが保存</span>されます。</p>



<p>先程のファイルで変数「message」を使用していました。</p>



<p>こちらの変数の値が残っているか「echo $message」で試してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# echo $message
シェルスクリプト実行しました！</code></pre></div>



<p>$message変数に入っていた「シェルスクリプトを実行しました！」という文字が残っています。</p>



<p>setコマンドで何が残っているか確認出来ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# set
省略～
message=シェルスクリプト実行しました！</code></pre></div>



<p>元々設定されている値がほとんどですが、最後に<span class="swl-bg-color has-swl-deep-01-background-color">「message」変数がはいっていました。</span></p>



<p>削除したい場合は「unset」コマンドで可能です。</p>



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



<p>setコマンドで確認</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>～省略～
XDG_SESSION_ID=122
_=message
colors=/root/.dircolors
[root@localhost workspace]#</code></pre></div>



<p><span class="swl-bg-color has-swl-deep-02-background-color">「message」変数が消えていました。</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="576" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-1024x576.jpg" alt="スクリプト" class="wp-image-3870" srcset="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>シェルスクリプトでコマンド実行する方法は以下です。</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>実行権を与えて相対パスでシェルスクリプトを実行する</li>



<li>bashコマンドでシェルスクリプトを実行する</li>



<li>sourceコマンドか.(省略形)でシェルスクリプトを実行する</li>
</ul>
</div></div>



<p>それぞれの特徴を以下にまとめます。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><thead><tr><th>実行方法</th><th>実行環境</th><th>使うべき環境</th><th>メリットデメリット</th></tr></thead><tbody><tr><td>相対パスで実行</td><td>カレントシェルの<br>子プロセス</td><td>運用環境</td><td>権限付与が面倒</td></tr><tr><td>bashコマンドで実行</td><td>カレントシェルの<br>子プロセス</td><td>テストや開発</td><td>気軽に試せる</td></tr><tr><td>sourceコマンドか<br>.（省略形） で実行</td><td>カレントシェル</td><td>テストや開発で<br>変数なども残したい</td><td>変数などが残り、運用と違う動きをする可能性あり</td></tr></tbody></table><figcaption class="wp-element-caption">シェルスクリプト実行のやり方の特徴一覧</figcaption></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shell-script-execution/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
