ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116
Rep:
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?
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.
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.
# 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
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.
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?
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.
Distribution: Fedora Core 3, Red Hat 9, CentOS 4.2, Mandriva, Ret Hat Enterprise Linux 4.0
Posts: 116
Original Poster
Rep:
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...
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.