1

I have a list of .gz files,like:

abc-1.1.0-abc.tar.gz
abc-1.2.3-hfc.tar.gz
hfg-1.8.9-jdh.tar.gz

Content of file_name:

abcd_name_fb
hsgdh_name_cd

in my scipt, i'm capturing the strings '1.1.0,1.2.3,1.8.9' from the file names and i need to pass these strings as a 3rd argument/parameter.

Basically, my script looks like this:

if [[ "$#" -ne '3' ]]
then
    exit 1
fi

for i in /home/dir/*.gz
do
    n=$( echo $i |sed -E -n 's/.*\-(.*)\-.*/\1/p')
    echo $n
done > /home/dir/n.txt

while read -r name
do
if [[ "$1" == "some_string" ]] && [[ "$name" == "$2" ]] || [[ "$2" == "ALL" ]]
then
mkdir -p /home/ce/pwde/${name}
fi
done < /home/file_name

I'm unable to figure out how to read the file n.txt for validating the 3rd parameter and at the same time reading the file file_name.

I'm executing my script from the terminal as:

./script.sh some_string ALL 1.2.3

Thanks.

  • 1
    You may want to visit shellcheck.net to first get rid of typos and syntax issues. – Thomas May 25 '19 at 10:13
  • @Thomas : I guess, the syntax is fine..has no issue. – User May 25 '19 at 10:18
  • What content does the file_name."$1" have? It is not clear from your question. You also might want to show how you call your script and which error is spit. – Thomas May 25 '19 at 10:36
  • @Thomas : I've updated my question. Basically, I'm not able to find out how to validate the 3rd parameter , i'm tring to use a nested while loop,first reading the filen.txt to read the 3rd parameter and then the file file_name.A to read the line to perform ops function. – User May 25 '19 at 10:42
  • 2
    It may be useful to add the kind of validation you want for your third positional argument. E.g. are you trying to read n.txt line by line and check if $3 equals one of them? Also, the nested loop from your previous comment might be a way. Have you tried it? It may be worth adding your attempt to the code you posted. – fra-san May 25 '19 at 11:23
0

Maybe, earlier my mind was wandering somewhere else, now i used below nested while loop and it's working:

while read -r n
do
  if [[ "$3" == "$n" ]]
  then
    while read -r name || [[ -n "$name" ]]
    do
      if [[ "$1" == "some_string" ]] && [[ "$name" == "$2" ]] || [[ "$2" == "ALL" ]]
      then
        mkdir -p /home/ce/pwde/${name}
      fi
    done < /home/file_name
  fi
done < /home/dir/n.txt
| improve this answer | |
  • 1
    It would none the less be good if the question could be updated with the overall goal of this exercise. Going via a temporary file seems needless, but it's hard to tell whether there's an easier way to do this without knowing what it is you'd like to do. – Kusalananda May 25 '19 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.