LoginSignup
0
0

More than 5 years have passed since last update.

Slackware の init 向けに TUI の起動項目編集ツールを書く話

Last updated at Posted at 2019-06-19

当方執筆の技術情報に関するライセンス、免責事項、禁止事項


Slackware における rc スクリプトの有効化と無効化

The Slackware Linux Project: Configuration Help の init の節より

Slackware Linux uses the BSD-style file layout for its system initialization files. These files are organized and easy to edit. All of the system initialization files are stored in the /etc/rc.d directory. To prevent a script from executing at startup you can remove the execute permission on the file and Slackware will not execute it.

/etc/rc.d 内のスクリプトの内、実行させたいものに実行権限を与え、実行させたくないものから実行権限を奪えば良い。とてもシンプルです。

アルゴリズムを考える

↓だいたいこんな感じ。

やらせたいこと

  1. /etc/rc.d/ 内のファイルをリストアップする
  2. 実行権限がついているものをチェックする
  3. 実行権限の付与と剥奪について対話的にユーザーに選ばせる
  4. 変更を適用する

書いてみた

プログラミングに全く馴染みがない、老境にある両親にスクリプト書きを手伝わせるべく試行錯誤した結果、関数名を自然言語っぽくする発想に至る。

※ cf. エドガー・ダイクストラが綴った「”自然言語プログラミング”の愚かしさについて」
※ リリース時には普通の ShellScript の書き方に戻します。

名付けて agate-init-config

"agate"は何となく。

最終的に関数はパイプラインで繋がるように仕上げる予定。

あとはとりあえず、
・ashやdashで動くものを作る。
・変数と一時ファイルは最小限に。
・引数を活用しハードコードを避ける。
・ローカル変数は使ったら必ずunset。
・インデントはハードタブで。
・ファイルの編集が伴う操作を関数外へ。
・不必要な関数、珍妙な関数は全部廃止。
・悪ふざけ部分の切除。


#!/bin/sh
STARTING_TIME="$(date +%s)_$(date +%F)"
MESSAGE_TITLE="/etc/rc.d/ MODIFICATION TOOL"
MESSAGE_BODY="Select files to be executable with using spacebar."
#HIGHT=15
#WIDETH=65
#LINES=7

caution_run_as_root () {
  echo "Run as root, bye."
  exit
}

check_which_file_in_a_directory_is_executable () {
  find . | sed -e "s/\.\///g" -e 1d | while read line
    do
      if [ -x "$line" ]; then
        echo "$line" found on
      else
        echo "$line" found off
      fi
    done | tr "\n" " "
  unset line
}

ask_whether_files_to_be_executable () {
  dialog --title "$MESSAGE_TITLE" --checklist "$MESSAGE_BODY" 15 65 7 $EXECUTABLE
}

add_a_space_to_the_end () {
  echo | tr "\n" " "
}

replace_a_space_to_a_linefeed () {
  tr " " "\n" < "$1"
}

add_the_execute_permission_to () {
  chmod +x "$1"
}

remove_the_execute_permission_from () {
  chmod -x "$1"
}

remove_the_read_permission_of_group_and_other_from () {
  chmod go-r "$1"
}

change_working_directory_to () {
  cd "$1"
}

create_the_directory () {
  mkdir "$1"
}

change_working_directory_to /root || caution_run_as_root

if [ ! -d /root/.agateinitcfg ]; then
  create_the_directory /root/.agateinitcfg
fi

change_working_directory_to /etc/rc.d
EXECUTABLE=$(check_which_file_in_a_directory_is_executable)
echo "$EXECUTABLE" > \
  /root/.agateinitcfg/"$STARTING_TIME"_executable_files_before_modification

change_working_directory_to /root
ask_whether_files_to_be_executable 2> \
  /var/tmp/agateinitcfg_selection_"$STARTING_TIME"
proceed=$?
echo $proceed
add_a_space_to_the_end >> /var/tmp/agateinitcfg_selection_"$STARTING_TIME"
remove_the_read_permission_of_group_and_other_from \
  /var/tmp/agateinitcfg_selection_"$STARTING_TIME"

if [ $proceed = 0 ]; then
  change_working_directory_to /etc/rc.d || exit
  remove_the_execute_permission_from ./*

  replace_a_space_to_a_linefeed /var/tmp/agateinitcfg_selection_"$STARTING_TIME" |
  while read line
    do
      add_the_execute_permission_to ./"$line"
    done
  unset line
  check_which_file_in_a_directory_is_executable > \
    /root/.agateinitcfg/"$STARTING_TIME"_executable_files_after_modification
  change_working_directory_to /root
fi

スクリーンショット

スクリーンショット_2019-06-19_19-51-07.png

ポエム

Slackware ゆるくて大好き!

あと、

魂の駆動体読むぞー!

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