ヒストリをとるための

仕事でunixをよく使うので、使ったコマンドをファイルに落とすためのスクリプトを書いた。久々にpython 2.Xで。

aliasに

alias his = "history|tail -2 |head -1|tee -a"
his his

とかで一つ前の入力hisに追加していってもいいんだけど、つい勢いでlsとかpwdとかしようものならとれなくなるので、入力にコマンドを与えると直近のそのコマンドを落としてくれたり、引数で何個としていするとその分とってきてくれるようなのを書いた。
課題はまだまだあるけど、とりあえずアップ。
pythonソースコードのアップとか久しぶりだよ、いつぶりだよ...)

#!/usr/bin/python
#get_his.py

import sys
import getopt
import re

def help():
       sys.stderr.write("unix> python get_his.py\n")
       sys.stderr.write("      options: -c <unix command> : a command to use as a history.\n")
       sys.stderr.write("               -n <int> : <int > used recently.\n")
       sys.stderr.write("               -s <word> : searching <word> and output.\n")
       sys.stderr.write("      PRIORITY: -s > -c\n")
       sys.stderr.write("      ### OSUSUME: alias his ='./get_his.py \!* |tee -a $!$'\n")
       sys.exit()

if __name__=='__main__':
       try:
               opts,args=getopt.getopt(sys.argv[1:], "hc:n:r:s:", ["help", "command=", "num=", "recently", "seach="])
       except getopt.GetoptError:
               sys.stderr.write("Invalid option.\n")
               help()

       num=0
       command=""
       recently=""
       search=""
       for o,a in opts:
               if o in ("-h","--help"):
                       help()
               if o in ("-c", "-command"):
                       command=a
               if o in ("-n","--num"):
                       recently=int(a)+1
               if o in ("-s", "--search"):
                       search=a

       totallineno=0
       line=[]
       for stdin in sys.stdin:
               line.append(stdin.rstrip())
       line.reverse()
       line.pop(0)

       if search != "":
               num_com=1
               for i in range(len(line)):
                       if re.search(search, line[i]):
                               print line[i]
                               num_com+=1
                               if recently == "":
                                       break
                               elif num_com == recently:
                                       break
       elif command != "" :
               num_com=1
               for i in range(len(line)):
                       item=line[i].split()
                       if command==item[2]:
                               print line[i]
                               num_com+=1
                               if recently == "":
                                       break
                               elif num_com == recently:
                                       break
       elif recently != "":
               for i in range(1,recently):
                       print line[i]
       else :
               print line[num]
alias his = "history|python get_his.py \!*|tee -a \$"

aliasに上記を追加。
unixコマンドの最後の引数のファイル名でダンプするので、his -n 10とかしたら、10というファイル名にヒストリが保存されることになるのが不満。

his -c ln -n 10 his

で、直近のlnコマンドを10個hisファイルに落としたりとか、

his -s hoge -n 10 his

で、直近のhogeを含むヒストリをhisファイルに落とすとかできる。

機能的な課題は前述の通り、最後の引数のファイル名に落とすので、ミスタイプすると変なファイル名に落としてしまったりしてしまう点。
コーディング的には、なんか階層が深い気がする。書き方が冗長。getoptよりも良いモジュールが最近の流行(らしい?)ので、流行についていけてない。などなど挙げたらキリがない。

そんな感じで、今年ももうすぐ半年が過ぎようとしてるけど、当初の宣言通り、まだpyhonは使えている様子。