Storing Grep Files into Var and comparing it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing Grep Files into Var and comparing it
# 1  
Old 11-26-2011
Storing Grep Files into Var and comparing it

Hi,

I was working on the password policy settings of Solaris where i wanted to grep the results of MINDIFF and comparing it to if else to make it into a auditing script.

I stored the grep into VAR1 and compare it if MINDIFF=3 but it doesnt work.
Can anyone help me with it?
Code:
!#/bin/bash

clear

echo "Checking Password Profile Policy"

VAR1= `grep "MINDIFF" "/etc/default/passwd"`
echo $VAR1

        if [ $VAR1  == "MINDIFF=3" ];
        then
        echo "MINDIFF is set to 3, which.."
        else
        echo "HELLO";
fi


Last edited by vbe; 11-26-2011 at 05:20 AM.. Reason: use code tags for your code and data next time thanks
# 2  
Old 11-26-2011
There was a space between VAR1 and =
Code:
VAR1=`grep "MINDIFF" "/etc/default/passwd"`

If the entry is commented, then of course it will display "HELLO".

And use double quotes around "$VAR" in the "if" condition, cause it will throw a syntax error if the entry is not found in the file.
Code:
if [ "$VAR1"  == "MINDIFF=3" ];

Something similar...
Code:
test `grep -c ^MINDIFF=3 /etc/default/passwd` -eq 1  && echo "MINDIFF is 3" || echo "Oops"

HTH
--ahamed
# 3  
Old 11-26-2011
... and correct the first line (the shebang line). Also lose the excess semi-colons. I've also given the code a more conventional indentation.

Code:
#!/bin/bash

clear

echo "Checking Password Profile Policy"

VAR1=`grep "MINDIFF" "/etc/default/passwd"`
echo "$VAR1"
if [ "$VAR1"  == "MINDIFF=3" ]
then
         echo "MINDIFF is set to 3, which.."
else
         echo "HELLO"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. UNIX for Beginners Questions & Answers

Storing timestamp of files in an array

Hi, I have written the below script to get the timestamp of each files and result is as below Script find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'} Input -rw-r--r-- 1 user domain users 17382 Jul 19 06:10... (5 Replies)
Discussion started by: nextStep
5 Replies

3. Shell Programming and Scripting

Is there a BASH script allowing me to grep specifics from /var/log/messages?

I am wondering if there is a script (if one exists, not confident in my own scripting ability) that is able to bring up specified information from the /var/log/messages. I need to show logged traffic on specific dates and times and protocols (ie. Show all insecure FTP traffic (most likely via... (13 Replies)
Discussion started by: vgplayer54
13 Replies

4. Shell Programming and Scripting

One instance of comparing grep and awk

Hi. In thread https://www.unix.com/shell-programming-and-scripting/267833-grouping-counting.html rovf and I had a mini-discussion on grep and awk. Here is a demo script that compares the awk and grep approaches for this single problem: #!/usr/bin/env bash # @(#) s2 Demonstrate group... (1 Reply)
Discussion started by: drl
1 Replies

5. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

6. Shell Programming and Scripting

Issue while storing grep result

Hi All, Command: grep -i -n "rule" *.err *.log | grep -v "SP_RULE" The above command when run gives me two -three lines of output. Now I am storing the result of the grep command ie, ruleerrors=`grep -i -n "rule" *.err *.log | grep -v "SP_RULE"` Now when I echo the value of ruleerrors, I... (6 Replies)
Discussion started by: Sreejith_VK
6 Replies

7. UNIX for Dummies Questions & Answers

Storing data to many files

Please help me store the content of Input file to many files. Input file: record a b c record d record e f record ..... Create file1: a b c Create file2: (1 Reply)
Discussion started by: buddyme
1 Replies

8. UNIX for Dummies Questions & Answers

Ksh Storing Multiple Files and reading each line in each file.

How would I go about storing multiple file paths in a directory that begin like: 20080402* and run a loop that reads each line of each file thats in a given directory. So far this is what I have: #!/bin/ksh echo "ENTER Reprint Date (YYYYMMDD): " read ReprintDate echo ""... (1 Reply)
Discussion started by: developncode
1 Replies

9. Shell Programming and Scripting

Storing space delimited line in var with loop?

I have a script that converts a file into an html table. This script works fine for a 1 column table. However, I'm trying to do this for a multi-column table. My input file will look something like this: a b c d e f g h i My script basically works by taking in each line and putting that... (2 Replies)
Discussion started by: eltinator
2 Replies

10. UNIX for Dummies Questions & Answers

Storing data of files

Hi, How to store datas of the file into a string? Thanks in advance.. (1 Reply)
Discussion started by: Fr0z3n999
1 Replies
Login or Register to Ask a Question