Linux

如何將多個變數作為 bash 腳本中的參數傳遞給 for 循環?

  • May 5, 2020

我是 linux 新手,我正在編寫一個 bash 腳本……在腳本中,我有 2 個變數(在變數內容裡面)。我試圖在同一個 for 循環中傳遞這兩個變數並執行一些操作。

但是當我在同一個 for 循環中傳遞 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) )
#!/bin/bash

# You said variables get their values from commands, so here 
# are stand-ins for those commands:

command_to_get_files(){
 echo "2019_06/
2019_07/"
}

command_to_get_filesizes(){
 echo "100
200"
}

# 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) )

# Repeat this pattern for how ever many parameters you have

# Hat tip to Kamil for idea of using arrays and C-style for loop

for ((i=0; i<"${#files[@]}"; i++)) do
   # This printf is a stand-in for what your really want to do
   #printf "FILES=%s FILESIZE=%s\n" "${files[$i]%?}" "${filesizes[$i]}"
   echo curl -i -XPOST "http://localhost:8086/write?db=S3check&precision=s" --data-binary "ecmwftrack,bucketpath=ecmwf-archive/${files[$i]%?} size=${filesizes[$i]}"
done

百分號問號%?去掉了/你似乎不想要的。

把它放在一個名為myscript.sh然後執行它的文件中:

$ chmod +x myscript.sh
$ ./myscript.sh 
FILES=2019_06 FILESIZE=100
FILES=2019_07 FILESIZE=200

更新:輸出printf替換為echo curl ...

$ ./myscript.sh 
curl -i -XPOST http://localhost:8086/write?db=S3check&precision=s --data-binary ecmwftrack,bucketpath=ecmwf-archive/2019_06 size=100
curl -i -XPOST http://localhost:8086/write?db=S3check&precision=s --data-binary ecmwftrack,bucketpath=ecmwf-archive/2019_07 size=200

我是這樣寫的:

#!/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

它從最後提供的 heredoc 中讀取兩個變數。

引用自:https://unix.stackexchange.com/questions/584327