LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-05-2007, 12:55 PM   #1
sidra
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116

Rep: Reputation: 15
Modifying a script


Hi all,

I need to modify the /etc/init.d/vsftpd script. It has the following line in it:

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

Because this ${NETWORKING} is not in quotes i get a syntax error when i try to start/restart or check the status of vsftpd on some systems. Rather than replacing the file i wanted to know if there's a way i could put quotes around this to make it look like:

"${NETWORKING}"

I know that either sed or awk might be used but i don't have much knowledge to do so. How would i do this from within a script?

Thanks in advance!
 
Old 09-05-2007, 01:49 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The test you reference is looking for the value of the NETWORKING token. That value is set by a . /etc/sysconfig/network, which should be in the script. On Fedora the /etc/sysconfig/network script is, I believe, (re)created each time system is booted.

If the evaluation ${NETWORKING} is returning a null string, putting quotes around the returned value would then test for [ "" = "no" ], which would fail. So the effect of putting quotes around the evaluation would be (if ${NETWORKING} is a null string) to take the NETWORKING != no branch. I suspect that may not be the branch down which you want to proceed.

You might, instead, try to see why the /etc/sysconfig/network evaluation is is not setting NETWORKING properly. (Or, if the evaluation was supposed to be done by some other script, why that value was not set when the vsftpd script was run.
 
Old 09-05-2007, 07:00 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
What PTrenholme said notwithstanding, you can edit any text file with, surprise, a text editor! Many people in a programming forum would like you to use vi or emacs. I suggest something like gedit, kedit, or nedit for greater newbie friendliness. Using sed or awk is technically possible to accomplish your task, but is a bit like using a table saw to sharpen a pencil.
--- rod.
 
Old 09-06-2007, 08:02 AM   #4
cconstantine
Member
 
Registered: Dec 2005
Distribution: RedHat, Ubuntu
Posts: 101

Rep: Reputation: 15
even better...

Quote:
Originally Posted by sidra View Post
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
that's a common shell script problem. If NETWORKING isn't defined, the shell tries to interpret

Code:
[ = "no" ]
and putting quotes around the var doesn't help. If the var is not set, empty var leads to empty quotes, leads to the same thing; a missing positional parameter in the test.

To wit, old-school shell hack:

Code:
[ "X${NETWORKING}" = "Xno" ]
will, at worst, degenerate to "X" = "Xno". Also, most modern shells have a syntax that you can put into the curlies to say "if the var isn't set, use this value -- a default can then be positioned. YMMV

-c
 
Old 09-06-2007, 11:29 AM   #5
sidra
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116

Original Poster
Rep: Reputation: 15
Thanks, i will check this out...
 
Old 09-06-2007, 12:19 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Ya, but: My point was that "X" = "Xno" will fail, and he'll end up taking the "network is up" branch, which is probably not what he wants to do, given the comment.
 
Old 09-27-2007, 06:45 PM   #7
sidra
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116

Original Poster
Rep: Reputation: 15
Explanation

What exactly does:
[ ${NETWORKING} = "no" ] && exit 0
mean?

It may be a stupid question but i'm confused. Basically NETWORKING is not present in my /etc/sysconfig/network file. That is one problem. But if it's not there putting quotes around this will evaluate to "" = "no" right? Which is going to be false. So if it's false then exit 0 is executed?

Thanks in advance
 
Old 09-27-2007, 08:08 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
It means: If NETWORKING equals "no" then EXIT-THE-SCRIPT.

Perhaps something like:

[ ! ${NETWORKING} = "yes" ] && exit 0

would work better? This means: If NETWORKING does not equal "yes" then EXIT-SCRIPT otherwise keep going;

NOTE - this does nothing to address the fact that the $NETWORKING token is apparently not present to begin with.
 
Old 09-27-2007, 11:32 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd say this is simpler

is not equal to

if [[ "$a" != "$b" ]]


http://tldp.org/LDP/abs/html/comparison-ops.html

Last edited by chrism01; 09-27-2007 at 11:36 PM.
 
Old 09-28-2007, 11:56 AM   #10
sidra
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116

Original Poster
Rep: Reputation: 15
So if you put quotes around it and NETWORKING is not defined what would this mean:

[ "${NETWORKING}" = "no" ] && exit 0

That would be [ "" = "no" ] && exit 0 right? Would you exit the script in this case as well?
 
Old 09-28-2007, 12:08 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
no, because "no" is a string of characters, while "" is an empty string. Therefore, the empty string does not equal the "no" string, and thus it would not exit.
 
Old 09-28-2007, 12:14 PM   #12
sidra
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116

Original Poster
Rep: Reputation: 15
So that means if NETWORKING is not defined the script will NOT exit. The problem i'm having is this. I have NTP running on a system, it sync with an NTP server. If NETWORKING is not defined in /etc/sysconfig/network then the sync doesn't happen. I've tried with quotes around NETWORKING in /etc/init.d/ntpd and without quotes. It seems like that script exits. If NETWORKING is defined then the sync with the ntp server happens and it works. So i'm trying to figure out what is going on...

Any insight would be appreciated.

Thanks
 
Old 09-28-2007, 12:23 PM   #13
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Something you could do, atleast until you figure out what's going on, would be to define $NETWORKING before all this happens. Like in a startup script, or even in the beginning of the script you are working on, define $NETWORKING as something erronious, like:

NETWORKING="bubbles"

and this way, when it gets to the part you are working on, the variable will ALWAYS be deffined as something; it will either be "yes" or "no" or "bubbles"

You could also, at the start of the script, EXPORT it, like:

EXPORT NETWORKING="whatever"

EDIT: Perhaps "undefined" would be a better word than "bubbles"

Last edited by GrapefruiTgirl; 09-28-2007 at 12:26 PM.
 
Old 09-28-2007, 03:14 PM   #14
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Just to be clear, did you look at the script /etc/sysconfig/network script that is executed at about line 20 in the /etc/init.d/network script (that's on my Fedora distribution)?

That script (again, on a Fedora system) sets the value of the NETWORKING variable. Here's the relevant section of the Fedora init.d file:
Code:
#! /bin/bash
#
# network       Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
### END INIT INFO

# Source function library.
. /etc/init.d/functions

if [ ! -f /etc/sysconfig/network ]; then
    exit 0
fi

. /etc/sysconfig/network

if [ -f /etc/sysconfig/pcmcia ]; then
        . /etc/sysconfig/pcmcia
fi


# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0
(I've highlighted the relevant line in red.)

And here's what the /etc/sysconfig/networking file looks like on my Fedora system:
Code:
NETWORKING=yes
HOSTNAME=tss-4

Last edited by PTrenholme; 09-28-2007 at 03:17 PM. Reason: Set color properly
 
  


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
help with modifying a backup script dfwcomputer Linux - General 25 10-10-2009 11:23 PM
startup script modifying my XF86Config bads Linux - Newbie 5 09-07-2005 09:29 AM
Modifying Kweather zmaster Linux - Software 4 06-29-2005 12:40 PM
Modifying this iptables script for non router use. slewis1972 Linux - Networking 6 09-21-2003 08:46 AM
modifying slackware monkeymartin Slackware 4 07-15-2003 02:32 AM

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

All times are GMT -5. The time now is 07:40 PM.

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