用 ipython 取代 shell script

142 views
Skip to first unread message

Albert Chun-Chieh Huang

unread,
Feb 7, 2013, 3:01:56 AM2/7/13
to pyth...@googlegroups.com
Hi, all,

最近開始嘗試使用 ipython 了, 因為我是要取代 shell script 用的, 我要怎麼
樣能夠把我 ipython shell 裡面打的命令, 逐字不漏的變成一個 shell script,
並且可以用 CLI 呼叫呢?

我用 ipthon test.py 並不會得到上述的結果, 我缺了什麼? 謝謝!
--
Albert Chun-Chieh Huang(黃俊傑)
Blog: Random Notes, http://alberthuang314.blogspot.com/

LittleQ

unread,
Feb 7, 2013, 3:13:57 AM2/7/13
to pyth...@googlegroups.com
在檔案的行頭加上

#!/usr/bin/env ipython
(ipython code)

然後直接執行

./your-ipython-script


不過照理講 ipython file.py 應該也會有同樣效果,所以可能要檢查一下程式:)

2013/2/7 Albert Chun-Chieh Huang <alberth...@gmail.com>
--
您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這封郵件通知您。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com
如要在此群組張貼留言,請傳送電子郵件至 pyth...@googlegroups.com
請前往以下網址造訪這個群組:http://groups.google.com/group/pythontw?hl=zh-TW
如需更多選項,請前往:https://groups.google.com/groups/opt_out





--
{
    greeting"Have a nice day!",
    name"Colin Su",
    nickname"LittleQ",
    email"littl...@gmail.com",
    website"http://about.me/littleq",
    title: [
        ["Genie Capital""Web Developer"],
        ["National Chengchi University""Student""Computer Science Dept."]
    ]
}

Albert Chun-Chieh Huang

unread,
Feb 7, 2013, 3:49:24 AM2/7/13
to pyth...@googlegroups.com
Hi,

哈,剛剛終於找到正解了,連 stackoverflow 都充滿了不一定正確的解答(有時
效性)。我這裡用的是 ipython 0.13

假設我們在 ipython shell 裡面鍵入了以下的命令:

==== 內容開始 ====
print 'Test script for ipython';

v = !ls;

print v;

==== 內容結束 ====


這個是我們在用 ipython 的時候能夠直接鍵入的命令, 基本上要「逐字不漏的執行」
就存成 test.ipy, 然後在 shell 執行:

$ ipython test.ipy

就搞定了。

LittleQ 的方法也必須要存成 .ipy 才能動作喔!


Albert

LittleQ

unread,
Feb 7, 2013, 3:54:27 AM2/7/13
to pyth...@googlegroups.com
屙,可是我不用打成 ipy 捏.....

可能是版本不同吧,不過一般來說跟副檔名沒什麼關係。

直接執行的時候如果有在開頭加一行 #!/usr/bin/env ipython

執行的時候前面就不用加 ipython

只要打 ./your-python-script.py 就好。

LittleQ

unread,
Feb 7, 2013, 3:57:41 AM2/7/13
to pyth...@googlegroups.com
阿,你說的是對的,因爲我之前都沒用 ipython 的 ! 指令。

如果附檔名是 py 他會當作 python 來執行
是 ipy 就會變成 ipython script

受教了 XD

2013/2/7 LittleQ <littl...@gmail.com>

Yung-Yu Chen

unread,
Feb 7, 2013, 5:18:39 AM2/7/13
to pyth...@googlegroups.com
2013/2/7 LittleQ <littl...@gmail.com>
阿,你說的是對的,因爲我之前都沒用 ipython 的 ! 指令。


正名一下,這個叫 shebang。

yyc



--
Yung-Yu Chen
http://solvcon.net/yyc/
+886 (99) 129 4763

Chang-Ching Yeh

unread,
Feb 7, 2013, 10:05:38 AM2/7/13
to pyth...@googlegroups.com
原來有名字,長知識了!

-- 
Best regards,
Jim Yeh (Chang-Ching Yeh)
Sent with Sparrow

pingooo

unread,
Feb 7, 2013, 7:23:12 PM2/7/13
to pythontw
補充一下,shebang 不是 python 專有的機制,是 un*x 都有的一般機制。



2013/2/7 Chang-Ching Yeh <lemon...@gmail.com>

