Link to home
Create AccountLog in
Avatar of atom_jelly
atom_jellyFlag for United States of America

asked on

if statement nesting, Linux, Shell Scripting

Trying to nest another if statement and I got some great help but I just don't understand why this section will not work, thanks for any help folks.


     37 if ! grep -q "$string5"
     38
     39 then
     40     echo "Skipping "$string5" is not found"
     41 else
     42
     43   if grep -q "$string5"
     44 then
     45     echo ""$string5" is found needs to be removed to avoid use of  blank passwords"
     46     sed -i -e 's/"$string5"/g' "$file"
     47 fi
     



      1 #!/bin/bash
      2 #exec > testlog.log 2>&1
      3 #set -x
      4 file="test"
      5 string="password"
      6 string1="ctrlaltdel"
      7 string2="ca::"
      8 string3="#ca::"
      9 string4="audit=1"
     10 string5="nullok"
     11 if ! grep -q "$string" "$file"
     12
     13 then
     14
     15   echo "The md5 hash does not exist the md5 hash will be inserted"
     16
     17 sed -i -e '4a\
     18 password --md5 $1$bBL.' "$file"
     19
     20 else
     21
     22   echo "The md5 hash exists in $file"
     23 fi
     24
     25 if grep -q "$string4" "$file"
     26
     27 then
     28    echo "Doing nothing $string4 kernel parameter is enabled"
     29 fi
     30
     31    if ! grep -q "$string4" "$file"
     32 then
     33    echo "$String4 kernel parameter is missing will be enabled"
     34     sed -i -e  "/quiet/ s|$| "$string4"|" "$file"
     35 fi
     36
     37 if ! grep -q "$string5"
     38
     39 then
     40     echo "Skipping "$string5" is not found"
     41 else
     42
     43   if grep -q "$string5"
     44 then
     45     echo ""$string5" is found needs to be removed to avoid use of  blank passwords"
     46     sed -i -e 's/"$string5"/g' "$file"
     47 fi
     48
     49 if grep -q "$string3" "$file"
     50 then
     51   echo "Doing nothing "$string1" already disabled"
     52 else
     53   if grep -q "$string1" "$file"
     54   then
     55     echo "$string1 is found must be disabled and logged"
     56     sed -i -e "s/$string2/$string3/g" "$file"
     57     sed -i '3a\
     58 ca:nil:ctrlaltdel:/usr/bin/logger -p security.info "Ctrl-Alt-Del was pressed"' "$file"
     59
     60   fi
     61 fi
SOLUTION
Avatar of Steven Carnahan
Steven Carnahan
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of atom_jelly

ASKER

Thanks. I am very thankful.
you have accepted the wrong answer since the syntax is wrong

my answer is correct
JARmod101:

atom_jelly accepted the response from amitnepal ID: 38581298 which the first part appears to show the original code missing the 2 lines that I had pointed to in my response ID: 38581190.

The second half of the accepted solution appears to be an attempt at the logic that was presented by omarfarid in response ID: 38581191 with some differences.

I am curious to know what the author actually used as a resolution.

If it was simply the removal of the two lines then I would think that my response should be the accepted with perhaps an assist by arnitnepal since he did offer the exact same solution.

If it was the logic that pointed to the solution then I would think that atom_jelly's solution is the proper one since it is more accurate and assists only if they proved valuable.

These are just my observations and I will accept your decision without question.  :)
pony10us :
 
 Yes you are right , the first part  shows the original code missing 2 lines that you pointed in your response ID : 38581190 , which The author could have figured out from your response. But really , his question was how to nest another if , as per what I could understand from his question, he already had that working, could have been typo in his code, so , I really rewrote , the piece of code that he had, not from your pointing in ID : 38581190 .

Regarding the second part  and regarding " Attempt at the logic presented by omarfarid " 

 I wonder what other logic would you have in if else with bash ?

There is no logic in bash if else format. it s just a standard format :

if condition; then
do this
else
do this
fi

This is a standard format , i don't think that is a logic, so whoever would have replied , would have replied in this format, so it's not attempted logic. And honestly , i didn't go through other expert's responses, i saw scripting question, i read authors question, and replied to his question.

 Scripting is a part of my daily work... so I just went ahead and replied. Also , if you look closely ,  my response is simpler to understand for the author, he understood my response, so he accepted my response. as far as i can think.

Other than that..Nothing to say.
In the original question

"Trying to nest another if statement and I got some great help but I just don't understand why this section will not work, thanks for any help folks. "

atom_jelly wanted to know what is wrong in the syntax and what what is the correct syntax of nested if, and my my comment answered that.

So I recommend my answer to be chosen as the correct answer.

Thanks
SouthMod:

I am not concerned with the recognition from this question however to address the previous response, my original post addressed "why this section will not work" to a point however after further review, even it was incorrect.  I believe that line 47 would have to be removed as well.

In any case, I did not address it from an explanation of why/how to nest "if" statements as other experts had done. So as they say on Shark Tank: "For that reason I'm out"  :)

I would say that omarfarid should probably be recognized as having the correct answer in response: ID: 38581191