Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
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 ...
  1. #1
    Just Joined! Loy Glenn's Avatar
    Join Date
    Sep 2008
    Location
    Houston,TX
    Posts
    89

    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.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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 $MYVAR
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined! Loy Glenn's Avatar
    Join Date
    Sep 2008
    Location
    Houston,TX
    Posts
    89
    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.

  4. #4
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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,
    Code:
    export MYVAR=`w -s | grep 'w -s' | awk '{print $3}'`
    This will make $MYVAR accessible from this environment by any other script.

    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

  5. #5
    Just Joined! Loy Glenn's Avatar
    Join Date
    Sep 2008
    Location
    Houston,TX
    Posts
    89
    Thanks. So does putting the var in the .bashrc file make it a global variable (for all instances of a shell implemented)?

  6. #6
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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

  7. #7
    Just Joined! Loy Glenn's Avatar
    Join Date
    Sep 2008
    Location
    Houston,TX
    Posts
    89
    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.

  8. #8
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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

  9. #9
    Just Joined! Loy Glenn's Avatar
    Join Date
    Sep 2008
    Location
    Houston,TX
    Posts
    89
    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. #10
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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 right
    Code:
    alias lp="export MYVAR=`w -s | grep 'w -s' | awk '{print \$3}'`; lp"
    The 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.

    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

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...