Saturday, May 4, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 2270  / 2 Years ago, sat, february 19, 2022, 2:05:08

I am checking the password complexity of ocredit in /etc/pam.d/password-auth which is this line :


password   requisite pam_pwquality.so try_first_pass retry=3 minlen=8 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=1 gecoscheck=1

I would like to grep the ocredit value "1" and make it as variable, let say check_ocredit.


My script would be checking, if the ocredit value is not 0, then it will return "true", otherwise, it will show what is the current value set in /etc/pam.d/password-auth.


Here's my logic that I'm going to implement :


if [[ $check_ocredit -ne "0" ]]
then
true
else
echo "Current ocredit value is : $check_ocredit"
fi

The output will looks like this :


Current ocredit value is : 1

I try with :


grep ocredit /etc/pam.d/password-auth | awk '{print $10}' | awk -F "=" '{print $2}'

But I was thinking, what if ocredit is missing, or it is not at 10th position.. so my result would be wrong. Can anyone advice me on this?


More From » command-line

 Answers
0

You can do it in a single grep command with -o for "print only the matching portion of the line" and -P which gives us K: "ignore anything matched up to here":


$ grep -oP 'ocredit=Kd+' /etc/pam.d/password-auth
1

So, in your script, you could do:


check_ocredit=$(grep -oP 'ocredit=Kd+' /etc/pam.d/password-auth)
if [[ "$check_ocredit" -ne "0" ]]
then
echo "Current ocredit value is : $check_ocredit"
fi

Your version of the if would have printed the value only when it was 0.


[#307] Saturday, February 19, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
imberst

Total Points: 370
Total Questions: 107
Total Answers: 123

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;