我是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) )
答案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
#!/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
答案3
将多个值放入一个变量中,不加引号地使用它并依靠分词来再次分隔这些值并不是一种好的做法。使用数组。您可以使用相同的索引“并行”从两个或多个数组中检索值;这应该可以解决你的问题。
准备两个大小相等的数组并像这样循环:
#!/bin/bash
file=( "2019_06/" "2019_07/" )
filesize=( "100" "200" )
for ((i=0; i<"${#file[@]}"; i++)) do
echo "${file[i]}" "size=${filesize[i]}"
done
替换echo …
为所需的(和调整后的)if … curl …
块。
如果FILES
和FILESIZE
是脚本接收数据的变量,并且您无法更改它并且必须依赖分词,则从变量创建数组:
file=( $FILES )
filesize=( $FILESIZE )
变量没有被引用,分词(和文件名生成)发生在这个阶段。
答案4
通常您会希望并行执行 S3 操作:
FILES="2019_06/
2019_07/"
FILESIZE="100
200"
doit() {
s3cmd du -r s3://ui-dl-weather-ecmwf-ireland/ecmwf-archive/"$1" |
awk '{print $1}'>>"$2"
}
export -f doit
parallel doit {1} {2} ::: $FILES :::+ $FILESISZE