LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-22-2004, 04:31 AM   #1
aus9
LQ 5k Club
 
Registered: Oct 2003
Location: Western Australia
Distribution: Icewm
Posts: 5,842

Rep: Reputation: Disabled
Request for best use of semicolon and fi in scripts


dear all

Recently fellow LQers solved an issue I had with some commands in my rc.M and rc.cups for slackware 9.1.

I had a command to go to cups and found it needed a semicolon to work correctly. like this


#####################fragment of rc.M
# Start the print spooling system.
if [ -x /etc/rc.d/rc.cups ]; then
. /etc/rc.d/rc.cups start;
fi

#####################fragment of rc.cups
esac

exit 0 ################THIS LINE NEEDED DELETING

#
# Exit with no errors.
#
# End of "$Id: cups.sh.in,v 1.22 2002/12/17 18:56:27 swdev Exp $".

############################END OF fragments

(1) Should I have ; at the end of each command line in say rc.M?

(2) for a IF THEN command do I need a fi at the end of that command?

3) Any links to use of ; or fi would be appreciated.
 
Old 04-22-2004, 04:33 AM   #2
aus9
LQ 5k Club
 
Registered: Oct 2003
Location: Western Australia
Distribution: Icewm
Posts: 5,842

Original Poster
Rep: Reputation: Disabled
Heres one simple link to ; INFO

http://www.justlinux.com/nhf/Shells/...html#semicolon
 
Old 04-22-2004, 07:41 AM   #3
portux
Member
 
Registered: Apr 2003
Location: Portugal
Distribution: Slackware 10.1
Posts: 42

Rep: Reputation: 15
aus9,

As you said earlier I'm not the best person to ask, and you're right.

I found this link about if and fi:

http://gd.tuwien.ac.at/linuxcommand.org/wss0090.php#if

I didn't come across with the option to not having a fi, but that may be possible and I'm just ignorant of the fact; I'd like to know if that's possible.
 
Old 04-22-2004, 01:46 PM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
for every 'if' there must be a 'fi'
using the ';' gives a little extra 'continuity' since it tells the shell that something follows. using the '&' is similar. Using '&&' tells bash to run the following command only if the first was successful.
a ';' is not used after 'then'.
It's best to make sure there is a space between all commands and ; & &&
also fi should have a blank space after it and it's best to end a script with a blank line
One of the best script writers out there, Klaus Knopper, always uses the semicolon.
Your script got fouled up someway. there should be a 'fi' to finish that rc.cups call. look at the entries above and below. See how they all begin and end in a little group. the way yours is all the calls that follow the rc.cups call are dependent on cups being present. if you were to uninstall cups rc.M wouldn't find it and wouldn't start anything in the script after that.
Start at the top of the script and follow the if/fi combos. If they are nested then you may get the fis together at the end:
if 'something' ; then
if 'somethingelse' ; then
'do something'
fi
fi

It helps if the scripts are well indented. You can insert TABS to help
 
Old 04-24-2004, 05:20 PM   #5
aus9
LQ 5k Club
 
Registered: Oct 2003
Location: Western Australia
Distribution: Icewm
Posts: 5,842

Original Poster
Rep: Reputation: Disabled
Thank you all for your input

and if anyone feels like showing any links thank you for your time as well
 
Old 05-13-2004, 02:14 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
for general bash introduction try : http://www.ibiblio.org/pub/Linux/doc...og-Intro-HOWTO
(web page load quick cause it is plain text file )
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 12:10 PM
printing numbers without using semicolon vijeesh_ep Programming 18 09-08-2004 11:59 AM
Need help running scripts from scripts sdouble Linux - Newbie 3 05-31-2004 12:56 PM
request from every one the anti-riced General 3 04-21-2004 12:37 PM
greetings and request portable oxygen Linux - Software 6 10-07-2003 07:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration