Script doesn't work, need help

Hi everyone. I’m not much experienced with scripting, but did try to write a simple one, without luck yet. So here is the story. I’m trying run a virtualbox guest os on startup. However it doesn’t start because of some wiered network bridge problem. But I’ve found out, that if I open VirtualBox and apply again the current active nic, it does start. For some strange reason the nic name is changing almost every time on boot up. So what I’m trying to do with my script it is: find out the currently active nic name, for instance eth21, apply this settings to the VB guest machine and fire it up. Here is my script

#!/bin/sh
sleep 60
date  >> xp_started
str1=$(ifconfig |grep eth)
spacepos=$(echo `expr index "$str" " "`)
str2=${str1:0:$spacepos}
VBoxManage modifyvm xp --bridgeadapter1 $str2
VBoxManage startvm xp --type headless

If I execute each of the command in the terminal it looks ok, however running the script via sh -x scriptname tells me that spacepos equals to 0 and therfore it can not proceed to copy the string in next command. Please help, thanks

Solved :slight_smile: this is the working script

#!/bin/bash
sleep 60
date  >> xp_started
str1=$(ifconfig |grep eth)
spacepos=$(echo `expr index "$str1" " "`)
str2=`expr substr "$str1" 1 $spacepos`
VBoxManage modifyvm xp --bridgeadapter1 $str2
VBoxManage startvm xp --type headless

Nice you found it (I didn’t).

One question (no need to answer me). You seem to know the $( … ) construct. Why then do you use the old fashioned, only there for compatability ... around the expr statement?

Good for you.

However, two things:

Eth should not keep changing numbers. Something is wrong with your setup.

And you should learn to use cut or awk to get the first field out of the output of ifconfig. E.g.

ifconfig | awk '/eth/{print $1}'

Far simpler than messing around with string offsets.

One question (no need to answer me). You seem to know the $( … ) construct. Why then do you use the old fashioned, only there for compatability ... around the expr statement?
I don’t know much about scripting yet :shame:, that’s the best I could write with help of “google friend”

Eth should not keep changing numbers. Something is wrong with your setup.

Probably there is something wrong, I remember it all started when I wanted to have static ip instead of dhcp , anyway as far as it works, I won’t try to fix it :slight_smile:

And you should learn to use cut or awk to get the first field out of the output of ifconfig. E.g.

uf, that really looks much easier, I’ll remember this one, thanks.

And without awk (to avoid the execution of an extra process):

ifconfig | grep eth | read ethinterface skip

And changing this one

spacepos=$(echo `expr index "$str" " "`)

into

spacepos=$(echo $(expr index "$str" " "))

is what I mean. The $( … ) is a replacment (since more then 20 years) for .... Much easier to read, can be nested (as you see). The mixture of both is curious to see.

Learning scripting: never a dull moment :slight_smile:

thanks for you help mates, I appreciate it very much :slight_smile:

Ah, but the awk does the “grep” also, see the /eth/ in front of code block?

You are right. Now the discussion ends in: what is worse, awk or grep? lol!

Sed should also be considered.

Hi,

I would use AWK, it’s more concise and in my opinion easier to read, this makes it less prone to mistakes, and as we all know, once the programs/scripts get less trivial, the fewer potential mistakes, the better.

I only really use SED if I want to store the output in a file, maybe I should spend some time learning more about it.

Regards,
Barry.

Actually the sed command isn’t that much different from awk in this case:

ifconfig | sed -n '/eth/s/ .*//p'

I have an old saying: There are always at least 50 ways to to something in Unix, of which at least 20 are not realy bad ones.

This was one of the legendary feats I read of when I was a rookie Unix person. It’s still amazing after all those years.

http://www.ee.ryerson.ca/~elf/hack/recovery.html

O yes, thank you for this. They did a really good job. And it is really nicely told. I do feel the stress while reading.

The thing is, that after such an exersize everybody is realy satisfied (and rightly so) with what is achieved and the wonderfull cooperation that goes with it. But it is always very difficult to recap what excact steps were taken during that night (such things usualy happen at the darkest hours).

Yes I love it, remembers me of some difficult, but wonderfull recoveries in my own carreer.

Yes, Linux users have it so much easier now, rescue CDs, extra machines to move the disk to, etc. None of this palaver with typing in hex code. But those three words must never be forgotten: backup, backup, backup.

And a recovery strategy. When they have a backup, they do not know how to use it lol!