Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2912

shell についてのメモ

$
0
0

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

https://paiza.io/ja

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 bashset-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

参考になるサイト
https://sousaku-memo.net/php-system/1817
http://www.ruanyifeng.com/blog/2020/04/bash-tutorial.html


Viewing all articles
Browse latest Browse all 2912

Trending Articles