LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH IF statements Part II (https://www.linuxquestions.org/questions/programming-9/bash-if-statements-part-ii-441888/)

kinetik 05-05-2006 09:49 AM

BASH IF statements Part II
 
Hi All

Me again, have a problem, again, need some help, again! :)


I have written this:

Quote:

P1=`less /folder/file.conf | grep "page1.html" | awk '{print $1}'`
P2=`less /folder/file.conf | grep "page2.html" | awk '{print $1}'`
P3=`less /folder/file.conf | grep "page3.html" | awk '{print $1}'`
P4=`less /folder/file.conf | grep "page4.html" | awk '{print $1}'`

clear

if [ $P1 == "#Text" ]
then
echo "Page1.html's text is commented out..."
elif [ $P1 == "Text" ]
echo "Page1.html's text is NOT commented out..."
fi


if [ $P2 == "#Text" ]
then
echo "Page2.html's text is commented out..."
elif [ $P2 == "Text" ]
echo "Page2.html's text is NOT commented out..."
fi


if [ $P3 == "#Text" ]
then
echo "Page3.html's text is commented out..."
elif [ $P3 == "Text" ]
echo "Page3.html's text is NOT commented out..."
fi


if [ $P4 == "#Text" ]
then
echo "Page4.html's text is commented out..."
elif [ $P4 == "Text" ]
echo "Page4.html's text is NOT commented out..."
fi
done

echo "To Continue, hit Enter now"
read

I basically want all the if statements to run, not just one or the other. How can I do this? I know it's probably a simple thing I'm missing and/ or doing wrong, but just need a nudge to the Great, Big, Wonderful Answer!

Any help please? :)

druuna 05-05-2006 10:23 AM

Hi,

I don't know if I understand what it is you want to accomplish.

The above code, when edited to fix some 'errors', executes all the if-then parts and prints the appropriate response.

Code:

#!/bin/bash

P1="#Text"    # Changed
P2="Text"      # to
P3="#Text"    # test
P4="Text"      # script.

clear

if [ $P1 == "#Text" ]
then
  echo "Page1.html's text is commented out..."
elif [ $P1 == "Text" ]
then
  echo "Page1.html's text is NOT commented out..."
fi


if [ $P2 == "#Text" ]
then
  echo "Page2.html's text is commented out..."
elif [ $P2 == "Text" ]
then
  echo "Page2.html's text is NOT commented out..."
fi


if [ $P3 == "#Text" ]
then
  echo "Page3.html's text is commented out..."
elif [ $P3 == "Text" ]
then
  echo "Page3.html's text is NOT commented out..."
fi


if [ $P4 == "#Text" ]
then
  echo "Page4.html's text is commented out..."
elif [ $P4 == "Text" ]
then
  echo "Page4.html's text is NOT commented out..."
fi

echo "To Continue, hit Enter now"
read

Output sample run:

Code:

Page1.html's text is commented out...
Page2.html's text is NOT commented out...
Page3.html's text is commented out...
Page4.html's text is NOT commented out...
To Continue, hit Enter now

Things I changed/added:
- added bash hashbang,
- after each elif there should be a then,
- removed done statement.

Hope this is what you wanted.

/bin/bash 05-06-2006 01:52 AM

Quote:

I basically want all the if statements to run, not just one or the other.
That would require individual if statements and not elif's. The elif will only test if the first "if" was false.

if [ $P1 == "#Text" ]
then
echo "Page1.html's text is commented out..."
fi
if [ $P1 == "Text" ]
then
echo "Page1.html's text is NOT commented out..."
fi


if [ $P2 == "#Text" ]
then
echo "Page2.html's text is commented out..."
fi
if [ $P2 == "Text" ]
then
echo "Page2.html's text is NOT commented out..."
fi

etc...

Disillusionist 05-06-2006 07:27 AM

True but pointless, the if statements are mutually exclusive:

If $P1 is set to "Text" then it can't also be "#Text"

However, we don't know the format of /folder/file.conf, if the file contains more than 1 reference for the html files the results could be different than expected.

Also, the original script looks to be incomplete (unless the done statement was supposed to be a comment.

kinetik 05-07-2006 01:18 AM

Thanks for the help so far :)


The "done" I put in due to pure desperation since what I was trying simply wasn't working.

There are more than one instance of both "Text" and "#Text" in the file.conf, but the values Page1.html, Page2.html, Page3.html and Page4.html are all unique.

Basically I want this portion of script to run first, then a second portion allowing you to choose which page (page1.html or page2.html etc.) you want to have commented out. My second portion of script works pretty good, but the one problem I have is that if there is nothing to comment out my script will leave the entry in file.conf as it is (for example "Text...") which is fine. When the sample is already "#Text", it will add another (for instance "##Text").

This is basically to allow me to see which are commented out and which aren't so that the second portion of the script will be based on this portion's information.

So this script will feature in another long script, first showing you which entries are commented out and which aren't, then giving you an option of which entry you want to comment out/ enable, then perform your selected action, restart a service if you choose to, then exit.


...Later

Ah, OK! I see now I left out the "elif then" bit! Still learning I guess, thanks for everyone's help, you're all super! :) :)


All times are GMT -5. The time now is 01:59 AM.