1

Here's the script. It is successful when I run it from the BASH prompt, but not in the script. Any ideas?


When I say "fails," I mean the sed regex doesn't match anything, so there is no replaced text. When I run it on the command line, it matches.

Also, I might have an answer to this. It has to do with my grep alias and GREP_OPTIONS having a weird interplay. I'll post back with the details on those.

#!/bin/bash

for ((x = 101; x <= 110; x++)); do
    urls="${urls} www$x.site.com/config"
done;

curl -s ${urls} | grep -i "Git Commit" | sed -r "s/.*Git Commit<\/td><td>([^<]+).*/\1/g"
  • 1
    how does it fail? What output do you expect and what do you get? Are there any error messages? There is no grep regular expression there, just a simple string. Are you sure it is the grep that's failing? – terdon Jul 10 '14 at 14:57
  • does $urls already have a value when in shell mode? – LatinSuD Jul 10 '14 at 15:07
  • added details per @terdon. And yes, I paste the whole thing into the command prompt (minus the shebang line). I'm pretty sure my GREP_OPTIONS are forcing color to always, while my command-line grep is using an alias that is automatically turning off coloring due to the piped grep output. So sed is getting color codes sent to it. Will give more details tonight when it let's me add an answer. – kghastie Jul 10 '14 at 15:13
  • 1
    Thanks for the edit. So you've solved it? Why in the world was your grep set to --color=always? That's a very bad idea. – terdon Jul 10 '14 at 15:25
  • 2
    That's exactly what --color-auto is for. It is intelligent enough to add it when you want it but not when you don't. – terdon Jul 10 '14 at 16:05
3

I was actually able to figure this out, and I figure I'd add it here for the next googler who bangs their head against the same wall.

I had a grep alias and GREP_OPTIONS set. This caused color highlighting to remain on in the script, even when piping to another command. That usually doesn't play nicely with sed.

Here's my .alias and options:

alias grep='grep -i --color'
export GREP_OPTIONS="--color=always"

So when running from the script, it doesn't use the aliased command and so forces color to always be on. So when I checked my alias and saw the --color option (which means auto, which means "don't color output that gets piped to another command" (like sed).

I was confused because I forgot I had set GREP_OPTIONS as well, so I expected the grep in the script to have color set to auto by default (as it would if I hadn't set the global GREP_OPTIONS). But not so.

Here are my new settings (I believe the --color flag to GREP_OPTIONS is redundant, but I leave it there as a reminder):

alias grep='grep --color=always'
export GREP_OPTIONS="--ignore-case --color"

That way, any time I am on the command line, I'll have highlighting on for all my greps (which is usually what I want). But in scripts it will default to coloring only when not piped to another command. I'll still have to add --color=always to many of my scripts (since I tend to prefer highlighting in most cases, even when piping to another command, unless I don't ever see the output).

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.