<?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>夜弓的咖啡馆 &#187; study</title>
	<atom:link href="http://yegong.net/tag/study/feed/" rel="self" type="application/rss+xml" />
	<link>http://yegong.net</link>
	<description>夜弓=Cooper=某间咖啡室的老板(尚未开张)</description>
	<lastBuildDate>Tue, 27 Jul 2010 04:18:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>bash script notes (1)</title>
		<link>http://yegong.net/bash-script-notes-1/</link>
		<comments>http://yegong.net/bash-script-notes-1/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 11:01:12 +0000</pubDate>
		<dc:creator>cooper</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[笔记]]></category>

		<guid isPermaLink="false">http://yegong.net/?p=815</guid>
		<description><![CDATA[I tied to study the shell script a few times. But on that time, I didn&#8217;t get enough knowledge to deal with it. Now, I&#8217;m back. Situation 1 I want to startup the vpn service when the computer startup. I added pon vpn_name to rc.local. However, this can be failed because when this line execute, [...]]]></description>
			<content:encoded><![CDATA[<p>I tied to study the shell script a few times. But on that time, I didn&#8217;t get enough knowledge to deal with it. Now, I&#8217;m back.</p>
<p><strong>Situation 1</strong></p>
<p>I want to startup the vpn service when the computer startup. I added <span class="code">pon vpn_name</span> to rc.local. However, this can be failed because when this line execute, the network may not be ready. So I decide to write a script to do this. It will wait until the Internet up. Here&#8217;s it.</p>
<pre>#!/bin/bash

sleep_time=1
export PATH=$PATH:/usr/bin

while [ 1 ]
do
  ping_success=`ping -c 1 -n vpn.yegong.net | \
    sed -n -r 's/^.* (.) received.*$/\1/p'`
  if [ $ping_success -eq 1 ]
  then
    pon yegong
    exit 0
  fi
  sleep $sleep_time
  sleep_time=$(expr $sleep_time + 1)
done</pre>
<ol>
<li><span class="code">export PATH</span> is very important.</li>
<li><span class="code">`ping -c 1 -n vpn.yegong.net | sed -n -r &#8216;s/^.* (.) received.*$/\1/p&#8217;`</span>, using ` surround the command to get the output text.</li>
<li><span class="code">sed -n -r &#8216;s/^.* (.) received.*$/\1/p&#8217;</span>, using sed to simplify the output. It&#8217;s the regular expression. Very similar with <a href="http://yegong.net/tag/vim/">VIM replace syntax</a>, isn&#8217;t it?</li>
<li><span class="code">if [ $ping_success -eq 1 ]</span>, <span class="code">[ condition ]</span> means the test program, equivalent to <span class="code">test condition</span>, so <span class="code">man test</span> will show more usage.</li>
<li><span class="code">sleep_time=$(expr $sleep_time + 1)</span>, it seems that every variable in shell is a string, so you can&#8217;t simply write <span class="code">x=x+1</span>. Instead of,  expr program read a string expression and output the results. Please notice the space in the expression.</li>
</ol>
<p><strong>Situation 2</strong></p>
<p>For each text files in a directory, convert the file encoding to utf8.</p>
<p>There&#8217;s a simple util can guess the file encoding. It can be found at <a href="http://wyw.dcweb.cn/">Wu Yongwei&#8217;s Programming Page</a> (<del>Can&#8217;t access when I write the article</del>). Then I just need a script to combine the tellenc and iconv.</p>
<pre>TMP_FILE="/tmp/utf8lize.output"
ENCODING="utf-8"

if [ $# == 0 ]
then
  echo "Usage: utf8lize FILES"
  exit 1
fi

for f in "$@"
do
  if [ -f "$f" ]
  then
    enc=`tellenc "$f"`
    if [ $enc != $ENCODING ] &amp;&amp; [ $enc != "binary" ]
    then
      echo $f : $enc
      cp "$f" "$f.bak"
      iconv -f "$enc" -t "$ENCODING" -o "$TMP_FILE" "$f"
      cp "$TMP_FILE" "$f"
      rm -f "$TMP_FILE"
    fi
  fi
done</pre>
<ol>
<li><span class="code">$#</span> results the number of arguments when it been executed.</li>
<li><span class="code">for f in &#8220;$@&#8221;</span> is the for-each loop. And <span class="code">&#8220;$@&#8221;</span> indicates the all program arguments. In addition, when you run the program <span class="code">utf8lize *</span>, * will convert to filenames array. Using the &#8221; to surrounding <span class="code">$@</span> can deal the filename with space.</li>
</ol>
<p><a href="http://yegong.net/bash-script-notes-1/#comments">Leave a comment</a></p>
<p>Category : <a href="http://yegong.net/category/coding/" title="View all posts in Coding" rel="category tag">Coding</a> | Tags : <a href="http://yegong.net/tag/bash/" rel="tag">bash</a>, <a href="http://yegong.net/tag/linux/" rel="tag">linux</a>, <a href="http://yegong.net/tag/script/" rel="tag">script</a>, <a href="http://yegong.net/tag/study/" rel="tag">study</a>, <a href="http://yegong.net/tag/tutorial/" rel="tag">tutorial</a>, <a href="http://yegong.net/tag/note/" rel="tag">笔记</a></p>
<!-- Analytics by image -->
<img alt="analytics image" src="http://img.users.51.la/2657393.asp" style="border:none;height:1px;width:1px;" />]]></content:encoded>
			<wfw:commentRss>http://yegong.net/bash-script-notes-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->