ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
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???
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.
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.
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
&& 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"?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.