Results 1 to 10 of 23
If this has been covered, please redirect....
I want to make a script that will determine "CurrentPrinter" by use of "w -s" by strippping the output to show only the ...
- 09-30-2008 #1
printing from client side
If this has been covered, please redirect....
I want to make a script that will determine "CurrentPrinter" by use of "w -s" by strippping the output to show only the ip address reported in the "FROM" column on the line of the terminal which is shown to issue the command "w -s". Can this info be put into a variable? Also would the command "lpr -P"variable" "file" print the file to "CurrentPrinter" Thanks, Loy.
- 10-01-2008 #2
Ok, let's try to do this step by step.
1) You are interested in the output of "w -s".
2) But only in the line which contains a "w -s" also. (We assume there is only one.) One can filter out lines containing a phrase easily by sending it to the grep program (over pipes).
Now we have: w -s | grep 'w -s'
which gives us the proper line.
3) You seem to be interested in the third entry. The awk tool is good at breaking text to separate fields. Hence, we ask it to print out only the third field, which leads to: w -s | grep 'w -s' | awk '{print $3}
4) This line finally outputs only the wished entry, if any. To assign output of any command to a variable, one enbraces it in ` characters.
MYVAR=`w -s | grep 'w -s' | awk '{print $3}'`
5) To fetch and process the content out of any variable, it has to be preceded by a '$' character. For example, echo $MYVARDebian GNU/Linux -- You know you want it.
- 10-02-2008 #3
Thank you for the help, been a while and may have taken me 4 or 5 hours to get here...One more question, how can I be sure that the variable will be stored for use by another script or command. It looks as if they way in which i am trying to utilize this isn't receiving the value of the variable. Thanks, Loy.
- 10-02-2008 #4
If you want a variable to be used by some other script, then you need to assign it to an environment variable. To continue GNU-fan's example,
This will make $MYVAR accessible from this environment by any other script.Code:export MYVAR=`w -s | grep 'w -s' | awk '{print $3}'`
However, if you start a new shell, then MYVAR won't be in its environment. If you want to do this, then you should include the export command in your .bashrc file.
(I'm assuming you're using bash, so if you're using another shell, you should modify those commands accordingly.)Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 10-02-2008 #5
Thanks. So does putting the var in the .bashrc file make it a global variable (for all instances of a shell implemented)?
- 10-02-2008 #6
I believe that putting "export VAR = blah" into .bashrc will cause $VAR to be set to blah for all shells. It's then referred to as an environment variable, not a global variable.
But remember, if this is to be called as a cronjob belonging to root, then you should put it in root's .bashrc, not your own.
By the way, welcome to the forums!Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 10-03-2008 #7
Ok, I have a .bashrc in my home dir. but the value from this command does not get stored. The var. is created and left empty. I can give more info if needed to specify the results I am looking for. Thanks, Loy.
- 10-03-2008 #8
What is the output of the command?
Code:w -s | grep 'w -s' | awk '{print $3}'Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 10-03-2008 #9
my static IP address, when I run it from the command line. But I can't seem to intergrate this into our current setup. I want this info to be pulled at the time a lp command is issued from a program which reads the command from a config file. It seems as if the prgram issuing the lp command, is running the command from its credentials. I may have to us a sudo in the config file the prog reads from...although the var. is blank if i execute the command from a file (ie put the command in a file, name it xx.sh, make it executable, and run it...)?????
- 10-03-2008 #10
Hmmmmm....
It seems calling it from .bashrc results in the output of "w -s" not containing the string "w -s", which results in the variable being empty.
I can't quite figure out the best way to do this. It seems like there should be a way to alias the command lp to initialise this variable before calling the lp command, but I can't quite get it right.
This is the closest I could come up with, but it's still not quite rightThe reason it's wrong is that the value MYVAR will be set to is decided when the alias command is called, not when lp is called, thus it will have the same result as before -- an empty variable.Code:alias lp="export MYVAR=`w -s | grep 'w -s' | awk '{print \$3}'`; lp"
Sorry I couldn't be of more help, but I hope this puts you onto the right track.Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode


Reply With Quote