You are not logged in.

#1 2007-11-20 19:36:15

spookykid
Member
From: Portugal
Registered: 2006-07-23
Posts: 141

bash script

Hi all, I'm in trying to implement a bash script to automagically detect which video driver the computer has and then provide a bare minimum xorg.conf file with the correct detected video driver. I'm not a good coder but I've come up with this:

#!/bin/bash

lspci | grep "VGA" | awk '{print $5}' > /tmp/videodriver

XDRIVER=$(cat /tmp/videodriver)

if [ "$XDRIVER" == "Intel" ]
then
    XDRIVER=i810  
    MANUFACTURER="Intel Corporation"
fi

if [ "$XDRIVER" == "nVidia" ]
then
    XDRIVER=nv
    MANUFACTURER="nVidia Corporation"
fi

if [ "$XDRIVER" == "ATI" ]
then
    XDRIVER=ati
    MANUFACTURER="ATI Technologies Inc"
fi

if [ "$XDRIVER" == "" ]
then
    XDRIVER=vesa
    MANUFACTURER="VESA Capable"
fi

echo "Found a" $MANUFACTURER "device. Will try to use the" $XDRIVER "driver."
cp -v /etc/X11/xorg.$XDRIVER /etc/X11/xorg.conf

I'm hoping to hear some suggestions or possible improvements. One downside I find in this is that I have to already have the xorg.<name> files built, and the most perfect solution would be to create a generic one and then overwrite the driver section(don't know yet how to do this). I've found raymano's script but unfortunately hwd isn't working for me hmm so I'm trying this way. Thank you.


There is no knowledge that is not power!

Offline

#2 2007-11-20 19:42:33

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Re: bash script

look at sed -i this will allow inline editing.

As for generating, I can only think of X -configure and copy it across though I generally find this actually detects my system very well and wouldn't need the editing.

Plus arch has hwdetect got a feeling this or a variant can generate a xorg file.

Edit its
hwd -xa from beginners guide have no experience with it though.

Last edited by FeatherMonkey (2007-11-20 19:54:36)

Offline

#3 2007-11-20 20:19:57

szlend
Member
From: Ptuj, Slovenia
Registered: 2007-11-04
Posts: 7

Re: bash script

hwd is outdated so it no longer works so that wont help him much. It can't even detect the latest xorg-server. The best thing to run is "X -configure" since it works flawlessly.

Last edited by szlend (2007-11-20 20:21:14)

Offline

#4 2007-11-21 02:04:46

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: bash script

I would recommend something like this:

#!/bin/sh

VENDOR=$(lspci | grep "VGA" | awk '{print $5}')

case "$VENDOR" in
    Intel)
        XDRIVER="i810"
        MANUFACTURER="Intel Corporation"
        ;;
    nVidia)
        XDRIVER="nv"
        MANUFACTURER="nVidia Corporation"
        ;;
    ATI)
        XDRIVER="ati"
        MANUFACTURER="ATI Technologies Inc"
        ;;
    *)
        XDRIVER="vesa"
        MANUFACTURER="VESA-capable"
        ;;
esac

echo "Found a $MANUFACTURER device, will try to use the $XDRIVER driver."
[ -e /etc/X11/xorg.conf ] && mv /etc/X11/xorg.conf /etc/X11/xorg.conf.orig
ln -sf /etc/X11/xorg.$XDRIVER /etc/X11/xorg.conf

You might also want to check out the Bash Programming HOWTO and the Advanced Bash Scripting Guide.

Offline

#5 2007-11-21 13:47:42

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: bash script

szlend wrote:

hwd is outdated so it no longer works so that wont help him much.

I am upgrading hwd by replacing the lshwd hardware detect engine with lspci (pciutils) and lsusb (usbutils). Version 5.0 will be released soon.

For the Xorg PCI device/driver list hwd will use this table:
http://www.calel.org/pci-devices/xorg-device-list.html


Markku

Offline

#6 2007-11-21 14:38:54

VikM
Member
Registered: 2007-11-10
Posts: 50

Re: bash script

Just to show what a scripting guru I am lol

awk '/VGA/ {print $5}'

and you don't need grep anymore.

Offline

#7 2007-11-22 16:13:50

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: bash script

VikM wrote:
awk '/VGA/ {print $5}'

While we're at it:

sed -n '/VGA/s#.*: \(\w*\).*#\1#p'

Offline

#8 2007-11-22 18:22:57

VikM
Member
Registered: 2007-11-10
Posts: 50

Re: bash script

Yeah, great for showing up! Actually I wanted to help a little, the guru thing was a joke (obviously I hope).
Now I'm really curious, which one would you use and why?

Offline

#9 2007-11-23 03:01:34

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: bash script

