The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   system_profiler and find Harddrive (http://hintsforums.macworld.com/showthread.php?t=49111)

mikael 12-23-2005 08:11 AM

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

trevor 12-23-2005 10:03 AM

Quote:

Originally Posted by mikael
Hi
Im trying to get info from system_profiler
to list my ATA-disc

For ATA disks, you would use
system_profiler SPIDEDataType

If you use system_profiler SPSerialATADataType you will see SATA disks.

Trevor

voldenuit 12-23-2005 10:04 AM

Would you care to elaborate what exactly you are trying to achieve ?

trevor 12-23-2005 10:07 AM

Quote:

Originally Posted by mikael
system_profiler SPSerialATADataType | grep -l "Capacity:"

You do not want to use the "-l" flag in this case, since you are not grepping files. Just skip the "-l".

Trevor

TrumpetPower! 12-23-2005 12:12 PM

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&

mikael 12-23-2005 02:24 PM

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"

voldenuit 12-23-2005 02:28 PM

Before you're done re-inventing the wheel, you might want to have a look at Apple Remote Desktop.

mikael 12-23-2005 03:48 PM

Quote:

Originally Posted by voldenuit
Before you're done re-inventing the wheel, you might want to have a look at Apple Remote Desktop.

yeah sure, but I want this on new installed computers, that way
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:

voldenuit 12-23-2005 03:56 PM

system_profiler SPParallelATADataType | grep Capacity | head -1

as trumpetpower previously suggested.

mikael 12-23-2005 05:28 PM

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

TrumpetPower! 12-23-2005 05:36 PM

Code:

diskutil list | awk '/0:/ {print $5, $3 $4}'
I don't have a multi-disk system handy to test it out on, but that should pick up every disk currently attached.

Cheers,

b&

hayne 12-23-2005 05:43 PM

Quote:

Originally Posted by mikael
Otherwise is the script ok, or any other suggestion you got to make it
more "hardcore" ?

I would suggest:
- 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.)

mikael 12-26-2005 02:46 PM

Okey heres my final version, that I liked to share!
Thanks for all the help!
I have really learned alot! :)
Mikael

Quote:

#!/bin/bash
# Inventory and print out a text file
#see comment at buttom to also create a tabtext-file
########################################################################
createtempfile=`system_profiler | sed -e 's/ *//' >>~/Desktop/temp`
gettempfile=`cat ~/Desktop/temp`
deletetempfile=`rm ~/Desktop/temp`
username=`whoami`
txtfile=~/Desktop/"$username"_inventarie.txt
date=`date "+Installationsdatum: 20%y-%m-%d"`
serialtopasteboard=`echo "$gettempfile" | egrep "Serial Number" | head -1 | sed -e 's/Serial Number/Serienummer/' | pbcopy`
########################################################################
echo "$createtempfile"
echo "$gettempfile" | egrep "Computer Name" | sed -e 's/Computer Name/Benämning/' >>"$txtfile"
echo "$gettempfile" | egrep "Serial Number" | head -1 | sed -e 's/Serial Number/Serienummer/' >>"$txtfile"
#Todays date
echo "$date" >>"$txtfile"
echo "$gettempfile" | egrep "System Version" | sed -e 's/System Version/Operativsystem/' >>"$txtfile"
echo "$gettempfile" | 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/' >>"$txtfile"
echo "$gettempfile" | egrep "Capacity" | head -1 |sed -e's/Capacity/Hårddisk/' >>"$txtfile"
echo "$gettempfile" | egrep "Memory" | head -1 | sed -e 's/Memory/Minne/' >>"$txtfile"
# Copy serial to pasteboard
echo "$serialtopasteboard"
echo "$deletetempfile"
# Remove the "#" below to also create a tab-text file
# cat "$txtfile" | tr ':' '\t' >>~/Desktop/"$username"_inventarie.xls
exit




TrumpetPower! 12-27-2005 12:17 PM

Quote:

Originally Posted by mikael
createtempfile=`system_profiler | sed -e 's/ *//' >>~/Desktop/temp`

In your particular case, this might not be a problem. However, it's a really dangerous habit to get into.

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"
Better:
Code:

