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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-16-2005, 08:48 AM
|
#1
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Rep:
|
question about shell-script programming
Hi all,
i would like to write a shell-script which should do the following:
For an example the scripts name ist TEST...you run
....#TEST & (to put it to the background)
The script is running in a loop and waiting for a special command, f.e "mycommand"..
Due to this fact the script is running in the background I can further work on my console prompt and when I write the "mycommand" the script will identify that I wrote "mycommand" and will do what it have to....
So I need a way to catch my console-input by a shell-script....Anybody an idea??
Thanks in advance
gsx
|
|
|
|
11-16-2005, 09:25 AM
|
#2
|
|
LQ Newbie
Registered: Oct 2005
Location: Netherlands
Posts: 21
Rep:
|
You don't need a shellscript for that. Just use command 'fg' to get back to the program you've put in the background.
And catching console input from within the shell is a costly operation. I've written something like that in perl ( using the select system call ), but system-load was going very high, due to a lot of interrupts and the overhead a scripting language has...
|
|
|
|
11-16-2005, 10:22 AM
|
#3
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
I don't have the same experience as fvgestel in capturing user input, but I come to the same conclusion: don't.
This is just far too complicated to mess with. I'd suggest an alternative. This might be useful:
1. Start the program by running it in the background
2. Have the script periodically check for the existence of a specific file (this is your flag)
3. Create an alias for your "mycommand" so that it will touch/create the file the script is looking for (and anything else it needs to do)
4. When the script sees the file is present, perform whatever action is necessary
5. Have the script delete the flag-file when it's done
Using something like that avoids the complexity of sharing user input between your script and the shell, and it accomplishes the same thing (from the user's perspective). The difference is, there's some added latency. If your script checks for the file every X seconds, then there's the chance the script won't perform its task for X-1 seconds after "mycommand" is issued. X can be reduced to minimize this delay, but the smaller X gets, the more time and resources spent checking for the file.
Another alternative would have the script capture a signal. I don't have any experience with signal handling in scripts so I can't help there. However, a signal would cause the script to immediately begin its task after "mycommand" is issued. This method would still require aliasing "mycommand." The alias would probably need to contain a kill command to send the specific signal.
|
|
|
|
11-18-2005, 02:52 AM
|
#4
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Original Poster
Rep:
|
fvgestel, dark_Helmet....
thanks a lot for your good ideas...
I thought about it thats to difficult to catch the console prompt by a shell-script
I think your idea with the alias and file-checking is a good way for my issue..
Its time to try it
thx
gsx
|
|
|
|
11-22-2005, 05:34 AM
|
#5
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Original Poster
Rep:
|
Ok, here is my test-script:
Code:
#!/bin/bash
#Variables
weiter="no"
# Aliases for root
#if [ -e "/root/.bashrc" ]; then
# cat alias.conf >> /root/.bashrc
#else
# echo
# echo "An error occured..."
#fi
while [ "$weiter" != "quit" ]; do
sleep 1
if [ -e "/tmp/gsx.flag1" ]; then #Info: When alias "d" is pressed, in /tmp the file "gsx.flag1" will be created!!!
echo
echo "[daemon] [start/stop/restart]:"
read a b
/etc/init.d/$a $b
rm /tmp/gsx.flag1
elif [ -e "/tmp/gsx.flag2" ]; then #When alias "q" is pressed, in /tmp the file "gsx.flag2" will be created!!!
echo
echo "Quitting gsx-daemon...."
weiter="quit"
rm /tmp/gsx.flag2
exit 0
fi
done
exit 0
I start the script in the background:
lowrider:/home/gsx/scripts# bash test &
[1] 6266
lowrider:/home/gsx/scripts#d
[daemon] [start/stop/restart]:
apache2 start
.
.
# Then the apache2-script show me the usage-text and don´t start the daemon!!
# Additionally the job will be stopped
[1]+ Stopped bash test
lowrider:/home/gsx/scripts#
--> What´s the error in the script? Why the daemon could not be started...when i type "/etc/init.d/apache2 start" (at normal cmd-prompt) it works fine..
--> When the job is stopped (like above) how to get it running again? First kill and restart or is there aa other way?
--> When i press "q" to exit the script (still in background) the script exit´s but i don´t have an command prompt. I have to type in a further command and the the job will be closed...is there any possibility to exit the script immediately?
Thx in advance
gsx
|
|
|
|
11-22-2005, 06:09 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
incidentally, it's a bad idea to call programs and scripts test
as often you will pick up a shell built-in or /usr/bin/test by mistake.
|
|
|
|
11-22-2005, 06:15 AM
|
#7
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Original Poster
Rep:
|
the name test was only a given name for this little "Test"-Script...
when the script run i will only take the code of this script...
But I think the error of daemon-use is not due to the fact that the script is named "test"..
gsx
Last edited by GSX; 11-22-2005 at 06:17 AM.
|
|
|
|
11-22-2005, 07:28 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
this seems peculiar.
Why in the background?
If it responds to user input why not just use a shell script?
seems pointless it being in the background to me.
can you explain further?
|
|
|
|
11-22-2005, 08:33 AM
|
#9
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Original Poster
Rep:
|
Ok...
this issue we talking about is only a small part...
I am writing a script which do a lot of things....f.e. Configuring the Xserver, starting daemons, change your grub-settings and so on.....
I want to give the user two options to use the script:
1. Like you said only as shell-script (work with parameter like $1,$2,...)
2. To load the script at boot into background and to use just a lot of simple commands to do the things for you...it�s more comfortable...
this script above is for testing only...
gsx
|
|
|
|
11-22-2005, 08:40 AM
|
#10
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
ok.
nice and simple then!
good luck! 
|
|
|
|
11-22-2005, 04:58 PM
|
#12
|
|
Member
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103
Original Poster
Rep:
|
thats a good info....thx a lot..
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:09 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|