LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Filter a word : Shell Scripting (https://www.linuxquestions.org/questions/programming-9/filter-a-word-shell-scripting-4175495343/)

raj k yadav 02-18-2014 04:14 AM

Filter a word : Shell Scripting
 
[root@daassna01vapp02 ~]# grep minlen /etc/pam.d/system-auth
password requisite pam_cracklib.so try_first_pass retry=3 minlen=8 difignore=8 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=-1
[root@daassna01vapp02 ~]#


How can I filter the word "minlen" and separate the number from "minlen=8" so that if condition can be applied to check the number given with "minlen=?".

chrism01 02-18-2014 04:25 AM

quick n dirty soln
Code:

grep minlen /etc/pam.d/system-auth |cut -d'=' -f3|cut -d' ' -f1
8


raj k yadav 02-18-2014 06:04 AM

Thanks for replying chrism01, but I need to run the script for all my servers and its not necessary, everywhere the value is in the same field .

colucix 02-18-2014 07:11 AM

Independently from the position:
Code:

grep minlen /etc/pam.d/system-auth | awk -F= '/minlen/{print $NF}' RS=' '
Use command substitution to retrieve the number and check its value, e.g.
Code:

if [[ $(grep minlen /etc/pam.d/system-auth | awk -F= '/minlen/{print $NF}' RS=' ') -eq 8 ]]
then
  echo minlen is 8
else
  echo minlen is another number
fi


grail 02-18-2014 09:11 AM

tut tut tut ... I would not think I would see the day when colucix would combine a needless command ... lol
Code:

awk -F= '/minlen/{print $NF}' RS=' ' /etc/pam.d/system-auth
I would also generally recommend using (()) when performing arithmetic in bash (personal preference)

raj k yadav 02-23-2014 12:38 PM

Thanks for your replies Colucix and Grail. Your solutions worked :)


All times are GMT -5. The time now is 03:39 PM.