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

  • whileへの情報の渡し方は cat hoge | while … より while read do ; … ; done < <( echo …) の方が良い。パイプだどパイブ以降はサブシェルとして扱われ、サブシェル内で変数を操作しても、サブシェルの外へは影響を及ぼせない。外の変数を操作できない。
  • IFS== で key=value を分解できる
  • eval local $key=$value の形で関数内だけで有効な変数とできる
create-tags(){
  INSTANCEID=$1 ; shift
  while read VAL ; do
    IFS== read create_tags_key create_tags_value <<<"$VAL"
    if [[ "x" = "x${create_tags_key}" ]] || [[ "x" = "x${create_tags_value}" ]] ; then
      echo -e "$0 ${FUNCNAME[0]} です。\n 第2引数以降は key=value の形式でお願いします"
      echo " key=value が ${create_tags_key}=${create_tags_value} でした"
      exit 4
    fi
    eval local ${create_tags_key}=${create_tags_value}
    TAGS="$TAGS Key=${create_tags_key},Value=${create_tags_value}"
  done < <( echo $@ | sed -e 's/ +/ /g' -e 's/ /\n/g' )
  TAGS="$TAGS Key=Creater,Value=${USER}@${HOSTNAME}"

  echo -e "$0 ${FUNCNAME[0]} $KEY $VALUE $moemoe $hogehoge"
  echo $TAGS
  # aws ec2 create-tags --tags Key=Name,Value=${NAME} Key=Nodes,Value=${NODES} Key=Roles,Value=${ROLES} Key=Creater,Value="$USER@$HOSTNAME" --resources ${INSTANCEID}
}
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