Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2914

linux: オリジナルのTerminal コマンドを作成

$
0
0

結論のみ

command.shを作成して、functionを記述
.bashrcを開いて、source ${path-to-command.sh}/command.shを追記
以下、詳細

1 基本

terminal
# terminal を開く# 以下入力(shift + Enterで改行)
user@pop-os:~$ function test(){>echo$1>}# 定義した関数を呼び出し
user@pop-os:~$ test"hello bash script"
hello bash script

簡単。でも、一度terminalを閉じると、もう使えなくなる

2 関数をファイルに保存

terminal
# rotate.sh を作る
user@pop-os:~$ mkdir commands
user@pop-os:~$ vim commands/rotate.sh
rotate.sh
#!/bin/bash# rotate screenfunction rotate(){case$1in# left[lL])
        xinput set-prop 13 'Coordinate Transformation Matrix' 0 -1 0 1 0 0 0 0 1
        xrandr -o left
    ;;# right[rR])
    xinput set-prop 13 'Coordinate Transformation Matrix' 0 1 0 -1 0 0 0 0 1
    xrandr -o right
    ;;# upside down[bBdD])
    xinput set-prop 13 'Coordinate Transformation Matrix'-1 0 0 0 -1 0 0 0 1
    xrandr -o inverted 
    ;;# normal*)
        xinput set-prop 13 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
        xrandr -o normal
    ;;esac}

#はコメント。
$1 は第一引数。
case は、switch文で、引数の値が、続く正規表現に一致するときだけ実行。
[lL]は正規表現で、小文字のlか大文字のLかどちらか。
)はcase文の1ケースであることを示す。
;;はbreak。
esacはcaseの終わりタグ。
xinput set-propとxrandrは、画面を回転させて、マウスの入力方向を回転させている。詳しくは、別記事参照。

これだけでは、当然関数は使えないので、bashに関数を読み込む必要がある

terminal
user@pop-os:~$ rotate
rotate: command not found

3 bashに作成した関数を読み込ませる

terminal
# bashに関数を追加
user@pop-os:~$ source ~/commands/rotate.sh
# 関数を実行
user@pop-os:~$ rotate l

簡単。ターミナルを閉じると、またsourceを実行しないといけない。そこで、.bashrc(bash run control) に、関数を呼び込む処理を書く必要がある

4 .bashrc (bash run control) に、作成した関数を追加

terminal
user@pop-os:~ sudo vim ~/.bashrc 

一番下の行に以下を追加

.bashrc
# オリジナルのコマンドを読み込みsource ~/command/*.sh

これで、未来永劫、コマンドが使えるようになる

terminal
user@pop-os:~ rotate r

Viewing all articles
Browse latest Browse all 2914

Trending Articles