60歳からITエンジニアを目指す無謀なブログ

60歳で定年し、職種を変更してIT技術者を目指すブログです。

bash シェルスクリプト を少し

ー--------------.bashrc file-------------------------------------------

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
if ! "$PATH" =~ "$HOME/.local/bin:$HOME/bin:"
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

ー--------------.bashrc file-------------------------------------------

上の.bashrcの意味

if [ -f /etc/bashrc ];  <<- /etc/bashrc が普通のファイルなら

. /etc/bashrc <<- /etc/bashをsourceする。.(ドット)はsourceと同じ意味

         現在のシェルに実行環境を引き継ぐ

  "$PATH" =~ "$HOME/.local/bin:$HOME/bin:"  <<- 比較して一致するか?

先頭に!がついているので否定となり、

$PATHが$HOME/.local/bin:$HOME/binと一致しないなら、

PATH="$HOME/.local/bin:$HOME/bin:$PATH"を実行する

[  ]テストコマンドで 中にスペースが必要で、posix正規表現で変数、文字列に”クォート必要

bash特有でクォート不要、テストコマンドではない、式を複数入れて評価できる

 

 

 

オプション 使用例 オプションの意味
-z test -z string string の文字列長が 0 ならば真となる。
-n test -n string string の文字列長が 0 より大ならば真となる。
-d test -d file file がディレクトリならば真となる。
-f test -f file file が普通のファイルならば真となる。
-s test -s file file が 0 より大きいサイズならば真となる。
-e test -e file file が存在するならば真となる。
-r test -r file file が読み取り可能ならば真となる。
-w test -w file file が書き込み可能ならば真となる。
-x test -x file file が実行可能ならば真となる。

略式 test コマンド

[ 文字列1 = 文字列2 ]
[ 数値1 オプション 数値2 ]
[ オプション 評価対象 ]