0

I'm trying to create an array and use Homebrew to install apps. But before I install the app I want to check to see if it's installed. I know it's already there in Brew, but I was looking at something like this:

declare -a applications=(Spotify Discord Franz Rectangle visual-studio-code VLC microsoft-excel)

for i in "${applications[@]}"
  do
    #check for app installer 
    if [ -d "/Applications/$i.app" ]; then
      echo " $i is installed" 
      appstatus="Installed"  
      else
        echo "/Applications/$i.app"
        appstatus=" $i, not installed - installing now"
        brew install cask "$i"
      fi
    echo $appstatus
  done`

However what's happening is the array of applications will always fail on VSC and Excel due to the -'s not being in the name in the application folder.

Either I was going to create another array with the correct names underneath - or I was wondering if I can parse the array and remove the -'s for when we check to see if the app is installed.

Hope this makes sense.

  • `array[$index]=newvalue` -- you can iterate over the indices using `"${!array[@]}"` – Charles Duffy Jul 05 '22 at 18:30
  • That said, what _should_ the name be? Should it be spaces instead of dashes? Why don't you just store the spaces in the array in the first place instead of editing it after-the-fact? – Charles Duffy Jul 05 '22 at 18:31
  • 1
    `declare -a applications=(Spotify Discord Franz Rectangle "visual studio code" VLC "microsoft excel")` – Charles Duffy Jul 05 '22 at 18:32
  • BTW, `echo $appstatus` should be `echo "$appstatus"`. See [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Jul 05 '22 at 18:37
  • ...or is the problem that `brew install` expects spaces? You can go in the reverse direction just for that one command: `brew install cask "${application// /-}"` – Charles Duffy Jul 05 '22 at 18:40

1 Answers1

1

To modify your array, replacing dashes with spaces all at once:

applications=( "${applications[@]//-/ }" )

To do it one-at-a-time:

for idx in "${!applications[@]}"; do  # iterate over array indices
  application=${applications[$idx]}   # look up item at index
  application=${application//-/ }     # transform to new value
  applications[$idx]=$application     # store new value
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441