我试图用skopeo副本进行迭代循环。我试过:
export image_name=$(sed -e 's/\-[^*]*$//' "$line");
export version=$(sed -e 's/[^0-9.]*//' "$line" | sed 's/.tar//');
IFS=$'\n'
for line in "$(cat list_files.txt)"; do
skopeo copy \
docker-archive:/opt/app-root/src/"$line" \
docker://private/dsop/test/"$image_name":"$version" \
--dest-creds="$USERNAME":"$PASSWORD" \
--dest-tls-verify=false
done我的变量是正确的,但它似乎没有把它正确地交给我的命令。有人能指出我的问题吗?
发布于 2020-02-04 17:51:35
在循环之前,您只对变量进行一次评估。我猜你想
while read -r line; do
image_name=$(sed -e 's/-[^*]*$//' <<<"$line")
version=$(sed -e 's/[^0-9.]*//;s/\.tar$//' <<<"$line")
skopeo copy \
docker-archive:/opt/app-root/src/"$line" \
docker://private/dsop/test/"$image_name":"$version" \
--dest-creds="$USERNAME":"$PASSWORD" \
--dest-tls-verify=false
done < list_files.txt没有必要使用export变量,除非它们需要对子进程可见(例如,这里是skopeo --但由于您将这些值作为变量传递,我猜它不会查找和使用带有这些名称的变量);sed -e script x使用x作为输入文件名,而不是作为要处理的字符串。破折号字符只是一个普通字符,不需要反斜杠-在sed中转义。最后,don't read files with for.
<<< "here string“语法是Bash扩展(在其他shell中也可用,但不能移植到POSIX/Bourne sh)。
发布于 2020-02-04 17:52:45
在使用sed命令设置变量时,$line变量不存在。你应该把它改为:
IFS=$'\n'
for line in "$(cat list_files.txt)"; do
image_name=$(sed -e 's/\-[^*]*$//' "$line");
version=$(sed -e 's/[^0-9.]*//' "$line" | sed 's/.tar//');
skopeo copy \
docker-archive:/opt/app-root/src/"$line" \
docker://private/dsop/test/"$image_name":"$version" \
--dest-creds="$USERNAME":"$PASSWORD" \
--dest-tls-verify=false
donehttps://stackoverflow.com/questions/60062913
复制相似问题