Skip to main content
added 544 characters in body
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231

When you have problems like this, you should start removing things from your test until you get something that works.

For example, if your starting point is:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*');echo $comment

First strip off the variable stuff:

cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'

Then strip off the second grep:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

Then the other grep:

cat /usr/share/applications/brasero.desktop

Eventually you'll find a point where it starts working. In your particular case, it'll start working with this:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

So it's that second grep which is a problem. Add the other stuff back and see if it still works:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=');echo $comment

Indeed it does. So what's the problem? Well, notice how on this last one, the output was in color? The output has ANSI color escape codes in it. This is preventing the second grep from matching.

The fix is to either change your grep alias to be --color=auto, or do unalias grep, or manually add --color=never to the commands.


There's also another approach that can be taken to this. Since you know that it works from within a script, the only explanation is that it's environmental.

So you can start with a clean environment, bash --noprofile --norc, and start bringing in your profile piece by piece until it breaks.

There is a catch to this technique though. Even with --noprofile --norc, there are a few differences between CLI and a script, such as history expansion, and job control. You can turn these off as well though: set +H and set -m.

When you have problems like this, you should start removing things from your test until you get something that works.

For example, if your starting point is:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*');echo $comment

First strip off the variable stuff:

cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'

Then strip off the second grep:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

Then the other grep:

cat /usr/share/applications/brasero.desktop

Eventually you'll find a point where it starts working. In your particular case, it'll start working with this:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

So it's that second grep which is a problem. Add the other stuff back and see if it still works:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=');echo $comment

Indeed it does. So what's the problem? Well, notice how on this last one, the output was in color? The output has ANSI color escape codes in it. This is preventing the second grep from matching.

The fix is to either change your grep alias to be --color=auto, or do unalias grep, or manually add --color=never to the commands.

When you have problems like this, you should start removing things from your test until you get something that works.

For example, if your starting point is:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*');echo $comment

First strip off the variable stuff:

cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'

Then strip off the second grep:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

Then the other grep:

cat /usr/share/applications/brasero.desktop

Eventually you'll find a point where it starts working. In your particular case, it'll start working with this:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

So it's that second grep which is a problem. Add the other stuff back and see if it still works:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=');echo $comment

Indeed it does. So what's the problem? Well, notice how on this last one, the output was in color? The output has ANSI color escape codes in it. This is preventing the second grep from matching.

The fix is to either change your grep alias to be --color=auto, or do unalias grep, or manually add --color=never to the commands.


There's also another approach that can be taken to this. Since you know that it works from within a script, the only explanation is that it's environmental.

So you can start with a clean environment, bash --noprofile --norc, and start bringing in your profile piece by piece until it breaks.

There is a catch to this technique though. Even with --noprofile --norc, there are a few differences between CLI and a script, such as history expansion, and job control. You can turn these off as well though: set +H and set -m.

Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231

When you have problems like this, you should start removing things from your test until you get something that works.

For example, if your starting point is:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*');echo $comment

First strip off the variable stuff:

cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'

Then strip off the second grep:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

Then the other grep:

cat /usr/share/applications/brasero.desktop

Eventually you'll find a point where it starts working. In your particular case, it'll start working with this:

cat /usr/share/applications/brasero.desktop |grep '^Comment='

So it's that second grep which is a problem. Add the other stuff back and see if it still works:

comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=');echo $comment

Indeed it does. So what's the problem? Well, notice how on this last one, the output was in color? The output has ANSI color escape codes in it. This is preventing the second grep from matching.

The fix is to either change your grep alias to be --color=auto, or do unalias grep, or manually add --color=never to the commands.