逆引きシェルスクリプト/if文のサンプル・ファイル・ディレクトリ編
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* if文のサンプルシェルスクリプト・ファイル・ディレクトリ...
bashを利用しif .. then .. fi を利用し、ファイルおよびディ...
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* ディレクトリかどうか調べる [#mc843b66]
ディレクトリかどうかは、''-d''オプションを利用します。
#ref(chk_dir.sh)
以下のサンプルシェルスクリプトは、fooディレクトリを作成し...
途中、''-f''オプションを利用してファイルかどうかを確認し...
次に、fooディレクトリを削除し''!''を利用しfooディレクトリ...
#/bin/bash
mkdir foo
if [ -d foo ]; then
echo "foo directory found."
fi
if [ -f foo ]; then
echo "foo file found."
fi
rmdir foo
if [ ! -d foo ]; then
echo "foo directory not found."
fi
-実行結果
$ chmod +x chk_dir.sh
$ ./chk_dir.sh
foo directory found.
foo directory not found.
実行結果の出力からわかるように、ディレクトリの有無は''-d'...
また、ファイルでは無いので''-f''オプションのif ... fiブロ...
* ファイルでもディレクトリでも存在しているかどうかを調べ...
ファイルでもディレクトリでも存在しているかどうかのチェッ...
以下のサンプルシェルスクリプトは、fooディレクトリとbarフ...
#ref(chk_dir_file.sh)
#/bin/bash
mkdir foo
touch bar
if [ -e foo ]; then
echo "foo found."
if [ -d foo ]; then
echo "foo is directory."
fi
if [ -f foo ]; then
echo "foo is file."
fi
fi
if [ -e bar ]; then
echo "bar found."
if [ -d bar ]; then
echo "bar is directory."
fi
if [ -f bar ]; then
echo "bar is file."
fi
fi
rmdir foo
rm bar
- 実行結果
$ chmod +x chk_dir_file.sh
$ ./chk_dir_file.sh
foo found.
foo is directory.
bar found.
bar is file.
* ファイルが存在しているかどうかを確認する [#z3143d75]
上記の例で既に記述していますが、''-f''オプションによりフ...
以下のサンプルシェルスクリプトファイルは、fooファイルを作...
途中、''-d''オプションを利用してディレクトリかどうかを確...
次に、fooファイルを削除し''!''を利用しfooファイルが存在し...
#ref(chk_file.sh)
#/bin/bash
touch foo
if [ -f foo ]; then
echo "foo file found."
fi
if [ -d foo ]; then
echo "foo directory found."
fi
rm foo
if [ ! -f foo ]; then
echo "foo file not found."
fi
* 関連記事 [#f8f90509]
- [[if文のサンプルシェルスクリプト・数値比較編>逆引きシェ...
-[[if文の条件式でand, or, notを使う方法>逆引きシェルスク...
-[[ファイルのパーミッションを確認する方法>逆引きシェルス...
-[[シンボリックリンクかどうか判別する方法>逆引きシェルス...
-[[ファイルサイズが0より大きいかを確認する>逆引きシェルス...
-[[case…esacのサンプルシェルスクリプト>逆引きシェルスクリ...
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
終了行:
#navi(../)
* if文のサンプルシェルスクリプト・ファイル・ディレクトリ...
bashを利用しif .. then .. fi を利用し、ファイルおよびディ...
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* ディレクトリかどうか調べる [#mc843b66]
ディレクトリかどうかは、''-d''オプションを利用します。
#ref(chk_dir.sh)
以下のサンプルシェルスクリプトは、fooディレクトリを作成し...
途中、''-f''オプションを利用してファイルかどうかを確認し...
次に、fooディレクトリを削除し''!''を利用しfooディレクトリ...
#/bin/bash
mkdir foo
if [ -d foo ]; then
echo "foo directory found."
fi
if [ -f foo ]; then
echo "foo file found."
fi
rmdir foo
if [ ! -d foo ]; then
echo "foo directory not found."
fi
-実行結果
$ chmod +x chk_dir.sh
$ ./chk_dir.sh
foo directory found.
foo directory not found.
実行結果の出力からわかるように、ディレクトリの有無は''-d'...
また、ファイルでは無いので''-f''オプションのif ... fiブロ...
* ファイルでもディレクトリでも存在しているかどうかを調べ...
ファイルでもディレクトリでも存在しているかどうかのチェッ...
以下のサンプルシェルスクリプトは、fooディレクトリとbarフ...
#ref(chk_dir_file.sh)
#/bin/bash
mkdir foo
touch bar
if [ -e foo ]; then
echo "foo found."
if [ -d foo ]; then
echo "foo is directory."
fi
if [ -f foo ]; then
echo "foo is file."
fi
fi
if [ -e bar ]; then
echo "bar found."
if [ -d bar ]; then
echo "bar is directory."
fi
if [ -f bar ]; then
echo "bar is file."
fi
fi
rmdir foo
rm bar
- 実行結果
$ chmod +x chk_dir_file.sh
$ ./chk_dir_file.sh
foo found.
foo is directory.
bar found.
bar is file.
* ファイルが存在しているかどうかを確認する [#z3143d75]
上記の例で既に記述していますが、''-f''オプションによりフ...
以下のサンプルシェルスクリプトファイルは、fooファイルを作...
途中、''-d''オプションを利用してディレクトリかどうかを確...
次に、fooファイルを削除し''!''を利用しfooファイルが存在し...
#ref(chk_file.sh)
#/bin/bash
touch foo
if [ -f foo ]; then
echo "foo file found."
fi
if [ -d foo ]; then
echo "foo directory found."
fi
rm foo
if [ ! -f foo ]; then
echo "foo file not found."
fi
* 関連記事 [#f8f90509]
- [[if文のサンプルシェルスクリプト・数値比較編>逆引きシェ...
-[[if文の条件式でand, or, notを使う方法>逆引きシェルスク...
-[[ファイルのパーミッションを確認する方法>逆引きシェルス...
-[[シンボリックリンクかどうか判別する方法>逆引きシェルス...
-[[ファイルサイズが0より大きいかを確認する>逆引きシェルス...
-[[case…esacのサンプルシェルスクリプト>逆引きシェルスクリ...
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
ページ名: