LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-13-2008, 03:36 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Question Trouble I am having


I have been writing a bash shell script for the OS X 10.5(based off of Free BSD) and i am having a few problem.

The first is where i take a variable and set it to that of the os version. I use the command of version=| sw_vers -v productVersion and it works. However it has a newline character at the end that I have no earthly idea how to get rid of.

I have tried version=| perl -e "chomp" however it doesnt work. The new line is still there. How do i use the perl command in bash to run the chomp or chop() command??

The next one is the use of the /usr/bin/enable command, in 10.4 it is there and in 10.5 it is not. What is it replaced with???

The last is the lpstat command. I can use the lpstat -a $prntr and get the accepting status of the printer but it is a long line and I just want the accepting part. How do I isolate just the accepting part??

I ask here because none of the Mac forums are anywhere close to being good. And I have had great luck with solving issues here.

Thank you in advance for any help.
 
Old 05-13-2008, 04:47 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I don't know what version of bash you are using, but I've never seen such syntax like
Code:
variable|=command to excute
Please post what code you have written. Please use [code] tags when doing this.
The proper syntax for assigning the result of some command to a variable is one of these two:
Code:
variable=$(command to execute)
variable=`command to execute`
I prefer the first one, because the quote type in the second (backticks) is easily confused with the apostrophe, and can lead to people getting the wrong idea. The first syntax is also nestable.

Regarding your lpstat question, please post the actual output of the lpstat command, and the result you want.
 
Old 05-14-2008, 08:41 AM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Exclamation

Code:
version=| sw_ver -v productVersion
This was the code I used and it worked, with the exception that it always added a newline that I could never get rid of. Your solution of:

Code:
variable=$(command to execute)
worked beautifully, no newlines or anything. I still need to know how to use the perl command of chop and even split in the shell bash so that I can turn 10.5.2 into 10.5 so I can cover all the aspects of the Mac OS. How do I use the Perl command for Split and Chop in the bash shell to get 10.5 or 10.4 out of 10.5.2 or 10.4.2???


What I am doing is designing a shell script that will install about 16 different printers on laptops. With over 50 thousand students and maybe 10% of them using macs, we want to take stupidity out of the equation and let them still print.


The output that I want to use to try to isolate is:


command lpstat -a arc-116-bwq
ARC-116-BWQ accepting requests since Mon May 12 04:38:28 2008


I just want to use the accepting part.


Thank you in advance
 
Old 05-14-2008, 10:07 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
For simple string manipulation in a shell script, I wouldn't recommend using Perl for a couple of reasons. The general rule is that if you can do string manipulation with shell internal commands, it will be a lot quicker and a lot more robust (because you don't rely on external programs to be installed).

You should review the Parameter Expansion part of the bash manual page, from which I take the solution to your problem.... You can just use the %, %%, #, and ## parameter expansions to remove a suffixes and prefixes from strings.
Code:
full_version=10.5.2
major_version=${full_version%%.*}
minor_version=${full_version%.*}
minor_version=${minor_version#*.}
revision=${full_version##*.}

echo "full_version   = $full_version"
echo "major_version  = $major_version"
echo "minor_version  = $minor_version"
echo "revision       = $revision"
Although this is (IMO) more elegantly accomplished withing Perl using regular expression sub-pattern matching, it's a lot slower to invoke the the Perl binary for a few lines of elegant code than to use already in-memory bash internals like this.
 
Old 05-14-2008, 10:47 AM   #5
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

was kinda weird, when I keyed in exactly what you wrote and got
10.5.2
10
5
2

then I retyped the major revision and got 10.5. So I go that to work, which was interesting to say the least. if I create a popup that asks the user to hit ok so they know how that is was installed correctly (IE):

Code:
osascript -e 'tell application "Finder"' -e "activate" -e "display dialog \"Printer for $prtnm has been installed.\"" -e 'end tell
How do I force the previous window to regain focus so the rest of the script to continue???
 
Old 05-14-2008, 01:31 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I assume osascript is an OSX thing? I have no clue about that - I only use Linux.
 
Old 05-14-2008, 01:38 PM   #7
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

yes it is a OS X thing. It invokes applescript, was just wondering how to using bash to change the focus back to the correct window. Not to awefully important.

One last question and I promise I will leave you alone.

Code:
  printer=$(lpstat -a arc-116-bwq | grep -o 'accepting')
does exactly what is supposed to do when the printer is present. I am obviously attempting to verify the printer is installed. The problem is when the printer is not there, the script hangs. How do i build in a fail safe that will ensure that if the printer is not present it will either leave it blank or add something else to the variable? I need to keep it from hanging like that.


I do really appreciate everything you have done for in advance. I am also sorry for such Newbish questions as I just started in the world of Linux and OS X shell and Gui programming.
 
Old 05-14-2008, 01:46 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Bash has no concept of a current window, or any other sort of window. OSX's GUI is not the same as Linux (or other more traditional unix-like OSes), which uses X-Windows and one of various window managers / desktop environments. The only way I can imagine to achieve something like this would be through some sort of program which can be run from bash - perhaps this osascript program can do it - but for that you need to consult the osascript documentation or ask an experienced OSX programmer.

If you find out how to do it, please do post your solution here as other people may get sent to this place by a search engine.

Sorry I can't help with that bit.
 
Old 05-14-2008, 01:51 PM   #9
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
anything on the getting the script to not hang when the printer isn't installed??
 
Old 05-14-2008, 05:51 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, the man page says:

-a [printer(s)]
Shows the accepting state of printer queues. If no printers are
specified then all printers are listed.

Maybe you could take advantage of the 2nd sentence and then parse the results for the printer you expect?
 
Old 05-15-2008, 07:44 AM   #11
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

is there something like????


Code:
 printer=$(lpstat -a arc-116-bwq | grep -o 'accepting' |or | grep -o 'does not' )

I know the code of this is wrong, but is there a way to do this??? That way if the first one isnt there it will know to look for the second and then it wont hang
 
Old 05-15-2008, 08:48 AM   #12
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

The code I have that will still set the variable to accepting and not hang when it is not installed is:

Code:
 printer=$(lpstat -a arc-116-bwq | grep -o 'accepting' && 'Unknown' )
However when it is the Unknown the variable will just be blank. Is the command in the correct format???
 
Old 05-15-2008, 09:17 AM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
&& is a shell operator which runs the command on the right if the command on the left completed without error - grep never even sees it, let alone knows what to do with it. What do you wish to achieve - matching lines which "have both accepting and unknown", or "have either accepting or Unknown"?
 
Old 05-15-2008, 09:37 AM   #14
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
"have either accepting or Unknown"

That way it checks to see if it is installed with the looking of the 'accepting' or if it is not, then it will check to see if it isn't by looking for the 'Unknown'. Basically it looks for the second if the first fails.
 
Old 05-15-2008, 11:23 AM   #15
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Th lpstat manual page says the -a option lists printers accepting new jobs, so I would have thought that by definition each line of output from this command refers to a printer in that state - no need to check the lines to see - you can assume that if you have output, that they are accepting jobs. Using a parameter to that option filters the list for you, so I am not sure grep is really very useful in this base, since it's job is to filter.

Furthermore, lpstat seems to set the exit status to true if a specified printer is accepting jobs, so there's no need to even look at the output of the command - just the exit status. This is how the && and || shell constructs can be very useful. Consider this test of mine. First I list all the printers on my system (just to get the ID of then). The I test to see if one of them is accepting jobs (it is). Then I test to see if a printer which does not exist on my system is accepting jobs. It does not. The $ character is my shell prompt, lines without this are program output.
Code:
$ lpstat -a
officejet_4100_series accepting requests since Tue 06 May 2008 09:31:38 BST
officejet_4100_series_fax accepting requests since Thu 15 May 2008 17:13:14 BST
PDF accepting requests since Wed 19 Mar 2008 21:24:01 GMT
$ lpstat -a officejet_4100_series && echo "accepting jobs" || echo "not accepting jobs"
officejet_4100_series accepting requests since Tue 06 May 2008 09:31:38 BST
accepting jobs
$ lpstat -a not_a_printer && echo "accepting jobs" || echo "not accepting jobs"
lpstat: Unknown destination "not_a_printer"!
not accepting jobs
You can see that the output of lpstat is printed AND my message is printed. If you don't want to see the actual message from lpstat, you can re-direct it like this (since we are only checking the error level, not looking at the output):
Code:
$ lpstat -a officejet_4100_series > /dev/null 2>&1 && echo "accepting jobs" || echo "not accepting jobs"
accepting jobs
$ lpstat -a not_a_printer > /dev/null 2>&1 && echo "accepting jobs" || echo "not accepting jobs"
not accepting jobs
One question remains in my mind - does lpstat set the error level to non-0 if there exists a printer, but it is not accepting jobs? I could not work out how to disable a printer in this way - I tried using cupsdisable, but this simply paused the printer, and it still accepted new jobs, so I do not know. You will have to test it on a existing printer with non-accepting status to see if it works.
 
  


Reply



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
Sound Trouble + Modem Trouble on Knoppix 4.0 AJones Linux - Software 0 01-05-2007 12:06 PM
Trouble with SCSI, Trouble with Sata humbletech99 Linux - Hardware 0 05-24-2006 04:05 AM
im in trouble rxs160 Linux - Software 9 09-23-2003 10:43 PM
trouble ahead, trouble behind....trouble with mplayer Goonie Linux - Software 3 07-02-2003 02:29 AM
Kernel Trouble (Not actually trouble though) chem1 Linux - General 4 10-01-2002 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration