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 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.