0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

設定変更なしダブルクリックのみでデスクトップエントリを活用しLinuxのシェルスクリプトを実行する

Posted at

テンプレ

デスクトップエントリのテンプレは以下です。

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=name
Name[ja]=名前
Comment=comment
Comment[ja]=コメント
# 何故かTryExecだと失敗する
# TryExec=bash -c '#ここにスクリプト本体を入れる;'
Exec=bash -c '#ここにスクリプト本体を入れる;'
StartupNotify=true
Terminal=false
Type=Application
Icon=アイコン。絶対ディレクトリにしたほうが無難
Categories=GTK;Utility;ここにスクリプトのカテゴリ;
Keywords=カテゴリとか;
# MimeType=

シェルスクリプトで簡単にGUIを使えるライブラリzenityと通知を簡単に作成できるnotify-sendを使ったサンプル

apt install -y zenity libnotify-bin

notify-sendはまず入っていることが多い

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Easy generate notify
Name[ja]=簡易通知作成
Comment=Easy generate notify
Comment[ja]=簡易通知作成
# TryExec=bash -c 'notify_message=$(zenity --text-info --editable --title メッセージ) && notify-send $notify_message'
Exec=bash -c 'notify_message=$(zenity --text-info --editable --title メッセージ) && notify-send $notify_message'
StartupNotify=true
Terminal=false
Type=Application
Icon=絶対ディレクトリにしたほうが無難
Categories=GTK;Utility;generate notify;
Keywords=generate notify;
# MimeType=

動機

よく頻繁に実行するワンライナーなどはシェルスクリプトにまとめたりします。
またLinuxデスクトップを使っているとキー入力1文字すら入力が面倒でダブルクリックでWindowsバッチのようにスクリプトを実行したいことが度々起こります。
よくある.shファイルをダブルクリックで実行する方法の解説はLinux側の設定をいじらなければならず面倒です。また環境依存も少なくないように見えます。
しかもメンテすら不要なワンライナーのような短いコードでわざわざ設定を変えるのは面倒です。
それにスクリプトのコード部分とデスクトップエントリを分割するのはファイルがあっちこっちに移動して面倒です。
そこで1つのファイルで完結して設定を変えることなくWindowsのバッチファイルのようにダブルクリックでシェルスクリプトを実行する方法を調べました。
意外とこれが記事になってるようで見つけられなかった。

中身の解説

Execでコマンドを呼び出すのですがパイプなどが直接だと機能しないようなのでbash -cで子に入れて呼び出すことで機能するようにしました。
あとはワンライナーの要領でコードを書いていくだけです。
メンテナンス性は良くないので注意してください。

###参考文献
https://mattintosh.hatenablog.com/entry/20141006/1412550000
https://stackoverflow.com/questions/22477569/desktop-shortcut-doesnt-work-if-use-pipe-in-command

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?