0

I am trying to do an iterative loop with a skopeo copy. I have tried:

  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

My variables are rightly placed, but it doesn't seem to hand it off properly to my command. Can anyone point out my problem?

2
  • You first 2 lines cannot be outside of the loop, they have no visibility of the $line variable Commented Feb 4, 2020 at 17:48
  • See Bash FAQ 001 for the proper way to iterate over the lines of a file. Commented Feb 4, 2020 at 18:32

2 Answers 2

3

You are evaluating the variables just once, before the loop. I'm guessing you want

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

There is no need to export variables unless they need to be visible to a subprocess (such as, here, skopeo - but since you pass these values as variables, I'm guessing it doesn't look for and use variables with these names); and sed -e script x uses x as the input file name, not as the string to process. The dash character is just a normal character, and doesn't need to be backslash-escaped in sed. Finally, don't read files with for.

The <<< "here string" syntax is a Bash extension (which is also available in some other shells, but not portable to POSIX/Bourne sh).

Sign up to request clarification or add additional context in comments.

Comments

0

At the time you are setting the variables with the sed command, the $line variable does not exist. You should change it to:

  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
  done

1 Comment

This still fails to fix several of the remaining bugs, though without access to the input file, we can only speculate about what the code is really supposed to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.