-
-
Save onokatio/0b1865cfb9b2418ab7fa281edc1f16aa to your computer and use it in GitHub Desktop.
【 ${var:-"abc"} 】マイナーだけど作業に便利なBashのシェル機能とか標準コマンドを紹介してみた ref: http://qiita.com/onokatio/items/d303dcd5edbb22c14c02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function event(){ ## 関数作成 | |
| echo 'get signal !!' | |
| } | |
| trap event SIGUSR1 ## これで5版のシグナルが来たときはeventコマンド(関数)を実行する | |
| echo $$ ## pid出力 | |
| while : | |
| do | |
| sleep 10 | |
| done ## 無限ループ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ a=(1 2 "hello" "world") | |
| $ echo ${a[1]} | |
| 1 | |
| $ echo ${a[3]} | |
| hello |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ echo ${arg:-"abc"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ var=test # 代入してやってみる | |
| $ echo ${var:-"abc"} # $varが定義されていればvarの値、なかったら"abc"を返す | |
| test | |
| $ echo ${var:="abc"} # $varが定義されていればvarの値、なかったらvarに"abc"を代入して"abc"を返す。 | |
| test | |
| $ echo ${var:?"abc"} # $varが定義されていればvarの値、なかったら標準エラー出力に"abc"を表示する。 | |
| test | |
| $ echo ${var:+"abc"} # $varが定義されていれば"abc"、なかったら空文字を返します。 | |
| abc | |
| $ unset var # 今度は消してやってみる | |
| $ echo ${var:-"abc"} # $varが定義されていればvarの値、なかったら"abc"を返す | |
| abc | |
| $ echo ${var:="abc"} # $varが定義されていればvarの値、なかったらvarに"abc"を代入して"abc"を返す。 | |
| abc | |
| $ unset # 上で代入されちゃったのでまた消す | |
| $ echo ${var:?"abc"} # $varが定義されていればvarの値、なかったら標準エラー出力に"abc"を表示する。 | |
| bash: var: abc | |
| $ echo ${var:+"abc"} # $varが定義されていれば"abc"、なかったら空文字を返します。 | |
| # 空文字 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ str=abcdefghi | |
| $ echo ${str:0} # 0文字目から最後まで全部 | |
| abcdefghi | |
| $ echo ${str:5} # 5文字目から最後まで全部 | |
| fghi | |
| $ echo ${str:4:2} # 4文字目から2文字分 | |
| ef |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ str=abcdefghi | |
| $ echo ${str:3:-2} # 「3文字目」から「後ろから2文字目」まで返す | |
| defg | |
| $ echo ${str:0:-6} # 「0文字目」から「後ろから6文字目」までを返す | |
| abc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ echo $(( (1 + 2) * 100 % 11)) | |
| 3 | |
| $ a=123 b=456 && echo $((a+b)) | |
| 579 | |
| $ echo $(( ( 0xf0 ^ 4 ) >> 1 )) | |
| 122 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ cat a.txt | |
| https://google.com | |
| $ curl "$(cat a.txt)" | |
| <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> | |
| <TITLE>302 Moved</TITLE></HEAD><BODY> | |
| <H1>302 Moved</H1> | |
| The document has moved | |
| <A HREF="https://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=TK3VWc2IG7DEXtvltZgD">here</A>. | |
| </BODY></HTML> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ kill -SIGUSR1 プロセスID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ kill -SIGUSR1 プロセスID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ echo {1..6} | |
| 1 2 3 4 5 6 | |
| $ echo 今日、{私,僕}はご飯を食べました。 | |
| 今日、私はご飯を食べました。 今日、僕はご飯を食べました。 | |
| $ echo {a,b}{c,d} | |
| ac ad bc bd | |
| $ cat test{1..5}.c | |
| cat1の内容 | |
| cat2の内容 | |
| cat3の内容 | |
| cat4の内容 | |
| cat5の内容 | |
| $ for i in {a,b,c,d}.c ; do echo $i ; gcc $i ; done | |
| # echo a.cをしてからgcc a.cをabcdの四回繰り返します |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment