﻿<?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>シェルスクリプト  |  Web Creators Hub</title>
	<atom:link href="https://web-creators-hub.com/category/linux/%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88/feed/" rel="self" type="application/rss+xml" />
	<link>https://web-creators-hub.com</link>
	<description>WEB技術などの情報をわかりやすく配信するメディア</description>
	<lastBuildDate>Wed, 08 Feb 2023 01:56:31 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.8.2</generator>
	<item>
		<title>シェルスクリプトで任意の文字入力待ち→入力→確認→完了 を作成</title>
		<link>https://web-creators-hub.com/linux/shell-s/</link>
		<pubDate>Thu, 21 Nov 2019 15:46:13 +0000</pubDate>
		<dc:creator><![CDATA[taizo]]></dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[シェルスクリプト]]></category>

		<guid isPermaLink="false">https://web-creators-hub.com/?p=500</guid>
		<description><![CDATA[ウェブの入力フォームのようにLinuxのシェルスクリプトで再現します。再現するためには入力を受け付ける「READ」と画面遷移のように確認→完...]]></description>
				<content:encoded><![CDATA[<p>ウェブの入力フォームのようにLinuxのシェルスクリプトで再現します。再現するためには入力を受け付ける「READ」と画面遷移のように確認→完了を再現するために繰り返し「while」「case」を使用します。</p>
<p><img src="https://web-creators-hub.com/__wordpress/wp-content/uploads/2019/11/shell1.gif" alt="" width="464" height="432" class="alignnone size-full wp-image-503" /></p>
<h2>read 入力を受け付ける</h2>
<p>readコマンドでユーザーから入力を受け付ける部分を作成します。オプション「-p」を付けると入力する前に表示するプロンプトの文字を設定できます。</p>
<p><span class="bold">・使用例</span></p>
<pre class="brush: bash; title: ; notranslate">
echo &quot;+-------------------------------------------------------+&quot;
echo &quot;文字を入力してください。&quot;
echo &quot;+-------------------------------------------------------+&quot;
read -p &quot;:&quot; INPUT_STR
echo &quot;入力した文字は[$INPUT_STR]です。&quot;
read -p &quot;ENTERキーを押してください。&quot;
</pre>
<h2>while true do done で無限に繰り返す</h2>
<p>while trueにすれば無限に繰り返します。これを利用して確認から文字入力に戻るところを再現します。繰り返しのループから抜けるにはbreakを使用します。</p>
<p><span class="bold">・使用例</span></p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash
i=0
while true
do
  echo $i
  i=`expr $i + 1`
  if [ $i -eq 10 ]; then
    break
  fi
done
</pre>
<h2>case</h2>
<p>caseを利用して確認画面のYes、noの処理を作成します。</p>
<p><span class="bold">・使用例</span></p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash
read -p &quot; (y/n):&quot; CHECK_YN
case &quot;$CHECK_YN&quot; in
[yY])
    echo &quot;YES&quot;  
;;
[nN])
    echo &quot;NO&quot;
;;
esac
read -p &quot;ENTERキーを押してください。: &quot;
clear
</pre>
<h2>文字入力→確認→完了</h2>
<p>「read」「while」「case」を使って任意の文字入力→確認→完了 を完成させます。<br />
以下がサンプルコードになります。</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash
while true
do
clear
echo &quot;+-------------------------------------------------------+&quot;
echo &quot;文字を入力してください。&quot;
echo &quot;+-------------------------------------------------------+&quot;
read -p &quot;:&quot; INPUT_STR
echo &quot;+-------------------------------------------------------+&quot;
echo &quot;入力した文字は[$INPUT_STR]でよろしいですか？&quot;
echo &quot;+-------------------------------------------------------+&quot;
read -p &quot; (y/n):&quot; CHECK_YN
case &quot;$CHECK_YN&quot; in
  [yY])
echo &quot;+-------------------------------------------------------+&quot;
echo &quot;完了しました。&quot;
echo &quot;+-------------------------------------------------------+&quot;
  ;;
  [nN]) echo &quot;作成はキャンセルされました。&quot; ;;
esac

read -p &quot;処理を終了しますか? (y/N): &quot; yn
case &quot;$yn&quot; in
  [yY]) break
  ;;
esac
done
read -p &quot;ENTERキーを押してください。: &quot;
clear
</pre>
]]></content:encoded>
			</item>
	</channel>
</rss>
