2
3

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.

bashシェル芸

Last updated at Posted at 2018-01-18

bash

  • 随時追加予定

配列

# 宣言
array1=("r_7_&_O r_10_&_N" "r_8_&_O r_11_&_N" "r_7_&_NZ r_10_&_OE1_OE2" "r_15_&_O r_18_&_N" "r_16_&_O r_19_&_N" "r_18_&_OE1_OE2 r_20_&_NZ" "r_8_&_NZ r_18_&_OE1_OE2")

echo ${array1[@]}  # 全要素の参照
echo ${array1[0]}  # 1番目の要素の参照
echo the number of pairs ${#array1[@]}  # 全要素数の参照

for (( i=0; i < ${#array1[@]}; i++ ))  # 全要素に対するループ処理
do
  echo $i  ${array1[$i]}
done

ヒアドキュメントの中にインデントを含めたい

  • <<- ハイフンを付ける
  • tab のみ有効(スペースだとうまくいかない)

Appending a minus sign to the << has the effect that leading tabs are ignored. This allows indenting here documents in shell scripts (primarily for alignment with existing indentation) without changing their value:[a]

$ tr a-z A-Z <<- END_TEXT
>       one two three
>       four five six
>       END_TEXT

ONE TWO THREE
FOUR FIVE SIX

This yields the same output, notably not indented.

bashスクリプトの中にgnuplotのスクリプトを埋め込みたい

gnuplot -e "plot sin(x); pause -1"
#!/bin/bash
minval=0    # the result of some (omitted) calculation
maxval=4219   # ditto
gnuplot -persist <<-EOFMarker
    set title "Walt pedometer" font ",14" textcolor rgbcolor "royalblue"
    set timefmt "%y/%m/%d"
    set yrange $minval:$maxval
    set xdata time
    set pointsize 1
    set terminal wxt  enhanced title "Walt's steps " persist raise
    plot "/home/walt/var/Pedometer" using 1:2 with linespoints
EOFMarker
# rest of script, after gnuplot exits
#!/bin/bash

rm xxx.dat

for i in {1..100}
do
  echo $i >> xxx.dat
done

gnuplot -persist << EOF
  p "xxx.dat" w l
  pause -1
EOF

変数をawkに渡したい

awkの-vオプションを使う

awk -v fn_out1="$outputfile" -v var2="variant 2" '{print $0,fn_out1, var2}' hoge.txt

bashスクリプトの中にawkのコードを埋め込みたい1

#!/bin/bash -eu

inputfile=hoge.txt

################################
# AWK scripts                  #
################################
read -d '' scriptVariable << 'EOF'
BEGIN {
    print "start"
}
{
    print $0
}
END {
    print "hello"
}
EOF
################################
# End of AWK Scripts           #
################################

awk "$scriptVariable" ${inputfile}
  • read:入力行を単語に分離してシェル変数に格納する2
    • -d:セパレータの指定→ここでは文字列を区切らないように設定しているため,EOFまでのすべての文字列が,シェル変数の$scriptVariableに格納される。

参考

  1. https://stackoverflow.com/questions/15020000/embedding-awk-in-a-shell-script

  2. http://itpro.nikkeibp.co.jp/article/COLUMN/20060227/230866/?rt=nocnt

2
3
3

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?