Open7

コマンド メモ

remonremon

たまにスクリプトを書くときにいつも困るので、ちょっとずつメモして育てていこうかな。

jq

たしかbrewで入れた。

-r

--raw-output / -r:
With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems

ちょっといい例が思いつかないが・・

$ echo '{"a": 0, "b": 1, "c": 0}' | jq '. | keys | join(" ") '
"a b c"

$ echo '{"a": 0, "b": 1, "c": 0}' | jq -r '. | keys | join(" ")'
a b c
remonremon

xargs

-n

$ echo "a b c d" | xargs
a b c d

$ echo "a b c d" | xargs -n1
a
b
d
c
$ echo "a b c d" | xargs -n2
a b
c d

-Iとは一緒に使得ないときがあった気がするが気のせいかな??

$ echo "a b c d" | xargs -n2 -I_ echo _ called
a b called
c d called
remonremon

tr

-d

$ echo "abcd" | tr -d "a"
bcd
$ echo "abcd" | tr -d "ac"
bd

$ echo '"abcd"'
"abcd

$ echo '"abcd"' | tr -d '"'
abcd
remonremon

Date

$ date +"%Y/%m/%d"
2021/03/03

Macだと'%N'は使えなかった。BSD系

$ date +"%N"
828964952

N日前

macだと-v
seqと合わせて過去10日を表示

seq 10 | xargs -I_ date -v -_d “+%Y/%m/%d”
remonremon

Bash

コマンドじゃないけど文法も怪しいのでメモ。

if

if [ "$VAR" = "" ]; then

fi
# こうするとから$VARが空の時にダメ。

if [ $VAR = "" ]; then

if

.

カレントシェルで実行する。

ENV=qa
. child.sh
echo "parent finised"
echo $ENV
exit 0
  • シェル変数は引き継ぐ
  • child.shでexportしたらexportされる
  • child.shでexitしてもexitされる。(parent finised は出力されない)
local XXX="xxx" 
YYY="yyy"
. child.sh
echo $XXX $YYY
yyy
remonremon

curl

スクリプトで描くとき。

hint:

  • JSONに文字を埋め込む
  • 可読性のために\で開業
  curl -X POST \
    -H 'Content-type: application/json' \
    -H 'Authorization: Bearer '"$TOKEN"'' \
    --data '{
     "channel": "'"$CHANNEL"'",
     "text": "'"$Text"'",
    }' \
    https://slack.com/api/chat.postMessage

-L (リダイレクト)

curl -L