0
1

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 3 years have passed since last update.

bash備忘録

Last updated at Posted at 2021-02-13

一連のコマンドや、ジョブサブミット等で、*.shを使う。何かとbashコマンドが使えるとやっぱり便利なのでbashの文法を勉強したところから順次まとめていく。

参考にさせていただいたサイト: Bashの便利な構文だがよく忘れてしまうものの備忘録 - Qiita

やりたいことから

コマンドcommandの標準出力を変数に代入する

val=`command`

commandの標準出力を変数valに代入できる。コマンドを囲んでいるのはバッククォート(日本語キーボードの場合はshift + @)。例えば

val=`ls ./`
echo ${val}

で、PWDでlsを実行した時の結果がvalに代入されていることがわかる。

日付と時刻毎のファイルに出力を書き込む

NOW=`date +'%Y_%m_%d-%H'`   #変数NOWに YYYY_MM_DD-HH 形式の時間を変数NOWに代入
LOGFILE="$NOW.log"          #日付と時刻.logの文字列を変数LOGFILEに代入
ls ./> ${LOGFILE}       #`ls`の出力を変数LOGFILEの名前のファイルに書き込む

文字列の結合

valA="hoge"valB="fugafuga"を結合したい場合は

valC=${valA}${valB}

で出来る。

valC=${valA}${valB}
echo ${valC}

を実行すればhogefugafugaが標準出力に表示されるはず。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?