<?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>SecureCRT &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/securecrt/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:51:35 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>shell脚本因文件编码问题不能正确执行的问题</title>
		<link>https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 14 Dec 2010 12:26:44 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[ANSI]]></category>
		<category><![CDATA[BOM]]></category>
		<category><![CDATA[cannot execute binary file]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[ps]]></category>
		<category><![CDATA[SecureCRT]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[UltraEdit]]></category>
		<category><![CDATA[UTF-8]]></category>
		<category><![CDATA[UTF8]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[去除]]></category>
		<category><![CDATA[文件]]></category>
		<category><![CDATA[汉字]]></category>
		<category><![CDATA[编码]]></category>
		<category><![CDATA[错误]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=912</guid>

					<description><![CDATA[<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">遇到一例因shell文件的编码问题导致执行后不能得到正确结果的问题。一般来说脚本中不会带有中文，不过有时候例外，例如你要查找一个文本中的字符串，cat test.txt &#124; grep &#34;汽车&#34; &#160;这样的命令，如果文件编码不对，就可能得不到正确结果。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span id="more-912"></span></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">我的问题是：有一个脚本，无论怎么执行结果都不对，但是我把它里面的关键的几句话单独拿出来，在SecureCRT终端下依次执行，最后得到的结果却正确，奇了。于是我终于想起来可能有中文的影响，在终端里查看shell脚本的内容，发现不能正确显示出一处中文（乱码了），可见正是这处乱码，导致了grep输出的结果不正确。但是在SecureCRT终端下，无论是用vi还是emacs，我都无法向文件中输入中文（上屏的中文全都乱码），于是怀疑是文件编码问题。</span></span></p>
<p>
	<span style="font-size: 14px; color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" style="font-size: 14px; " target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">然后我就将脚本下载到Windows下，用UltraEdit将其另存为UTF-8格式，再上传到Linux中，再执行脚本，竟然报错：<strong><span style="color:#f00;">cannot execute binary file</span></strong></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">这种问题的原因之一是脚本中有不可识别字符。于是我再用UltraEdit，将文件重新存为&#8220;<strong><span style="color:#00f;">UTF-8 无 BOM</span></strong>&#8221;格式（如下图），再上传到Linux中，就可以正确执行了。</span></span></p>
<p style="text-align: center; ">
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><img decoding="async" alt="另存为UTF-8 无BOM格式" src="http://www.codelast.com/wp-content/uploads/2011/11/utf-8_no_bom_file_format.jpg" style="width: 353px; height: 103px; " /><br />
	</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">网上还有很多文章，介绍了如何用emacs，vi 或 vim更改文件编码的方法，应该也是可以的，但用UltraEdit最方便。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">另外再记一个小技巧：用 ps -ef &#124; grep XXX 命令查看系统中是否存在某个进程的时候，输出结果中会包含有grep命令这一行，例如：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef &#124; grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">部分输出为：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
root     27154 27129  0 20:35 pts/3    00:00:00 grep httpd
daemon   28834  2955  3 13:35 ?</pre>&#8230; <a href="https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">遇到一例因shell文件的编码问题导致执行后不能得到正确结果的问题。一般来说脚本中不会带有中文，不过有时候例外，例如你要查找一个文本中的字符串，cat test.txt | grep &quot;汽车&quot; &nbsp;这样的命令，如果文件编码不对，就可能得不到正确结果。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span id="more-912"></span></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">我的问题是：有一个脚本，无论怎么执行结果都不对，但是我把它里面的关键的几句话单独拿出来，在SecureCRT终端下依次执行，最后得到的结果却正确，奇了。于是我终于想起来可能有中文的影响，在终端里查看shell脚本的内容，发现不能正确显示出一处中文（乱码了），可见正是这处乱码，导致了grep输出的结果不正确。但是在SecureCRT终端下，无论是用vi还是emacs，我都无法向文件中输入中文（上屏的中文全都乱码），于是怀疑是文件编码问题。</span></span></p>
<p>
	<span style="font-size: 14px; color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" style="font-size: 14px; " target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">然后我就将脚本下载到Windows下，用UltraEdit将其另存为UTF-8格式，再上传到Linux中，再执行脚本，竟然报错：<strong><span style="color:#f00;">cannot execute binary file</span></strong></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">这种问题的原因之一是脚本中有不可识别字符。于是我再用UltraEdit，将文件重新存为&ldquo;<strong><span style="color:#00f;">UTF-8 无 BOM</span></strong>&rdquo;格式（如下图），再上传到Linux中，就可以正确执行了。</span></span></p>
<p style="text-align: center; ">
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><img decoding="async" alt="另存为UTF-8 无BOM格式" src="http://www.codelast.com/wp-content/uploads/2011/11/utf-8_no_bom_file_format.jpg" style="width: 353px; height: 103px; " /><br />
	</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">网上还有很多文章，介绍了如何用emacs，vi 或 vim更改文件编码的方法，应该也是可以的，但用UltraEdit最方便。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">另外再记一个小技巧：用 ps -ef | grep XXX 命令查看系统中是否存在某个进程的时候，输出结果中会包含有grep命令这一行，例如：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef | grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">部分输出为：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
root     27154 27129  0 20:35 pts/3    00:00:00 grep httpd
daemon   28834  2955  3 13:35 ?        00:16:23 /usr/local/apache2/bin/httpd -k start
</pre>
<p>
	<span style="font-size: 14px; ">可见第一行是干扰，我们不想看到它，那么可以有两种方法来过滤掉这一行：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef | grep httpd | grep -v grep
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><font class="Apple-style-span">或：</font></span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -Ao pid,comm,args | grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><font class="Apple-style-span">这种方法输出的内容中只包含进程号、进程名、调用参数，具体使用可man ps。</font></span></span></p>
<p>
	<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
