bashで引数をIFS==で文字列を分解する

  • cat file | while read ... より while read ...; do ...; done < <(...) の方が、ループ本体を現在のシェルで実行できるので変数を保持しやすい。
  • IFS='=' read -r key value <<<"$pair" の形で key=value を分解できる。
  • 動的な変数代入が必要でも eval は避け、local "$key=$value" のように扱う方が安全。
create_tags() {
  local instance_id=$1
  shift

  local tags=""
  local pair key value

  for pair in "$@"; do
    IFS='=' read -r key value <<<"$pair"
    if [[ -z "$key" || -z "$value" ]]; then
      echo "$0 ${FUNCNAME[0]}: 第2引数以降は key=value の形式で指定してください" >&2
      echo " 受け取った値: ${key}=${value}" >&2
      exit 4
    fi
    local "$key=$value"
    tags="$tags Key=$key,Value=$value"
  done

  tags="$tags Key=Creator,Value=${USER}@${HOSTNAME}"
  echo "$tags"
  # aws ec2 create-tags --tags $tags --resources "$instance_id"
}
create_tags i-02d848a7 Name=inoue-test1 Nodes=default ROLES=ekitan_essence hogehoge=4

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228