I am putting this thread to shell-threads, because it is about how to make a function work properly. I need a hint for declaring a function right, it has been more than a year I did not work that straight with bash.
So my aim is to turn off the eth0 (as it would be in linux, and bge0 in bsd ), next step to generate a random mac-address as a function, turn on the bge0 device with the newly set mac-address.
My first attempt looks like this. The nasty looking sed-part is working fine.
#!bin/bash
# declare function for random mac in bsd
# turn off the ethernet device, create a random mac,
#turn on the ethernet device again
randmac() {
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
exit 1
}
ifconfig bge0 down
ifconfig bge0 hw ether $randmac
ifconfig bge0 up
exit;
May someone can give me the decisive hint, thanks in advance. Or may someone can tell me how to
install macchanger, which I use on linux. thanks!
Having a utility (or a shell function) exit with a non-zero value conventionally means that the script failed. And exit in a function definition exits the containing script; not just the function. (To leave a function and return an exit status from that function to the invoking shell script, use return instead of exit ). But, the more normal case with what you're doing would be to just fall off the end of the function which will return the exit status of the last command executed by the function.
And, you treat a function as a command; not as a variable. So, try (although I have not tested this):
#!/bin/bash
# declare function for random mac in bsd
randmac() {
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
}
# turn off the ethernet device, create a random mac,
# turn on the ethernet device again
ifconfig bge0 down
ifconfig bge0 hw ether $(randmac)
ifconfig bge0 up