http://linux-wiki.cn/index.php?title=%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause&feed=atom&action=history
用shell实现bat的pause - 修订历史
2024-03-29T12:40:56Z
本站上此页的修订历史
MediaWiki 1.17.0
http://linux-wiki.cn/index.php?title=%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause&diff=2124&oldid=prev
Lenzo537: /* 改进 */
2013-07-05T05:04:50Z
<p><span class="autocomment">改进</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">←上一修订</td>
<td colspan='2' style="background-color: white; color:black;">在2013年7月5日 (五) 05:04所做的修订版本</td>
</tr><tr><td colspan="2" class="diff-lineno">第23行:</td>
<td colspan="2" class="diff-lineno">第23行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang=bash></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang=bash></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>read -n 1 -p "Press any key to continue..." INP</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>read -n 1 -p "Press any key to continue..." INP</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div>if <del class="diffchange diffchange-inline">[</del>[ $INP != '' <del class="diffchange diffchange-inline">]</del>] ; then</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>if [ $INP != '' ] ; then</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         echo -ne '\b \n'</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         echo -ne '\b \n'</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>fi</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>fi</div></td></tr>
</table>
Lenzo537
http://linux-wiki.cn/index.php?title=%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause&diff=2123&oldid=prev
Lenzo537: /* 改进 */
2013-07-05T05:03:37Z
<p><span class="autocomment">改进</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">←上一修订</td>
<td colspan='2' style="background-color: white; color:black;">在2013年7月5日 (五) 05:03所做的修订版本</td>
</tr><tr><td colspan="2" class="diff-lineno">第37行:</td>
<td colspan="2" class="diff-lineno">第37行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>function pause(){</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>function pause(){</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         read -n 1 -p "$*" INP</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         read -n 1 -p "$*" INP</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div>         if <del class="diffchange diffchange-inline">[</del>[ $INP != '' <del class="diffchange diffchange-inline">]</del>] ; then</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>         if [ $INP != '' ] ; then</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>                 echo -ne '\b \n'</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>                 echo -ne '\b \n'</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         fi</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         fi</div></td></tr>
</table>
Lenzo537
http://linux-wiki.cn/index.php?title=%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause&diff=802&oldid=prev
Chenxing在2008年6月24日 (二) 03:20
2008-06-24T03:20:46Z
<p></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">←上一修订</td>
<td colspan='2' style="background-color: white; color:black;">在2008年6月24日 (二) 03:20所做的修订版本</td>
</tr><tr><td colspan="2" class="diff-lineno">第9行:</td>
<td colspan="2" class="diff-lineno">第9行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 用户输入的字符会显示在屏幕上,影响美观。</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 用户输入的字符会显示在屏幕上,影响美观。</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 如果进行了输入重定向,哪怕有:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 如果进行了输入重定向,哪怕有:</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div><bash></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><<ins class="diffchange diffchange-inline">source lang=</ins>bash></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>while read line</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>while read line</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>do</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>do</div></td></tr>
<tr><td colspan="2" class="diff-lineno">第16行:</td>
<td colspan="2" class="diff-lineno">第16行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> …</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> …</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>done < somefile</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>done < somefile</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div></<del class="diffchange diffchange-inline">bash</del>></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></<ins class="diffchange diffchange-inline">source</ins>></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 这样的代码,都会导致错误。</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 这样的代码,都会导致错误。</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==改进==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==改进==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 关于重定向引发的错误,如果需要,可以用文件描述符代替重定向解决。目前没有想到更好的解决方案。主要针对前两个不足做了改进。</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 关于重定向引发的错误,如果需要,可以用文件描述符代替重定向解决。目前没有想到更好的解决方案。主要针对前两个不足做了改进。</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div><bash></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><<ins class="diffchange diffchange-inline">source lang=</ins>bash></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>read -n 1 -p "Press any key to continue..." INP</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>read -n 1 -p "Press any key to continue..." INP</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>if [[ $INP != '' ]] ; then</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>if [[ $INP != '' ]] ; then</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         echo -ne '\b \n'</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>         echo -ne '\b \n'</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>fi</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>fi</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div></<del class="diffchange diffchange-inline">bash</del>></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></<ins class="diffchange diffchange-inline">source</ins>></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 说明:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 说明:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 记录了输入的字符(存到$INP),以便处理。</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* 记录了输入的字符(存到$INP),以便处理。</div></td></tr>
<tr><td colspan="2" class="diff-lineno">第33行:</td>
<td colspan="2" class="diff-lineno">第33行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 为了方便使用,可以写成:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 为了方便使用,可以写成:</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div><bash></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><<ins class="diffchange diffchange-inline">source lang=</ins>bash></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>#! /bin/bash</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>#! /bin/bash</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>function pause(){</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>function pause(){</div></td></tr>
<tr><td colspan="2" class="diff-lineno">第44行:</td>
<td colspan="2" class="diff-lineno">第44行:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>#使用时:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>#使用时:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>pause 'Press any key to continue...'</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>pause 'Press any key to continue...'</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div></<del class="diffchange diffchange-inline">bash</del>></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></<ins class="diffchange diffchange-inline">source</ins>></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 仍存有的问题:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> 仍存有的问题:</div></td></tr>
</table>
Chenxing
http://linux-wiki.cn/index.php?title=%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause&diff=346&oldid=prev
Chenxing: 新页面: windows批处理程序中的pause命令可以实现“按任意键继续”的效果,要在shell中实现这一功能,可以借助read。 {{Note|注意不是所有的shell提供...
2007-08-05T15:00:57Z
<p>新页面: windows批处理程序中的pause命令可以实现“按任意键继续”的效果,要在<a href="/wiki/Shell" title="Shell">shell</a>中实现这一功能,可以借助read。 {{Note|注意不是所有的shell提供...</p>
<p><b>新建页面</b></p><div>windows批处理程序中的pause命令可以实现“按任意键继续”的效果,要在[[shell]]中实现这一功能,可以借助read。<br />
{{Note|注意不是所有的shell提供的read都支持本文介绍的方法。目前本文已确定适用于[[bash]]。}}<br />
<br />
==简单实现==<br />
你可能还没注意到read的强大功能,其实只要用下面的代码,就可以实现简单的"pause"。<br />
read -n 1 -p "Press any key to continue..."<br />
但它有明显的不足:<br />
* 如果用户输入的不是回车,虽然能"continue",但没有输出换行,导致输出格式混乱。(直接在命令提示符下运行更为突出)。<br />
* 用户输入的字符会显示在屏幕上,影响美观。<br />
* 如果进行了输入重定向,哪怕有:<br />
<bash><br />
while read line<br />
do<br />
…<br />
read -p somevar<br />
…<br />
done < somefile<br />
</bash><br />
这样的代码,都会导致错误。<br />
<br />
==改进==<br />
关于重定向引发的错误,如果需要,可以用文件描述符代替重定向解决。目前没有想到更好的解决方案。主要针对前两个不足做了改进。<br />
<bash><br />
read -n 1 -p "Press any key to continue..." INP<br />
if [[ $INP != '' ]] ; then<br />
echo -ne '\b \n'<br />
fi<br />
</bash><br />
说明:<br />
* 记录了输入的字符(存到$INP),以便处理。<br />
* 注意如果输入的是回车,read一个字符也读到,而不是读到\n。<br />
* 当输入不是换行时,用退格盖掉原来的空格。<br />
<br />
为了方便使用,可以写成:<br />
<bash><br />
#! /bin/bash<br />
function pause(){<br />
read -n 1 -p "$*" INP<br />
if [[ $INP != '' ]] ; then<br />
echo -ne '\b \n'<br />
fi<br />
}<br />
<br />
#使用时:<br />
pause 'Press any key to continue...'<br />
</bash><br />
<br />
仍存有的问题:<br />
* 对空格和Tab支持不好。<br />
* 重定向的问题没有解决。<br />
<br />
==参考资料==<br />
* http://www.cyberciti.biz/tips/linux-unix-pause-command.html<br />
<br />
[[Category: shell技巧]]</div>
Chenxing