LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-16-2005, 08:48 AM   #1
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Rep: Reputation: 15
Question 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
 
Old 11-16-2005, 09:25 AM   #2
fvgestel
LQ Newbie
 
Registered: Oct 2005
Location: Netherlands
Posts: 21

Rep: Reputation: 15
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...
 
Old 11-16-2005, 10:22 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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.
 
Old 11-18-2005, 02:52 AM   #4
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Original Poster
Rep: Reputation: 15
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
 
Old 11-22-2005, 05:34 AM   #5
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Original Poster
Rep: Reputation: 15
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
 
Old 11-22-2005, 06:09 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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.
 
Old 11-22-2005, 06:15 AM   #7
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Original Poster
Rep: Reputation: 15
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.
 
Old 11-22-2005, 07:28 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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?
 
Old 11-22-2005, 08:33 AM   #9
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Original Poster
Rep: Reputation: 15
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
 
Old 11-22-2005, 08:40 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
ok.
nice and simple then!

good luck!
 
Old 11-22-2005, 09:40 AM   #11
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,091

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
this is a related post i was in:
http://www.linuxquestions.org/questi...hreadid=351711

this furthers helmet's idea,
 
Old 11-22-2005, 04:58 PM   #12
GSX
Member
 
Registered: Feb 2005
Location: Germany
Distribution: Debian Etch, kernel 2.6.18
Posts: 103

Original Poster
Rep: Reputation: 15
thats a good info....thx a lot..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
creating shell script programming using KNOPPIX.. help cinderella Linux - Newbie 4 12-20-2004 07:12 PM
shell script programming problem newbie_ken Programming 5 08-16-2004 11:19 AM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM
shell script programming steltner Linux - General 0 05-17-2003 07:00 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration