0

Im new to the Shell-Script World and im trying to find a solution to my problem.

I have the following Script:

HSLarray= $(grep -E '((HSL|hsl)+(\()+(.)+(\));)' $1 )

for i in "${HSLarray[@]}"
do
    echo $i
done

(The Script has no real functionality at this moment) It should search a SCSS File (or any file) for the Pattern "hsl(*);", but it exits with the Error:

./search_HSL.sh: line 1: color:: command not found

If i use the grep from above as a normal command, it gives the following output:

$ grep -E '((HSL|hsl)+(\()+(.)+(\));)' scss/template/sass/_base.scss
    color: hsl($button_primary_hue, 0%, 67%);
    border: $standard_border_width dashed hsl($primary_hue + 1, 95%, 51%);
    color: hsl($button_primary_hue, 0%, 40%);
        color: hsl($button_primary_hue, 0%, 67%);
        color: hsl($button_primary_hue, 0%, 0%);
        color: hsl($button_primary_hue, 0%, 67%);
    background-color: hsl($menuButton_hue, 0%, 100%);
        background: hsl($menuButton_hue, 0%, 100%);
    color: hsl($menuButton_hue, 0%, 67%);
    border: $standard_border_width solid adjust-color(hsl($splitBar_hue - 12, 57%, 46%), $saturation: $splitBar_saturation, $lightness: $splitBar_lightness);
    border: $standard_border_width solid adjust-color(hsl($splitBar_hue - 12, 57%, 46%), $saturation: $splitBar_saturation, $lightness: $splitBar_lightness);
    border-right: $standard_border_width solid adjust-color(hsl($splitBar_hue - 12, 57%, 46%), $saturation: $splitBar_saturation, $lightness: $splitBar_lightness);
    border-left: $standard_border_width solid adjust-color(hsl($splitBar_hue - 12, 57%, 46%), $saturation: $splitBar_saturation, $lightness: $splitBar_lightness);
    background-color: adjust-color(hsl($primary_hue, 95%, 86%), $saturation: $sectionStack_saturation, $lightness: $sectionStack_lightness);
            adjust-color(hsl($sectionStack_primary_hue, 0%, 67%), $saturation: $sectionStack_saturation, $lightness: $sectionStack_lightness) 100%)) padding-box);
                adjust-color(hsl($sectionStack_primary_hue, 95%, 85%), $saturation: $sectionStack_saturation, $lightness: $sectionStack_lightness) 100%)) padding-box);
                adjust-color(hsl($sectionStack_primary_hue + 1, 25%, 69%), $lightness: $sectionHeader_border_lightness) 100%)) padding-box);
        color: hsl(212, 53%, 60%);
    //    color: hsl(212, 53%, 60%);
    border: $standard_border_width solid hsl($primary_hue + 2, 34%, 51%);
        right: $standard_border_width solid hsl($secondary_hue + 1, 63%, 83%);
        left: $standard_border_width solid hsl($secondary_hue - 2, 36%, 78%);
    background-color: hsl($secondary_hue, 100%, 92%);
        bottom: $standard_border_width dotted hsl($secondary_hue + 7, 63%, 77%);
        top: $standard_border_width dotted hsl($secondary_hue + 7, 63%, 77%);
    background-color: hsl($secondary_hue + 2, 68%, 89%);
        bottom: $standard_border_width dotted hsl($secondary_hue + 7, 63%, 77%);
        top: $standard_border_width dotted hsl($secondary_hue + 7, 63%, 77%);
        bottom: $standard_border_width solid adjust-color(hsl($filterBuilder_bracketBorder_hue - 12, 57%, 46%), $saturation: $filterBuilder_bracketBorder_saturation, $lightness: $filterBuilder_bracketBorder_lightness);
        left: $standard_border_width solid adjust-color(hsl($filterBuilder_bracketBorder_hue - 12, 57%, 46%), $saturation: $filterBuilder_bracketBorder_saturation, $lightness: $filterBuilder_bracketBorder_lightness);
        top: $standard_border_width solid adjust-color(hsl($filterBuilder_bracketBorder_hue - 12, 57%, 46%), $saturation: $filterBuilder_bracketBorder_saturation, $lightness: $filterBuilder_bracketBorder_lightness);
    background-color: hsl($primary_hue - 3, 100%, 87%);
        color: hsl($primary_hue + 2, 0%, 100%);
        hsl($primary_hue + 2, 15%, 75%) 100%)));
    border-top: 2px dotted hsl($primary_hue + 2, 100%, 73%);
    border-right: 2px dotted hsl($primary_hue + 2, 100%, 73%);
        adjust-color(hsl($listGrid_headerButton_disabled_hue - 6, 88%, 90%), $saturation: $listGrid_headerButton_disabled_saturation, $lightness: $listGrid_headerButton_disabled_lightness) 100%)) padding-box);
    color: hsl($primary_hue, 0%, 67%);
    background-color: hsl($secondary_hue, 93%, 88%);
    background-color: hsl($primary_hue - 6, 54%, 84%);
    background-color: hsl($primary_hue - 6, 54%, 84%);
    background-color: hsl($primary_hue - 6, 54%, 84%);
    background-color: hsl($primary_hue + 9, 57%, 88%);
    color: hsl($primary_hue - 2, 53%, 27%);

So im thinking that in Line 1 of the output, something escapes the grep command and is taken as an command of it's own. But how can i prevent this? I want each line as something as a String Element in the Array.

LeWAIEN
  • 23
  • 7
  • 4
    No idea why the other comment got removed, so I repeat it: `HSLarray= $(...` should be `HSLarray=$(...`. Bash does not allow spaces around the assignment operator `=`. – Socowi Jun 15 '18 at 07:04
  • Hey Thanks for the Quick response, it really worked now. – LeWAIEN Jun 15 '18 at 07:04
  • 1
    @Socowi To be precise, it's not that spaces aren't allowed, but that you have a completely different meaning with a space there. `foo=bar` means "set `foo` to `bar` in the current environment", `foo= bar` means "run command `bar` in a sub-environment where `foo` is set to empty". – Amadan Jun 15 '18 at 07:06
  • 1
    Also note that `var=$(cmd)` is not a declaration of an array. It's just a string assignment. You can see that with `declare -p HSLarray`. See [wiki.bash-hackers.org/syntax/arrays](http://wiki.bash-hackers.org/syntax/arrays) and [Reading output of a command into an array in Bash](https://stackoverflow.com/a/32931403/6176817). – PesaThe Jun 15 '18 at 07:33
  • [shellcheck.net](https://www.shellcheck.net) is good at spotting common mistakes like this. – Gordon Davisson Jun 16 '18 at 18:39

0 Answers0