fgrep "Computer Name" "$tempfilename | sed -e 's/Computer Name/Benämning/' >>"$txtfile"
b&

trevor 12-27-2005 04:26 PM

Quote:

PS I hadn't read that far down in the note, but you're certainly the winner of the UUOC Award this week.
TrumpetPower!, as this is the Newcomers' Forum, you might want to phrase your critiques in a gentler way.

Trevor

TrumpetPower! 12-27-2005 05:30 PM

Quote:

Originally Posted by trevor
TrumpetPower!, as this is the Newcomers' Forum, you might want to phrase your critiques in a gentler way.

Trevor

Sorry--I hadn't intended it as harsh. The UUOC Awards have always been a tongue-in-cheek gentle humor sort of thing to me....

Cheers,

b&

mikael 12-30-2005 04:27 AM

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:

#!/bin/bash
getinfo=`system_profiler | sed -e 's/ *//'`
username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
echo "$getinfo" | egrep "Computer Name|Serial Number|System Version|Machine Name|Number Of CPUs|CPU Speed|Capacity|Memory" | head -8 | sed -e 's/Computer Name/Benämning/;s/Serial Number/Serienummer/;s/System Version/Operativsystem/;s/Machine Name/Modell/;s/Number Of CPUs/Antal CPU/;s/CPU Speed/CPU/;s/Capacity/Hårddisk/;s/Memory/Minne/' >>"$textfile"
egrep "Serienummer" "$textfile" | pbcopy
exit

TrumpetPower! 12-30-2005 11:37 AM

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&

mikael 12-30-2005 08:59 PM

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:

#!/bin/bash
# Inventory script to a specific order and translate the output!
username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
date=`date "+Installationsdatum: 20%y-%m-%d"`
##################################################################
system_profiler | egrep "Computer Name|Serial Number|System Version|Machine Name|Number Of CPUs|CPU Speed|Capacity|Memory" | head -8 |
# Translate then add date after a specific line, then use nl to add numbers to sort by sed
sed -e 's/ *//' -e 's/Computer Name/Benämning/' \
-e 's/Serial Number/Serienummer/' \
-e 's/System Version/Operativsystem/' \
-e 's/Machine Name/Modell/' \
-e 's/Number Of CPUs/Antal CPU/' \
-e 's/CPU Speed/CPU/' \
-e 's/Capacity/Hårddisk/' \
-e 's/Memory/Minne/' \
| sed '/Hårddisk/ a\
'"$date"'' | nl -n ln -s "#" | sed -e '7 s/7/1/' -e '5 s/5/2/' -e '6 s/6/4/' -e '1 s/1/5/' -e '2 s/2/6/' -e '3 s/3/7/' -e '4 s/4/9/' -e '9 s/9/3/' | sort | sed -e 's/[0-9] #//' >> "$textfile"
egrep "Serienummer" "$textfile" | pbcopy
exit

TrumpetPower! 12-31-2005 10:06 AM

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&

mikael 01-01-2006 04:28 PM

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:

#!/bin/bash
# Inventory script to a specific order and translate the output!
username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
date=`date "+3:Installationsdatum: 20%y-%m-%d"`
modell=`system_profiler SPHardwareDataType | egrep "Machine Name|Number Of CPUs|CPU Speed" | awk 'BEGIN {FS = ":"; ORS = ""} ; { print $2 }'`
##################################################################
system_profiler | egrep "Computer Name|Serial Number|System Version|Capacity|Memory" | head -5 | sed -e 's/ *//' \
-e 's/Computer Name/1:Benämning/' \
-e 's/Serial Number/2:Serienummer/' \
-e 's/System Version/4:Operativsystem/' \
-e 's/Capacity/6:Hårddisk/' \
-e 's/Memory/7:Minne/' \
-e '/4:Operativsystem/ a\
5:Modell:'"$modell"'' \
-e '/2:Serienummer/ a\
'"$date"'' \
| sort | sed -e 's/[1-7]://' >> "$textfile"
egrep "Serienummer" "$textfile" | pbcopy
exit


TrumpetPower! 01-02-2006 12:31 AM

Quote:

