6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

バッシュ ワンライナー テンプレート

Last updated at Posted at 2016-03-11

bash one liner template

#if文 条件判定

[hnishi@hn home]$ ls
hnishi
[hnishi@hn home]$ if ! test ls; then echo yes; fi
[hnishi@hn home]$ if test ls; then echo yes; fi
yes
[hnishi@hn home]$ if ! ls; then echo yes; fi
hnishi
[hnishi@hn home]$ if ls; then echo yes; fi
hnishi
yes

「test」でコマンドが成功(True)したか失敗(False)したか判定

[hnishi@hn oneliner]$ if [ -f file.txt ];then echo true;else echo false;fi
true
[hnishi@hn oneliner]$ if [ -f .txt ];then echo true;else echo false;fi
false

条件中の「-f」はファイルがあるときにTrue、ないときにFalse

##コマンドの返り値
なお、コマンドの返り値(終了コード)を知りたいときは「$?」を見る

[hnishi@hn oneliner]$ ls
file.txt
[hnishi@hn oneliner]$ echo $?
0
[hnishi@hn oneliner]$ lslsls
lslsls: command not found
[hnishi@hn oneliner]$ echo $?
127

0が成功したときに返される値、それ以外は失敗

#for文 ループ

[hnishi@hn home]$ for i in {1..3};do echo $i ;done
1
2
3
6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?