Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
it's a long single line however I want to put it into a startup script in /etc/init.d/ and I want to wrap the lines within the console window but I can't get it to work. I've seen other commands work ok with a '/' on the end of each line but it doesn't work for me. This is the first part of the script:
I'm using putty on a windows 7 machine to create a terminal for access to the server. I've googled about this but I can't find the correct syntax to enable me to break and wrap the lines of this long command. The log keeps saying "Trailing options were found on the commandline". What am I doing wrong?
Last edited by tonj; 05-17-2014 at 10:43 AM.
Reason: solved
thanks for your response: good news and bad news:
your command works in that it starts ffmpeg but it causes a long scroll in the console and the stream.log file is not touched.
When the script works properly the console looks like this:
Quote:
# /etc/init.d/stream start
Starting service…
Service started
and if I tail /var/log/stream.log I can see details of the process running
but now it looks like this:
I would hazard a guess that whatever it is you are snipping is probably causing some problems.
May I ask why we are either storing the command in a variable (your first example) or capturing the output of the command into your variable (the example from next post)?
Is there an issue with simply executing it where it is needed within the script?
Are you using either of the above methods because your script is calling this command several times?
As a third alternative, if being called multiple times, perhaps you should place it in a function??
thanks but I hardly understand any of what you said, my knowledge isn't that good. I don't know what a variable is, or a function. I'm capturing the log with tail -f because it's the only way to confirm the script is working and ffmpeg is actually runnng, which at the moment it isn't. If it's not possible to wrap long lines within a bash script then ok but I don't know what can be done and what can't.
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f stream remove
rm -fv "$0"
fi
}
It is one of the difficulties of shell programming - when the parameters get long, and you try to pass the entire string as a parameter to something else that is going to interpret the string..
It is hard to get right.
One thing you can do to test things is to run it manually:
sh -vx stream start >& errorlog
This should fail as you have seen, but the recording in "errorlog" will show each step of the script, AND show how each line/variable gets expanded.
@jpollard: I think you misunderstand. The command works, that's not the problem. The essence of this topic is how to break the long line down in the script into shorter lines so it all fits in the console window and so it still works when it's run.
You have said this script works, I am not sure how as there are a number of typos and even a variable which does not exist but is called (namely PIDNAME).
So i will point out that all startup scripts are generally run as root so not only do I not see the point of your RUNAS variable, I see no point to the entire call to su?
If you think about it, if you were really calling su to change yourself to root, you would need to enter the password, which would seem mute at startup as there would
be nowhere to capture this information.
Finally, jpollard does not misunderstand and is exactly correct ... ie apart from escaping the end of the line to create the continuation, how you have quoted is the biggest issue.
To demonstrate using your original example:
Here I have colour coded all the matching sets of double quotes you have used. If you look at where you see the colour first appearing and then where it ends, you will notice that a great deal of your
code is not quoted at all, point in case - the last set of quotes is at the end of everything with nothing between them
1. su isn't needed to run a root process (it already is).
2. The CMD gets turned into multiple redirects... and the "LOGFILE" may not be a log file... (remember, the $SCRIPT is expanded..
As the "CMD" and "LOGFILE" environment variables are expanded for the assignment there can easily be more quoting peculiarities - things you expect to be quoted won't be, things you didn't expect might be...
And when SCRIPT is expanded you again get quoting reinterpreted...
@grail thankyou for your help, you solved it. Using ' instead of " worked and I am now able to break this long command down into several lines in the script, I can see the whole string clearly and the command works when the script is started.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.