bashスクリプトのforループに複数の変数をパラメータとして渡すには? [閉鎖]

bashスクリプトのforループに複数の変数をパラメータとして渡すには? [閉鎖]

私はLinuxを初めて使用し、bashスクリプトを書いています。スクリプトには2つの変数(内部に変数の内容を含む)があります。同じ for ループでこれらの 2 つの変数を渡し、いくつかの操作を実行しようとしています。

ただし、同じforループで2つの変数を渡すとエラーが発生します。 以下のコードで。便宜上、2つのパラメータを渡しますが、実際には異なります。コマンドからこれらの変数の出力を取得します。

以下は私のコードです。

FILES="2019_06/
2019_07/"
FILESIZE="100
200"

for file in `cat $FILES`; for filesize in `cat $FILESIZE`
 do
 do
 if [ -n "$file" ] && if [ -n "$filesize" ] 
  then
echo  $file

   curl -i -XPOST "http://localhost:8086/write?db=S3check&precision=s" --data-binary 'ecmwftrack,bucketpath=ecmwf-archive/'$file' size=$filesize'

fi
 done
done

誰もがforループで2つのパラメータを同時に渡すのに役立ちますか?

パラメータはforループのように渡す必要があります。

FILES=2019_06 FILESIZE=100
FILES=2019_07 FILESIZE=200

以下はエラーメッセージです。

ここに画像の説明を入力してください。

助けてください!

以下は私の結果です

ここに画像の説明を入力してください。

下は私のカールクーマンです

echo  curl -i -XPOST "http://localhost:xxxx/write?db=S3check&precision=s" --data-binary 'ecmwftrack,bucketpath=ecmwf-archive/'$files' size='$filesizes''


#!/bin/bash -x

# You said variables get their values from commands, so here 
# are stand-ins for those commands:
command_to_get_files(){
  aws s3 ls "s3://ui-dl-weather-ecmwf-ireland/ecmwf-archive/"| awk '{print $2}'  >>"$FILES"
}

command_to_get_filesizes(){
 for file in `cat $FILES`
 do
 if [ -n "$file" ]
  then
  # echo $file
   s3cmd du -r s3://ui-dl-weather-ecmwf-ireland/ecmwf-archive/$file | awk '{print $1}'>>"$FILESIZE"

 fi
 done
}

# I assume the values returned from the commands are whitespace delimited
# Therefore it is easy to use command substitution and transform the output
# of the commands into arrays:

files=( $(command_to_get_files) )

filesizes=( $(command_to_get_filesizes) )

ベストアンサー1

これが私が書いたものです:

#!/bin/bash

while read file filesize; do
  if [[ -n "$file" && -n "$filesize" ]]; then
    # This printf is a stand-in for what your really want to do
    printf "FILES=%s FILESIZE=%s\n" "$file" "$filesize"
    # I commented out the curl invocation because I don't understand it
    #curl -i -XPOST "http://localhost:8086/write?db=S3check&precision=s" --data-binary "ecmwftrack,bucketpath=ecmwf-archive/$file" size="$filesize"
  fi
done <<'EOF'
2019_06 100
2019_07 200
EOF

最後に区切られた文書から2つの変数を読み取ります。

おすすめ記事