0

I have a bash script written by some previous colleague in my company. It's shellcheck result is horrible and me, who is using zsh can't run the script. He seems to use the notorious find with for loop thingy in bash. But I can't figure out how to get it better.

At the moment i got a temporary fix.

this is his code

#!/bin/bash

  releases=$(for d in $(find ${DELIVERIES} -maxdepth 1 -type d -name "*_delivery_33_SR*" | sort) ; do echo ${d##*_} ; done)
  
  
  for sr in ${releases[@]}
  do
         echo "Release $sr"
         deliveries=$(find ${deliveries_path}/*${sr}/ -type f -name "*.ear" -o -name "*.war" | sort)
         if [ ! -e ${sr}.txt ]
         then
                 for d in ${deliveries[@]}
                 do
                         echo "$(basename $d)" | tee -a ${sr}.txt
                 done
         fi
         echo
  done

And this is my code that get to even loop the first part.

#!/bin/bash
                                                                                                                                                             
  for release in $(for d in $(find "${DELIVERIES}" -maxdepth 1 -type d -name "*_delivery_33_SR*" | sort) ; do echo "${d##*_}" ; done)
  do                                                                                                                                 
    echo "Release $release"                                   
  done

As you can see I needed to put the find inside the loop and I cant save it in an variable, because when i try to loop over it will try to put \n everywhere and it is like a single element? Could any1 suggest How should I solve this problem, because this previous colleague uses this kind of find search a lot.

EDIT:

The script went to each folder with a specific name and then created a file X.X.X.txt with the version number in the X part. And appended the filenames inside the subfolder to the X.X.X.txt

Taavi Ansper
  • 143
  • 9
  • 1
    Why is this tagged [tag:zsh] if you are specifically asking about Bash? – tripleee Oct 04 '21 at 10:08
  • Without knowledge about the directories it is supposed to handle and what their contents mean, we probably understand even less than you do. Could you please [edit] to clarify what the script is supposed to do? – tripleee Oct 04 '21 at 10:10
  • It should return a list of changed "modules, files " in a release, thats the best documentation I have. – Taavi Ansper Oct 04 '21 at 10:12
  • We can't guess what files these directories contain or how they are organized. – tripleee Oct 04 '21 at 10:16
  • @TaaviAnsper: What is really horrible - in particular if you are groomed by zsh - is that the script does not care much about word splitting. In zsh, when you have a `${deliveries_path}`, you know that this goes as one argument. In bash, this would undergo word splitting, which means that the code would break fi `deliveries_path` contains a space. But with such a small script, why don't you rewrite it from scratch in zsh, since you are more familiar with zsh instead of bash? – user1934428 Oct 04 '21 at 11:46
  • Actually, the script is really plunder. For instance, `releases` is defined as a _scalar_, but further down used as if it were an _array_. Did this **ever** work? – user1934428 Oct 04 '21 at 11:47
  • It seemed to work for my colleague... Thanks for the tips. – Taavi Ansper Oct 04 '21 at 12:31

1 Answers1

2

Blindly refactoring gets me something like

#!/bin/bash

for d in "$DELIVERIES"/*_delivery_33_SR*/; do
    sr=${d##*_}
    echo "Release $sr"
    if [ ! -e "${sr}.txt" ]
    then
        find "${deliveries_path}"/*"${sr}"/ -type f -name "*.ear" -o -name "*.war" |
        sort |
        xargs -n 1 basename |
        tee -a "$sr.txt"
    fi
    echo
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • deliveries_path should be DELIVERIES and then it worked thank you very much! – Taavi Ansper Oct 04 '21 at 10:59
  • Probably [don't use upper case for your private variables](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) actually. – tripleee Oct 04 '21 at 11:02