LoginSignup
0
1

More than 1 year has passed since last update.

shell についてのメモ

Last updated at Posted at 2020-05-30

online でいろいろ試せるサイト(シェルだけじゃない)

https://paiza.io/ja

PS1設定(例)

export PS1='[${PIPESTATUS[@]}] \$ '
or
export PS1='[\w][${PIPESTATUS[@]}] \$ '

コマンドの実施結果がわかるようになる。パイプも。

[0] $ cls
-bash: cls: command not found
[127] $ pwd
/usr
[0] $ cat index.html | grep NG
[0 1] $

shebang シバン

###### 一般的
#!/bin/bash
###### 推奨
#!/usr/bin/env bash -Ceuo pipefail

set -C : リダイレクトによるファイル上書き事故を予防します。
set -e : スクリプトの実行中にエラーが発生すると、そこでスクリプトが終了するようになります。
set -u : 未定義の変数を使用すると、そこでスクリプトが終了するようになります。
set -o pipefail : パイプの途中でエラーが起きた場合もエラーが発生するようになります。

例えばデバッグの為に一時的に -x オプション(実行されたコマンドを表示)を付けて実行したいからといって

bash -x ./hoge.sh

などと実行すると -x は適用されますが -Ceuo pipefail に関しては適用されないということです。
確実に設定したい場合はシバンの直後あたりで set コマンドで設定する方がいいでしょう。

#!/usr/bin/env bash
set -Ceuo pipefail

パラメーター数チェック

if [[ $# != 2 ]]; then
    echo "Parameter incorrect."
    exit 1
fi

自分のフォルダを取得

script_dir=$(cd $(dirname $0) && pwd)
or
script_dir=$(dirname $(readlink -f $0 ))

ファイルについて存在チェック

if [[ ! -f /tmp/foo.txt ]]; then
    echo "File not found!"
fi

-b filename - 阻止特殊文件
-c filename - 特殊字符文件
-d directoryname - 检查目录是否存在
-e filename - 检查文件是否存在,与类型(节点,目录,套接字等)无关
-f filename - 检查常规文件是否存在而不是目录
-G filename - 检查文件是否存在并由有效的组ID拥有
-G filename set-group-id - 如果文件存在且为set-group-id,则为true
-k filename - 粘性位
-L filename - 符号链接
-O filename - 如果文件存在且由有效用户ID拥有,则为True
-r filename - 检查文件是否可读
-S filename - 检查文件是否为套接字
-s filename - 检查文件是否为非零大小
-u filename - 检查是否设置了文件set-user-id位
-w filename - 检查文件是否可写
-x filename - 检查文件是否可执行

-b filename - Block special file
-c filename - Special character file
-d directoryname - Check for directory Existence
-e filename - Check for file existence, regardless of type (node, directory, socket, etc.)
-f filename - Check for regular file existence not a directory
-G filename - Check if file exists and is owned by effective group ID
-G filename set-group-id - True if file exists and is set-group-id
-k filename - Sticky bit
-L filename - Symbolic link
-O filename - True if file exists and is owned by the effective user id
-r filename - Check if file is a readable
-S filename - Check if file is socket
-s filename - Check if file is nonzero size
-u filename - Check if file set-user-id bit is set
-w filename - Check if file is writable
-x filename - Check if file is executable

色付きメッセージ

#!/usr/bin/env bash
set -Ceuo pipefail

function cecho() {
    local color_name color
    readonly color_name="$1"
    shift
    case $color_name in
        red)    color=31 ;;
        green)  color=32 ;;
        yellow) color=33 ;;
        blue)   color=34 ;;
        cyan)   color=36 ;;
        *)      error_exit "An undefined color was specified." ;;
    esac
    printf "\033[${color}m%b\033[m\n" "$*"
}

cecho red "[ERROR]"
cecho green "[DEBUG]"
echo "Normal"

参考になるサイト

Bash の if 文(test文)のオプションを整理してみた
https://qiita.com/wakayama-y/items/a9b7380263da77e51711
シェルスクリプト(bash)のif文やwhile文で使う演算子について
https://qiita.com/egawa_kun/items/196cd354c0d8e4e0fefc
Bashにおける括弧類の意味
https://qiita.com/yohm/items/3527d517768402efbcb6
シェルスクリプトでの == を認めているのはbashだけ?
https://qiita.com/yu81/items/b1bde03c06b45f7d267f
《Bash 脚本教程》
http://www.ruanyifeng.com/blog/2020/04/bash-tutorial.html

日本語
https://sousaku-memo.net/php-system/1817

中国語
https://www.jianshu.com/p/422a9467f97b
https://www.jianshu.com/p/1a96d0d9da37
https://www.jianshu.com/p/b00ab0399e92
https://www.jianshu.com/p/767c6f79b8d8

以上

0
1
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
1