LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-05-2006, 09:49 AM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
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?
 
Old 05-05-2006, 10:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 05-06-2006, 01:52 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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...

Last edited by /bin/bash; 05-06-2006 at 01:56 AM.
 
Old 05-06-2006, 07:27 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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.
 
Old 05-07-2006, 01:18 AM   #5
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
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!

Last edited by kinetik; 05-07-2006 at 02:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
Part of files to show in the bash. pittopitto Linux - Newbie 3 10-19-2005 10:12 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
Newbie troubles with Bash if/then statements jimieee Programming 4 12-04-2003 06:33 PM


All times are GMT -5. The time now is 02:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration