![]() |
system_profiler and find Harddrive
Hi
Im trying to get info from system_profiler to list my ATA-disc system_profiler SPSerialATADataType | egrep "Capacity:" Its list 3 matches, how can i limit just no the first match? I have tried grep -l but then it only shows (standard output) system_profiler SPSerialATADataType | grep -l "Capacity:" Anyone with a clue, and when Im asking I output this to a file, but the text is not left-align is it anyway to control that also it whould be great! =) Merry Christmans Mikael |
Quote:
system_profiler SPIDEDataType If you use system_profiler SPSerialATADataType you will see SATA disks. Trevor |
Would you care to elaborate what exactly you are trying to achieve ?
|
Quote:
Trevor |
The standard Unix way of just getting so many rows of the output is to use head(1) and / or tail(1). You could also do an ed(1) one-liner if it starts to get a bit more complex.
This assumes, of course, that you can't get the output you want from the original source in the first place.... Cheers, b& |
Thanks for the answers, that Im trying to make is a script
that collect information about the computer/clients so I can inventory them to a website where we have all the inventory, I have made this so far, exept the "capacity" problem: And it also translate some output to swedish! So if you wanna help me to get the last thing going it whould be great! =) This is one of my first script so be nice... #!/bin/bash var1=`whoami` var2=~/Desktop/"$var1"_inventarie.txt var3=`system_profiler SPSoftwareDataType` var4=`system_profiler SPHardwareDataType` var5=`system_profiler SPSerialATADataType` var6=`system_profiler SPParallelATADataType` var7=`date "+Installationsdatum: 20%y-%m-%d"` ######################################################################## echo "$var3" | egrep "Computer Name" | sed -e 's/Computer Name/Benämning/' >>"$var2" echo "$var4" | egrep "Serial Number" | sed -e 's/Serial Number/Serienummer/' >>"$var2" # Copy Serial echo "$var4" | egrep "Serial Number" | sed -e 's/Serial Number/Serienummer/' | pbcopy echo "$var7">>"$var2" echo "$var3" | egrep "System Version" | sed -e 's/System Version/Operativsystem/' >>"$var2" echo "$var4" | egrep "Machine Name|Number Of CPUs|CPU Speed" | sed -e 's/Machine Name/Modell/' -e 's/Number Of CPUs/Antal CPU/' -e 's/CPU Speed/CPU/' >>"$var2" # newer Machines echo "$var5" | egrep "Capacity" | sed -e 's/Capacity/Hårddisk/' >>"$var2" # older Machines echo "$var6" | egrep "Capacity" | sed -e 's/Capacity/Hårddisk/' >>"$var2" echo "$var4" | egrep "Memory" | sed -e 's/Memory/Minne/' >>"$var2" |
Before you're done re-inventing the wheel, you might want to have a look at Apple Remote Desktop.
|
Quote:
i dont have to use ARD (for the inventory), the output of this script is all I need as i mentioned before, I fail on the last output of the harddrive... :cool: |
system_profiler SPParallelATADataType | grep Capacity | head -1
as trumpetpower previously suggested. |
Thanks alot trumpetpower and voldeniut!
This really made my christmas! *ho ho* =) Is it a easy way to make the text leftalign, i get only one row that way? Otherwise is the script ok, or any other suggestion you got to make it more "hardcore" ? Mikael |
Code:
diskutil list | awk '/0:/ {print $5, $3 $4}'Cheers, b& |
Quote:
- using more descriptive variable names - investigating how to avoid having to invoke 'system_profiler' multiple times (instead invoke it once without arguments and then extract all the needed info from the text returned) - avoiding the duplicate 'sed' operation just to get the serial number onto the pasteboard. (Instead I would not output anything at all to the file while extracting the info from the text - just put the info desired into variables and then output the values of those variables at the very end. You can then do the 'pbcopy' at the end as well and it will be clearer.) |
Okey heres my final version, that I liked to share!
Thanks for all the help! I have really learned alot! :) Mikael Quote:
|
Quote:
The short version of the sermon is to only ever use mktemp(1) to make temporary files. The long version of the sermon tells you why, but the short version of the long version is that other methods make it possible and / or easy for you to unintentionally clobber stuff...and it opens up a whole raft of security holes. Some really nasty ones, too. Cheers, b& PS I hadn't read that far down in the note, but you're certainly the winner of the UUOC Award this week. If ever there were something begging for an ed(1) script, it's the second half of your program. But, if you don't want to go that far, at least re-write along these lines: Bad: Code:
echo "$gettempfile" | egrep "Computer Name" | sed -e 's/Computer Name/Benämning/' >>"$txtfile"Code:
fgrep "Computer Name" "$tempfilename | sed -e 's/Computer Name/Benämning/' >>"$txtfile" |
Quote:
Trevor |
Quote:
Cheers, b& |
No problems with the UUOC adward at least I won something this week! =)
I have worked with the script once again, if i ignore the 'date' i can get much cleaner output/script, but one thing bothering me why this the script doesnt print the output in that order that I want, same order as it egrep, is what i want to the textfile, my feeling is that different computers gives differents results in the order, is it a way to control that? regards mikael - the uuoc winner this week! Quote:
|
Yes, that script is much better. You're starting to get the hang of it, I think.
As for the order...it'll be non-deterministic. If you need it in a particular order, you'll have to put it in that order, yourself. At that point, your best bet would be to re-do the program in Perl.... Cheers, b& |
What about this one, I think this will be the last one.
The Date is back and im using sed to sort the output, but I have to use/call sed 3 times to make it work, hope thats okey so i aint getting a award for that too! =D Mikael Quote:
|
The thing is...I don't think you can count on the output of system_profiler always being in that particular order--which is why I suggested Perl.
The right way to make sure the output is in the order you want is to assign the values to variables, and then print out the variables in the order you want. (Actually, you could do it with a single regular expression, too...but only if your RE-fu is pretty good.) Cheers, b& |
yeah i thought about that to, that software_profiler doesnt
have to show info in that particular, so I now using the sed "translation" to nr in my specific order, I also fixed so Machine Name and CPU get on the same row. TrumpetPower whould you like to give me a example how to "assign the values to variables", maybe thats a good thing to learn! =) Mikael Quote:
|
Quote:
Just do the same thing for Computer Name, Serial Number, and the like. Then, write out each of those in the order that you like. Of course, what you wound up doing with numbers and sort will accomplish the same thing in the end. It looks ``good enough'' to me. If this is the sort of thing you'll be doing a lot of, I'd recommend a couple books. First is /The UNIX Programming Environment/ by Brian Kernighan and Rob Pike. Next is /Learning Perl/ by Randal Schwartz and Tom Christiansen. Both are excellent introductions to their respective topics. Once you're comfortable with those, you'll have most of the programming skills required by most of the Unix system administration tasks you'll come across. Oh, sure, they're both introductory. And, when it comes right down to it, they both just scratch the surface, really. But, in the real world, you rarely have to go deep. And, if you know how and where to scratch, going deep usually isn't too hard, given a bit of patience. Cheers, b& |
I doesnt do that much of script but it think its fun to learn,
so a book might be a good innvestment! I tried the script at work and didnt get the same output so i had to add some more variables just like suggested, when became varibles active, is it when i call for them or are they read-in when the script starts? Now i using system_profiler three times, is this really the right way? Without complex the script too much... If i did: echo "$info" "$harddrive" in the beginning somehow harddrive output did get on the same row as the last from info is it anyway to control that, i did solve it by putting that at the end? Quote:
|
Quote:
Cheers, b& |
Quote:
Just invoke it and capture all of its output in a variable. Then use that variable in subsequent statements to extract what you need. sysprof_output = `system_profiler` But I would second TrumpetPower's recommendation to use Perl for this sort of thing. It makes it much easier. But of course that is a longer term solution as you would need to spend a few days/weeks learning Perl - but that would be a useful investment if you will do any other system admin stuff in the future. |
This is the finalscript i think, learning som Perl might by good.
Also fun to learn what you can/cant do easily with bash. =) Thanks once again Mikael Quote:
|
You are still running system_profiler several times in that script.
What I had suggested was to execute system_profiler once (by having it inside back-quotes and assign the result to a variable: sysprof_output=`system_profiler` Then to extract things from the text that is now in the variable $sysprof_output, you would do something like: info=`echo $sysprof_output | egrep "Computer Name|Serial Number|System Version|Memory" | head -4` |
is this the right thing im getting it right, so i missed the backquotes?:
Quote:
|
| All times are GMT -5. The time now is 10:30 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.