Albert Chun-Chieh Huang

unread,
Feb 7, 2013, 11:40:47 PM2/7/13
to pyth...@googlegroups.com

pingooo 提到 Unix 都有的一般機制這句話引起我的興趣了, 畢竟我大學當過
Unix administrator, 雖然遠離 Unix admin 很久很久了。

我去查了一下, shebang 指的是 shell script 的第一行開頭的 #!, 用來呼叫
shell 或是其他程式, c.f. http://en.wikipedia.org/wiki/Shebang_(Unix).

至於 exclamation mark(!) 叫做什麼, 用作呼叫 shell command 的用途我就沒查
到了, 不知道有沒有專有名詞?


Albert


pingooo <ping.n...@gmail.com> writes:

> 補充一下,shebang 不是 python 專有的機制,是 un*x 都有的一般機制。
>
>
>
> 2013/2/7 Chang-Ching Yeh <lemon...@gmail.com>
>
>> 原來有名字,長知識了!
>>
>> --
>> Best regards,
>> Jim Yeh (Chang-Ching Yeh)
>> Sent with Sparrow <http://www.sparrowmailapp.com/?sig>

Yung-Yu Chen

unread,
Feb 8, 2013, 2:18:45 AM2/8/13
to pyth...@googlegroups.com
2013/2/8 Albert Chun-Chieh Huang <alberth...@gmail.com>

pingooo 提到 Unix 都有的一般機制這句話引起我的興趣了, 畢竟我大學當過
Unix administrator, 雖然遠離 Unix admin 很久很久了。

我去查了一下, shebang 指的是 shell script 的第一行開頭的 #!, 用來呼叫
shell 或是其他程式, c.f. http://en.wikipedia.org/wiki/Shebang_(Unix).


就是在講這個呀。

yyc

Buganini

unread,
Feb 8, 2013, 2:40:26 AM2/8/13
to pyth...@googlegroups.com
以上話題除了說#!之外,應該還有一個是說像linux/freebsd的ftp指令連到某處之後,ls是list remote files,!ls是list local file(在local shell執行ls)這樣的東西?

lloyd huang

unread,
Feb 8, 2013, 10:29:48 AM2/8/13
to pyth...@googlegroups.com
我對 #! 的印象來自 UNIX FAQ

http://netlab.cse.yzu.edu.tw/~statue/freebsd/docs/unixfaq/node41.html

Best Regards
Lloyd

2013/2/8 Albert Chun-Chieh Huang <alberth...@gmail.com>

Albert Chun-Chieh Huang

unread,
Feb 8, 2013, 11:57:05 AM2/8/13
to pyth...@googlegroups.com
Yung-Yu Chen <y...@solvcon.net> writes:

> 2013/2/7 LittleQ <littl...@gmail.com>
>
>> 阿,你說的是對的,因爲我之前都沒用 ipython 的 ! 指令。
>>
>>
> 正名一下,這個叫 shebang。
>
> yyc

yyc, 你用 inline replying 上面只有 LittleQ 講的「 ! 指令」, 很容易引起誤
會吧?萬一看的人沒有繼續查證, 就會以為 shebang 是 "!" 指令啊。
> >> 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我
> 們特別傳送這封郵件通知您。
> >> 如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳
> 送電子郵件到 pythontw+u...@googlegroups.com
> >> 如要在此群組張貼留言,請傳送電子郵件至
> pyth...@googlegroups.com
> >> 請前往以下網址造訪這個群組:
> http://groups.google.com/group/pythontw?hl=zh-TW
> >> 如需更多選項,請前往:
> https://groups.google.com/groups/opt_out
> >>
> >>
> >>
> >
> >
> > --
> > {
> >     greeting: "Have a nice day!",
> >     name: "Colin Su",
> >     nickname: "LittleQ",
> >     email: "littl...@gmail.com",
> >     website: "http://about.me/littleq",
> >     title: [
> >         ["Genie Capital", "Web Developer"],
> >         ["National Chengchi University", "Student",
> "Computer Science Dept."
> > ]
> >     ]
> > }
> >
> > --
> > 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們
> 特別傳送這封郵件通知您。
> > 如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送
> 電子郵件到 pythontw+u...@googlegroups.com
> > 如要在此群組張貼留言,請傳送電子郵件至
> pyth...@googlegroups.com
> > 請前往以下網址造訪這個群組:
> http://groups.google.com/group/pythontw?hl=zh-TW
> > 如需更多選項,請前往:
> https://groups.google.com/groups/opt_out
> >
> >
> > 在檔案的行頭加上
> >
> >
> > #!/usr/bin/env ipython
> > (ipython code)
> >
> > 然後直接執行
> >
> > ./your-ipython-script
> >
> >
> > 不過照理講 ipython file.py 應該也會有同樣效果,所以可能
> 要檢查一下程式:)
> >
> > 2013/2/7 Albert Chun-Chieh Huang
> <alberth...@gmail.com>
> >
> >     Hi, all,
> >
> >     最近開始嘗試使用 ipython 了, 因為我是要取代 shell
> script 用的, 我
> >     要怎麼
> >     樣能夠把我 ipython shell 裡面打的命令, 逐字不漏的變
> 成一個 shell
> >     script,
> >     並且可以用 CLI 呼叫呢?
> >
> >     我用 ipthon test.py 並不會得到上述的結果, 我缺了什麼
> ? 謝謝!
> >     --
> >     Albert Chun-Chieh Huang(黃俊傑)
> >     Blog: Random Notes,
> http://alberthuang314.blogspot.com/
> >
> >     --
> >     您已訂閱「Google 網上論壇」的「python.tw」群組,因此
> 我們特別傳送這
> >     封郵件通知您。
> >     如要取消訂閱這個群組並停止接收來自這個群組的郵件,請
> 傳送電子郵件到
> >     pythontw+u...@googlegroups.com
> >     如要在此群組張貼留言,請傳送電子郵件至
> pyth...@googlegroups.com
> >     請前往以下網址造訪這個群組:
> >     http://groups.google.com/group/pythontw?hl=zh-TW
> >     如需更多選項,請前往:
> https://groups.google.com/groups/opt_out
> >
> >
> >
> >
> >
> >
> > --
> > {
> >     greeting: "Have a nice day!",
> >     name: "Colin Su",
> >     nickname: "LittleQ",
> >     email: "littl...@gmail.com",
> >     website: "http://about.me/littleq",
> >     title: [
> >         ["Genie Capital", "Web Developer"],
> >         ["National Chengchi University", "Student",
> "Computer Science
> > Dept."]
> >     ]
> > }
>
> --
> Albert Chun-Chieh Huang(黃俊傑)
> Blog: Random Notes, http://alberthuang314.blogspot.com/
>
> --
> 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特
> 別傳送這封郵件通知您。
> 如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電
> 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這
> 封郵件通知您。
> 如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到
> pythontw+u...@googlegroups.com
> 如要在此群組張貼留言,請傳送電子郵件至 pyth...@googlegroups.com
> 請前往以下網址造訪這個群組:
> http://groups.google.com/group/pythontw?hl=zh-TW
> 如需更多選項,請前往:https://groups.google.com/groups/opt_out
>  
>  
>
>
>
>
> --
> Yung-Yu Chen
> http://solvcon.net/yyc/
> +886 (99) 129 4763

Yung-Yu Chen

unread,
Feb 8, 2013, 4:56:31 PM2/8/13
to pyth...@googlegroups.com
2013/2/9 Albert Chun-Chieh Huang <alberth...@gmail.com>
Yung-Yu Chen <y...@solvcon.net> writes:

> 2013/2/7 LittleQ <littl...@gmail.com>
>
>> 阿,你說的是對的,因爲我之前都沒用 ipython 的 ! 指令。
>>
>>
> 正名一下,這個叫 shebang。
>
> yyc

yyc, 你用 inline replying 上面只有 LittleQ 講的「 ! 指令」, 很容易引起誤
會吧?萬一看的人沒有繼續查證, 就會以為 shebang 是 "!" 指令啊。


似乎是我造成誤會了。我的錯,抱歉。

從頭到尾我都以為 LittleQ 在講 shebang。經 Albert 提醒才發現後來 LittleQ 是在講另一個東西。

再來正名一下。一般 Un*x script 檔頭的 "#! blah"  叫作 shebang。ipython "!" 開頭的指令是另外的東西,效果上來講是對 Python 環境的脫逸 (escape) 字元,會跳去執行系統裡的可執行檔。

yyc-1
Reply all
Reply to author
Forward
0 new messages