Actually, awk is a lot more powerful and you could write the entire thing as a single awk script (as opposed to just using it for getting the xth element of an lspci line).

Try this out:

lspci | awk '/VGA/ {
if ($5 == "nVidia") {
  manuf="Nvidia corp"
  xdriv="nv" }
if ($5 == "Intel") {
  manuf="Intel corp"
  xdriv="i810" }
print "Found a", manuf, "will use", xdriv, "driver."
system("echo copy copy, move move, symlink symlink " xdriv " " xdriv) }'

This shouldn't be hard to finish into the same program.

Offline

#10 2007-11-23 03:09:34

spookykid
Member
From: Portugal
Registered: 2006-07-23
Posts: 141

Re: bash script

Man this scriptin' is way ahead for me... smile


There is no knowledge that is not power!

Offline

#11 2007-11-23 03:14:04

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: bash script

Nothing the UNIX Grymoire can't fix. smile

Offline

#12 2007-11-23 13:10:49

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: bash script

woo! i'll never use sed again... awk for teh win! kill sed!


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

#13 2007-11-23 13:23:14

VikM
Member
Registered: 2007-11-10
Posts: 50

Re: bash script

Don't kill it, have mercy!

Take some time to browse beginning from http://sed.sourceforge.net/#docs. If you don't have the time, http://sed.sourceforge.net/sed1line.txt may provide some nice short-cuts.

Offline

#14 2007-11-23 14:03:23

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: bash script

well, the grymoire has a nice sed page too:

Anyhow, sed is a marvelous utility. Unfortunately, most people never learn its real power.

this makes it sound like vi smile (or emacs, but I don't use emacs tongue)

Last edited by lloeki (2007-11-23 14:04:58)


To know recursion, you must first know recursion.

Offline

#15 2007-11-23 15:53:06

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: bash script

he should add a paragraph to that sentence

Anyhow, sed is a marvelous utility. Unfortunately, most people never learn its real power. [...] Because it's pretty much the model example of un-newbiefriendlyness

big_smile

awk i got preeeety fast though smile

Last edited by test1000 (2007-11-23 15:53:23)


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

#16 2007-11-23 19:15:15

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: bash script

well, read this small paragraph and you will get to know why smile
that said, sed is pretty simple to use and learn actually (and the grymoire really helps in that matter), it's just that the documentation is completely useless unless you know what it means (which defeats the whole point of a documentation)


To know recursion, you must first know recursion.

Offline

#17 2007-11-27 08:54:03

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: bash script

lcpci -nn

01:00.0 VGA compatible controller [0300]: ATI Technologies Inc Radeon Mobility X1400 [1002:7145]
03:00.0 Ethernet controller [0200]: Broadcom Corporation BCM4401-B0 100Base-TX [14e4:170c] (rev 02)
03:01.0 FireWire (IEEE 1394) [0c00]: Ricoh Co Ltd R5C832 IEEE 1394 Controller [1180:0832]
03:01.1 SD Host controller [0805]: Ricoh Co Ltd R5C822 SD/SDIO/MMC/MS/MSPro Host Adapter [1180:0822] (rev 19)
03:01.2 System peripheral [0880]: Ricoh Co Ltd R5C843 MMC Host Controller [1180:0843] (rev 01)
0c:00.0 Network controller [0280]: Intel Corporation PRO/Wireless 3945ABG Network Connection [8086:4222] (rev 02)

For my information, to improve the script in hwd, what would be an accurate syntax to grep/awk the id number e.g. 8086:4222 of  Network controller

ID=$(lspci -nn | .......

Markku

Offline

#18 2007-11-27 10:07:12

VikM
Member
Registered: 2007-11-10
Posts: 50

Re: bash script

Maybe using "lspci -mn" would be a better idea. See "MACHINE READABLE OUTPUT" in lspci man.

Offline

#19 2007-11-27 15:40:41

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: bash script

This I also thought. Whoever developed pciutils didn't keep the same format in both -nn and -mnn, split the vendor and device ids and merged in the descriptions.

lspci -nn
0c:00.0 Network controller [0280]: Intel Corporation PRO/Wireless 3945ABG Network Connection [8086:4222] (rev 02)

lspci -mnn
 0c:00.0 "Network controller [0280]" "Intel Corporation [8086]" "PRO/Wireless 3945ABG Network Connection [4222]" -r02 "Intel Corporation [8086]" "Unknown device [1021]"

This is what I have in mind. But "[ ]" is not  unique character when some hardwares use it in the description.

lspci -nn | awk /Network/ | sed 's/\[/"/g' | sed 's/\]/"/g' | awk -F\" '{print $4}'

Markku

Offline

Board footer

Powered by FluxBB