blog/content/post/2010/11/03/2010-11-03-00001414.md

80 lines
9.5 KiB
Markdown

---
title: Gaucheでスクリプトのあるディレクトリを取得する
author: kazu634
date: 2010-11-03
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5375;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- Lisp
---
<div class="section">
<p>
実行しているスクリプトファイルがあるディレクトリを取得しようとしたのだが、探し方が悪いのかそのものずばりの組み込み関数を見つけられず。手作りしてみました。こんな感じになるのかな?
</p>
<h4>
ソース
</h4>
<pre class="syntax-highlight">
<span class="synSpecial">(</span>use file.util<span class="synSpecial">)</span>
<span class="synComment">;; (car *argv*)にスクリプトファイルが束縛されるけど、</span>
<span class="synComment">;; スクリプトファイルの存在するディレクトリでスクリプトを起動すると、</span>
<span class="synComment">;; 「スクリプトファイル名.scm」となる。</span>
<span class="synComment">;; スクリプトファイルの存在するディレクトリ以外の場所で</span>
<span class="synComment">;; スクリプトを起動すると、(car *argv*)はフルパス表記になる</span>
<span class="synComment">;; そこを区別する。</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>is-executed-at-script-directory? script-file<span class="synSpecial">)</span>
<span class="synSpecial">(</span>receive <span class="synSpecial">(</span>path<span class="synStatement"> . rest</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>decompose-path script-file<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synConstant">&#34;.&#34;</span> path<span class="synSpecial">)</span>
#t
#f<span class="synSpecial">)))</span>
<span class="synComment">;; スクリプトファイルの存在するディレクトリでスクリプトを起動された場合、</span>
<span class="synComment">;; カレントディレクトリがスクリプトが存在するディレクトリ</span>
<span class="synComment">;; スクリプトファイルの存在するディレクトリ以外の場所で起動した場合、</span>
<span class="synComment">;; (car *argv*)のディレクトリ部分だけを取得する</span>
<span class="synSpecial">(</span>define <span class="synSpecial">(</span>main <span class="synType">*argv*</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">if</span> <span class="synSpecial">(</span>is-executed-at-script-directory? <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synType">*argv*</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span>display <span class="synSpecial">(</span>current-directory<span class="synSpecial">))</span>
<span class="synSpecial">(</span>receive <span class="synSpecial">(</span>path<span class="synStatement"> . rest</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>decompose-path <span class="synSpecial">(</span><span class="synStatement">car</span> <span class="synType">*argv*</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span>display path<span class="synSpecial">))))</span>
</pre>
<h4>
実行例
</h4>
<p>
こんな感じです:
</p>
<pre class="syntax-highlight">
kazu634@kazu634% ll ~/junk <span class="synStatement">[</span><span class="synConstant">3503</span><span class="synStatement">]</span>
total <span class="synConstant">96</span>
-rwxr-xr-x <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.4K <span class="synConstant">8</span> <span class="synConstant">21</span> <span class="synConstant">22</span>:<span class="synConstant">49</span> <span class="synConstant">2010-08-21-120641</span>.scm
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff 975B <span class="synConstant">8</span> <span class="synConstant">21</span> <span class="synConstant">13</span>:<span class="synConstant">43</span> <span class="synConstant">2010-08-21-134109</span>.el
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.1K <span class="synConstant">8</span> <span class="synConstant">21</span> <span class="synConstant">13</span>:<span class="synConstant">43</span> <span class="synConstant">2010-08-21-134109</span>.elc
-rwxr-xr-x <span class="synConstant">1</span> kazu634 staff 829B <span class="synConstant">8</span> <span class="synConstant">22</span> <span class="synConstant">16</span>:<span class="synConstant">15</span> <span class="synConstant">2010-08-22-130237</span>.sh
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff 977B <span class="synConstant">8</span> <span class="synConstant">29</span> <span class="synConstant">21</span>:<span class="synConstant">05</span> <span class="synConstant">2010-08-29-202329</span>.scm
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff 671B <span class="synConstant">9</span> <span class="synConstant">3</span> <span class="synConstant">22</span>:<span class="synConstant">58</span> <span class="synConstant">2010-09-01-231839</span>.scm
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.0K <span class="synConstant">9</span> <span class="synConstant">3</span> <span class="synConstant">23</span>:<span class="synConstant">21</span> <span class="synConstant">2010-09-03-232020</span>.txt
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.3K <span class="synConstant">9</span> <span class="synConstant">5</span> <span class="synConstant">22</span>:<span class="synConstant">48</span> <span class="synConstant">2010-09-05-212342</span>.scm
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff 84B <span class="synConstant">9</span> <span class="synConstant">11</span> <span class="synConstant">22</span>:<span class="synConstant">23</span> <span class="synConstant">2010-09-11-222030</span>.txt
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.0K <span class="synConstant">10</span> <span class="synConstant">24</span> <span class="synConstant">15</span>:<span class="synConstant">24</span> <span class="synConstant">2010-09-12-224720</span>.scm
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff <span class="synConstant">3</span>.6K <span class="synConstant">9</span> <span class="synConstant">30</span> <span class="synConstant">23</span>:<span class="synConstant">15</span> <span class="synConstant">2010-09-16-201912</span>.scm
-rwxr-xr-x <span class="synConstant">1</span> kazu634 staff <span class="synConstant">1</span>.0K <span class="synConstant">11</span> <span class="synConstant">3</span> <span class="synConstant">22</span>:<span class="synConstant">45</span> <span class="synConstant">2010-11-01-225103</span>.scm
drwxr-xr-x <span class="synConstant">5</span> kazu634 staff 170B <span class="synConstant">9</span> <span class="synConstant">16</span> <span class="synConstant">20</span>:<span class="synConstant">10</span> <span class="synStatement">source</span>
drwxr-xr-x <span class="synConstant">4</span> kazu634 staff 136B <span class="synConstant">9</span> <span class="synConstant">16</span> <span class="synConstant">20</span>:<span class="synConstant">57</span> <span class="synStatement">test</span>
-rw-<span class="synStatement">r</span>--<span class="synStatement">r</span>-- <span class="synConstant">1</span> kazu634 staff 0B <span class="synConstant">11</span> <span class="synConstant">3</span> <span class="synConstant">22</span>:<span class="synConstant">45</span> <span class="synStatement">test</span>.log
kazu634@kazu634% gosh <span class="synConstant">2010-11-01-225103</span>.scm ~/junk <span class="synStatement">[</span><span class="synConstant">3504</span><span class="synStatement">]</span>
/Users/kazu634/junk%
kazu634@kazu634% <span class="synStatement">cd</span> .. ~/junk <span class="synStatement">[</span><span class="synConstant">3505</span><span class="synStatement">]</span>
kazu634@kazu634% gosh ~/junk/<span class="synConstant">2010-11-01-225103</span>.scm ~ <span class="synStatement">[</span><span class="synConstant">3506</span><span class="synStatement">]</span>
/Users/kazu634/junk%
</pre>
</div>