selmertsxの素振り日記

ひたすら日々の素振り内容を書き続けるだけの日記

ふつうのLinuxプログラミング(4章)

学べること

ディレクトリのパーミッションの挙動は下記の通り。

  • read => ファイル一覧が取れる
  • write => ディレクトリの中にファイルを作れる
  • execute => そもそもファイルにアクセスできない

僕達がコマンドラインとして認識しているのは、shellとterminalという2つの構成要素から成る。端末はファイルとして表現される。

※ 端末については、こいつをファイルと見立てて入出力のストリームを作成することで、コンピューターと人間がやりとりできるようにするもの。というくらいの認識でいる。

以下実験内容

ファイルのパーミッション

普通につくると、パーミッションは実行権限を持たない

$ echo "echo 'hello'" > sample.sh
$ ls -l
total 8
-rw-r--r--  1 shuhei_morioka  admin  13  2 14 21:42 sample.sh
# shell command が sample.sh を読み込んで実行している
$ sh sample.sh
hello
# パイプを使ってストリームを作成し、sh コマンドに渡して実行することも可能
$ cat sample.sh | sh
hello
# 実行権限が無いので実行不可
$ ./sample.sh
zsh: permission denied: ./sample.sh
# sample.sh から読み込み権限を抜くと、shell scriptで実行不可
$ chmod -r sample.sh
$ sh sample.sh
sh: sample.sh: Permission denied
# 実行権限を付与すると、./sample.sh 単体で実行可能
$ chmod +x sample.sh
$ ls -l
total 8
-rwxr-xr-x  1 shuhei_morioka  admin  13  2 14 21:42 sample.sh
$ ./sample.sh
hello

ディレクトリのパーミション

読み込み権限が無いディレクトリの場合

$ mkdir sample_dir
$ ls -l
total 8
-rwxr-xr-x  1 shuhei_morioka  admin  13  2 14 21:42 sample.sh
drwxr-xr-x  2 shuhei_morioka  admin  68  2 14 22:33 sample_dir
$ chmod -x sample_dir
$ ls -l
total 8
-rwxr-xr-x  1 shuhei_morioka  admin  13  2 14 21:42 sample.sh
drw-r--r--  2 shuhei_morioka  admin  68  2 14 22:33 sample_dir
$ cd sample_dir
cd: permission denied: sample_dir

実行権限が無いディレクトリの挙動

➜  permission chmod +x sample_dir
➜  permission touch sample_dir/hoge
➜  permission ls -l sample_dir
total 0
-rw-r--r--  1 shuhei_morioka  admin  0  2 14 22:35 hoge
➜  permission chmod -x sample_dir
➜  permission ls -l
total 8
-rwxr-xr-x  1 shuhei_morioka  admin   13  2 14 21:42 sample.sh
drw-r--r--  3 shuhei_morioka  admin  102  2 14 22:35 sample_dir
➜  permission ls -l sample_dir
ls: hoge: Permission denied

端末の実験

今の端末のファイルを確認する方法

➜  permission tty
/dev/ttys000

別の端末から、/dev/ttys000 に文字列を入力すると

➜  ~ tty
/dev/ttys003
➜  ~ echo "hogehoge\nhogehoge" > /dev/ttys000

/dev/ttys000 の端末に出力される。

# /dev/ttys000 の端末
➜  permission hogehoge
hogehoge