Bash function, for BSD


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash function, for BSD
# 1  
Old 11-07-2015
Bash function, for BSD

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.

Code:
#!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!
# 2  
Old 11-07-2015
Quote:
Originally Posted by 1in10
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.

Code:
#!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):
Code:
#!/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

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-07-2015
allright, message understood.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies

2. Shell Programming and Scripting

Yes or No selection within bash function

I need to add a selection within the bash function below and am having some trouble doing so. phox2b() { printf "\n\n" printf "What is the id of the patient getting Phox2B analysis : "; read id printf "Is this an intronic variant? Y/N "; read match_choice case... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. UNIX for Dummies Questions & Answers

Bash INKEY$ function...

This is probably common knowledge to the professionals but not so much for amateurs like myself. This is a code snippet for the equivalent of BASIC's... LET char$=INKEY$ As the timeout parameter cannot be less than 1 second then this is the only limitation... It is a single line... (5 Replies)
Discussion started by: wisecracker
5 Replies

4. Shell Programming and Scripting

Bash function

startvm() { startguest } Is there a way use one line to get this ? actually I want startvm=startguest (5 Replies)
Discussion started by: yanglei_fage
5 Replies

5. Shell Programming and Scripting

BASH function error

Hey everyone. I am currently testing my first function based BASH script. The ultimate goal is going to be moving logs from point A to point B (or if B is down, to point C). Part of this involves the following function: function testAlive{ ping -c 1 -q $1 } Now when I run ping -c... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

bash search function

I want to have a function with a similar interface: search *.cpp asdf that will search recursively all directories for *.cpp file, containing line 'asdf' inside. I tried this: function search { find . -name "$1" | xargs grep -li $2; } But it doesn't work all the time. For example, if i run it... (3 Replies)
Discussion started by: doze
3 Replies

7. Shell Programming and Scripting

Need help in using power function in Bash

Hi, I would like to use a power function in Bash in an awk '{ } . e.g pow(2,3)=8 Any suggestion? I try to find online resources but most of them stated by using BC. Please advise. Thanks. (3 Replies)
Discussion started by: ahjiefreak
3 Replies

8. Shell Programming and Scripting

Need assistance with bash function

Hi all! I need a little help with an imbedded fuction I am trying to write. What I am trying to do is go to a series of hosts and get the contents of an XML file, storing some of the data, along with the name of the host it was gathered from. My intent is to store this in an array for later... (1 Reply)
Discussion started by: _jade_
1 Replies

9. BSD

for linux and BSD users interested in Unix system V/bsd

for all you unix/linux interested heres an online book for free that covers the basics of BSD SysV Unix commands and applications . giving the average linux user a perspective on the differences in context of the two operating systems and for BSD users covers material as a refernce guide. ... (0 Replies)
Discussion started by: moxxx68
0 Replies

10. BSD

BSD, Bash and Shells?

When I use Mac OS X's Terminal the UI is some what easier than that of Linux... I this just a shell or something because using Bash is a pain in RH's Linux 9. It's so sensitive about case etc. ??? In that way what is the shell that OS X uses as it's default Bash is on OS X (OK Duh) and... (3 Replies)
Discussion started by: RedVenim
3 Replies
Login or Register to Ask a Question