0

I want to grep a complete line from a file. This has to be done inside a function. I am not able to get it.

function password_strength()
{
    strength_value=`cat /etc/pam.d/system-auth |grep -E "password    requisite     pam_cracklib.so try_first_pass retry=3 type="`;
    echo $strength_value;
}

Here the strength_value returns null even after there is content.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Alpha
  • 1
  • 1
  • 1
    Does the input file contain tabs or just spaces? Try replacing all sequences of spaces in your regular expression with `[[:blank:]]+`, or `.+` – glenn jackman May 25 '18 at 17:28
  • just blank spaces. – Alpha May 25 '18 at 17:29
  • run with `set -x` and show what happens – glenn jackman May 25 '18 at 17:34
  • 1
    If you're trying to find an exact line in a file `grep -Fx` would be more appropriate – glenn jackman May 25 '18 at 17:34
  • Thank you Glenn, it did work grep -Fx... :) – Alpha May 25 '18 at 17:50
  • FYI, you should use `$(cat ...)` syntax instead of ``. Ref https://stackoverflow.com/questions/4708549/what-is-the-difference-between-command-and-command-in-shell-programming – Nic3500 May 25 '18 at 17:58
  • Also no need to end all of your instruction lines with `;` characters in shell scripts - that is only needed in languages like C or Java. – Robin Green May 25 '18 at 20:59
  • @Alpha `grep -Fx` would make your string **less** likely to match, not more likely as its intent is to avid false matches since it'd disable regexp metacharacters so if you're getting more output now than you were before you can be sure it's not because you switched to using `grep -Fx`. – Ed Morton May 26 '18 at 18:28

0 Answers0