Originally Posted by mikael
TrumpetPower whould you like to give me a example how to "assign the values to variables", maybe thats a good thing to learn! =)

Well, that's exactly what you're doing at the top of the script--the first four lines.

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&

mikael 01-02-2006 07:10 AM

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:

#!/bin/bash
# Inventory script to a specific order and translate the output!
username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
date=`date "+3:Installationsdatum: 20%y-%m-%d"`
info=`system_profiler -detailLevel basic | egrep "Computer Name|Serial Number|System Version|Capacity|Memory" | head -4`
harddrive=`system_profiler | egrep "Capacity" | head -1 | sed -e 's/Capacity/6:Hårddisk/'`
modell=`system_profiler SPHardwareDataType | egrep "Machine Name|Number Of CPUs|CPU Speed" | awk 'BEGIN {FS = ":"; ORS = ""} ; { print $2 }'`
##################################################################
echo "$info" | sed -e 's/ *//' \
-e 's/Computer Name/1:Benämning/' \
-e 's/Serial Number/2:Serienummer/' \
-e 's/System Version/4:Operativsystem/' \
-e 's/Memory/7:Minne/' \
-e '/4:Operativsystem/ a\
5:Modell:'"$modell"'' \
-e '/2:Serienummer/ a\
'"$date"'' \
-e '/7:Minne/ i\
'"$harddrive"'' \
| sort | sed -e 's/[1-7]://' >> "$textfile"
egrep "Serienummer" "$textfile" | pbcopy
exit

TrumpetPower! 01-02-2006 12:09 PM

Quote:

Originally Posted by mikael
Now i using system_profiler three times, is this really the right way?

That's why I'd personally write the script in Perl. It's got some really nice, simple, clean ways to do this kind of stuff. In the shell, you have to use awk to do that sort of thing, which is a bit more...awkward.

Cheers,

b&

hayne 01-02-2006 02:17 PM

Quote:

Originally Posted by mikael
Now i using system_profiler three times, is this really the right way?

There is no need to invoke system_profiler more than once.
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.

mikael 01-03-2006 06:42 AM

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:

#!/bin/bash
# Inventory script to a specific order and translate the output!
username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
date=`date "+3:Installationsdatum: 20%y-%m-%d"`
get_sys_pro='system_profiler'
info=`"$get_sys_pro" | egrep "Computer Name|Serial Number|System Version|Memory" | head -4`
harddrive=`"$get_sys_pro" | egrep "Capacity" | head -1 | sed -e 's/Capacity/6:Hårddisk/'`
modell=`"$get_sys_pro" | egrep "Machine Name|Number Of CPUs|CPU Speed" | awk 'BEGIN {FS = ":"; ORS = ""} ; { print $2 }'`
##################################################################
echo "$info" | sed -e 's/ *//' \
-e 's/Computer Name/1:Benämning/' \
-e 's/Serial Number/2:Serienummer/' \
-e 's/System Version/4:Operativsystem/' \
-e 's/Memory/7:Minne/' \
-e '/4:Operativsystem/ a\
5:Modell:'"$modell"'' \
-e '/2:Serienummer/ a\
'"$date"'' \
-e '/7:Minne/ i\
'"$harddrive"'' \
| sort | sed -e 's/[1-7]://' >> "$textfile"
egrep "Serienummer" "$textfile" | pbcopy
exit


hayne 01-03-2006 06:50 AM

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`

mikael 01-03-2006 07:00 AM

is this the right thing im getting it right, so i missed the backquotes?:
Quote:

username=`whoami`
textfile=~/Desktop/"$username"_inventarie.txt
date=`date "+3:Installationsdatum: 20%y-%m-%d"`
get_sys_pro=`system_profiler`
info=`echo "$get_sys_pro" | egrep "Computer Name|Serial Number|System Version|Memory" | head -4`
harddrive=`echo "$get_sys_pro" | egrep "Capacity" | head -1 | sed -e 's/Capacity/6:Hårddisk/'`
modell=`echo "$get_sys_pro" | egrep "Machine Name|Number Of CPUs|CPU Speed" | awk 'BEGIN {FS = ":"; ORS = ""} ; { print $2 }'`


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.