Click to See Complete Forum and Search --> : c shell script help
humble linuxer
04-04-2003, 10:45 AM
I am implementing client server communication using c shell script.
The problem I am having is that I want a way of passing a variable from the client script to the server. the Client has options for the various tasks the user wants to do. if the user chooses to find information from a text file database, they supply the name of the word they are looking for. This information is the piped to the server to do the actual searching and the result returned to the client.
Below is the fragment of code that I have used, but it isn't working.
#clientScript
#begin
set name = `head -1`
echo $name | serverScript #piping the output of the client to the server.
#end clientScript
#serverScript
#begin
grep argv[1] database
/*the client program is started by typing "clientScript" at the command prompt, therefore I'm assuming that any value the usergives within the clientScript will be the second argument.
end serverScript*/
Any help will be very much appreciated.
chrism01
04-04-2003, 11:07 AM
given that argv[0] = scriptname, try
set name=$argv[1]
ie
clientscript <name_to_srch_for>
Actually, according to my book you can abbreviate arg refs to
set name=$1
To be honest, the default shell in linux is bash; similar to korn (ksh).
Any particular reason you're using C shell?
humble linuxer
04-04-2003, 11:30 AM
Originally posted by chrism01
given that argv[0] = scriptname, try
set name=$argv[1]
ie
clientscript <name_to_srch_for>
Any particular reason you're using C shell? [/B]
Actually no other argument is required to start the client, as the user will be presented with options in the client program like:
1. find word
2. replace word
etc.
Therefore the word to find will only be known during the client execution, not before.
I am required to specifically use C shell script for the task.
chrism01
04-04-2003, 12:04 PM
ok; i think i see; you've got an interactive clientscript.
How about this
result=`./serverscript $1`
in the client. This should capture stdout from the serverscript. Note using backquotes (`), rather than single quotes ('). ;)
humble linuxer
04-04-2003, 12:22 PM
Originally posted by chrism01
ok; i think i see; you've got an interactive clientscript.
How about this
result=`./serverscript $1`
in the client. This should capture stdout from the serverscript. Note using backquotes (`), rather than single quotes ('). ;)
I will try that. but the main problem I'm facing is how the server script should be written to accept the piped output from within the client program. Any suggestions on that?
#serverScript
#begin
grep argv[1] database #this is where my main problem is
garskoci
04-04-2003, 02:45 PM
Is the server script running as a daemon? Or, is client script kicking off the server script? If the client is running the server, pass the data from the client to the server as command line arguements.
humble linuxer
04-07-2003, 08:15 AM
Originally posted by garskoci
Is the server script running as a daemon? Or, is client script kicking off the server script? If the client is running the server, pass the data from the client to the server as command line arguements.
Which is easier, having the server run as a deamon, or making the client kick it off?
At the moment I am making the client kick the server off, so how do I pass the value from the client to the server?
How do I make the server a deamon if that is easier?
chrism01
04-07-2003, 08:18 AM
see my 2nd comment, but replace $1 with $<name_of_your_variable>
humble linuxer
04-09-2003, 06:32 AM
Hi Chris
I want to pass the argument by means of a pipe
e.g clientscript | serverscript
Yes I could do what you suggested( server $variable), but I am required to implement using pipes.
Any ideas?
bwkaz
04-09-2003, 09:40 AM
Originally posted by humble linuxer
clientscript | serverscript Anything that serverscript reads from it's standard input stream, is going to be coming from clientscript's standard output stream (at least, assuming a sane implementation of pipes in csh, which isn't necessarily always the case).
What you should be able to do, is echo stuff from clientscript that you want to go to serverscript. Then, use read inside serverscript to get it into a variable.
humble linuxer
04-09-2003, 10:59 AM
Originally posted by bwkaz
Anything that serverscript reads from it's standard input stream, is going to be coming from clientscript's standard output stream (at least, assuming a sane implementation of pipes in csh, which isn't necessarily always the case).
What you should be able to do, is echo stuff from clientscript that you want to go to serverscript. Then, use read inside serverscript to get it into a variable.
like this?:
clientScript
#begin
set name = `head -1`
echo $name | serverScript #pipe output of the client to server.
#end clientScript
Then, use read inside serverscript to get it into a variable.
how is it done?
#serverScript
#begin
grep `read stdin` database #//using read in server script
#end server scriptThen, use read inside serverscript to get it into a variable
bwkaz
04-09-2003, 01:20 PM
Like this (at the risk of doing something that sounds like homework for you...):
client:
set name="bob" # you can get this value any way that normally works in csh
echo $name "Server":
read name
echo $name # or do whatever with it Then, you run it with ./client | ./Server
Hang on a minute, read might be a Bourne-shell construct; csh might not work the same way... er... I'm not sure if it does or not (and I don't have csh installed). Try it and see. All "read" does is it reads a full line from stdin, and assigns the first word to the first variable that is listed after the read, assigns the second word to the second variable, etc., etc., until it assigns the rest of the line to the last variable. In this case, there's only one variable, so it assigns the whole line to it.