Skip to content

Instantly share code, notes, and snippets.

@maeharin
Last active September 18, 2018 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maeharin/be8ef41286f796cad08b to your computer and use it in GitHub Desktop.
Save maeharin/be8ef41286f796cad08b to your computer and use it in GitHub Desktop.
[sh]シェルスクリプトお手本

[sh]シェルスクリプト

条件演算子

$ man test

参考:http://shellscript.sunone.me/if_and_test.html

よく使うlinuxコマンド

du

# 配下のディレクトリ容量調べる
$ du -d 1 | sort -n

# 人間が読みやすくするには("Human-readable")
$ du -h

sort

# フィールドを指定して(-k)数値として(-n)ソート
# http://d.hatena.ne.jp/a_bicky/20110724/1311497192
$ ls -laF | sort -k 5 -n

comm

# 2つのファイルの差異を検出(sortでユニークした後に)
# 結果は、a.txtにしかない | 両方にある | b.txtにしかない
$ comm <(sort -u a.txt) <(sort -u b.txt)

zip

# ディレクトリをパスワードつきzip
$ zip -e -r hoge.zip hoge/

sed

awk

オプションパース

複数のオプションなら、getopts

while getopts ":fhpcnNaAtTvVx+@Pd:" Option
do
  case $Option in
    '@' )

$#をループしてshiftもあり

while [ $# -gt 0 ];
do
    case ${1} in

        --debug|-d)
            set -x
        ;;

        --host|-h)
            HOST=${2}
            shift
        ;;

        --port|-p)
            PORT=${2}
            shift
        ;;

        *)
            echo "[ERROR] Invalid option '${1}'"
            usage
            exit 1
        ;;
    esac
    shift
done

1つのオプションなら$1から

case $1 in
    install)
        check_dependencies
        install_authy "$0" "$2"
        ;;
    update)
        check_dependencies
        require_root
        update_authy
        ;;

2つのオプションなら$1と$2決めうちもあり

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment