Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Ê×Ò³>>Unix&Linux>>system_profiler and find Harddrive

system_profiler and find Harddrive

mikael
12-23-2005, 08:11 AM
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
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
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
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
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
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


#!/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
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 (http://www.ruhr.de/home/smallo/award.html) 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:
echo "$gettempfile" | egrep "Computer Name" | sed -e 's/Computer Name/Benämning/' >>"$txtfile"

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

b&

trevor
12-27-2005, 04:26 PM
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
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!


#!/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

#!/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&


 

TOP

Windows Server Outsell
Unix Signals And C++ E

For more info

Unix Signals And C++ E
Windows Server Outsell
ssh setup for password
Bash script does not w
esc code 
ARD send unix command 
question about binarie
Scanning mail 
Issuing multiple comma
How do I install Linux

News Archive

/etc/hosts? 
Manually Start a Start
mounting missed hard d
Using Netinfo in Singl
darwin/bsd login probl
system_profiler and fi
Send mail from script 
mounting a drive 
OS X disk first aid ha
system.log shows steal

Related stories:

Using Netinfo in Single User Mode
darwin/bsd login problem
Send mail from script
mounting a drive
OS X disk first aid hangs
system.log shows stealth mode connection attempts
good book for newbies to OSX command line

Copyright@2004-2005 www.zzcoke.com All Right Reserved

link:Citenna