# Need ifconfig lo create name to fail instead of incrementing lo



## rcmcdonald (Apr 12, 2021)

I'm running


```
ifconfig lo create name myrandomname
```

This works fine. However, if myrandomname already exists, ifconfig will still create a lo interface by just incrementing lo1 to lo2. I need it to fail.


----------



## SirDice (Apr 12, 2021)

Don't do the creation and renaming in one go. 

```
create  Create the specified network pseudo-device.  If the interface is
             given without a unit number, try to create a new device with an
             arbitrary unit number.  If creation of an arbitrary device is
             successful, the new device name is printed to standard output
             unless the interface is renamed or destroyed in the same ifconfig
             invocation.
```


----------



## rcmcdonald (Apr 12, 2021)

So I guess I have to


```
ifconfig lo create
```

then


```
ifconfig lo1 name MYNAME
```

and then if that fails


```
ifconfig lo1 destroy
```


----------



## SirDice (Apr 13, 2021)

How about testing if the interface exists and creating it if it doesn't?


----------



## rcmcdonald (Apr 13, 2021)

Yea, I'm terrible at bash. I know if I run


```
ifconfig lo create MYNAME
```
 and it fails i.e. $?<>0 I can do something like this:


```
ifconfig lo create name MYNAME || .......
```
 is there anyway to use that logical operator and grab the output of the create to turn around and destroy it?



Edit:

So I'm looking for a one-liner:

The only solution I've got is


```
ifconfig lo create name MYNAME > /tmp/error || ifconfig `cat /tmp/error` destroy
```


----------



## SirDice (Apr 13, 2021)

vbman213 said:


> I'm terrible at bash.


It's a shell script. Use sh(1), not bash. And we all had to start at the beginning. This site is always useful: https://www.grymoire.com/Unix/Sh.html

I'll give you a hint, look at the output of `ifconfig -l`, parse the output and if you can't find the interface you're looking for then create it.

[Moved the thread to "Userland Programming and scripting" because this is more related to scripting than networking.]


----------



## rcmcdonald (Apr 13, 2021)

Cheers! Thanks


----------



## olli@ (Apr 13, 2021)

I would check upfront if an interface with that name already exists, and create it only when needed.
There are several ways to do it …

```
if ! ifconfig myname >/dev/null 2>&1; then
        ifconfig lo create name myname
fi
```
or if you prefer a one-liner (I think this is uglier, though):

```
ifconfig myname >/dev/null 2>&1 || ifconfig lo create name myname
```
You can omit the redirections (`>/dev/null 2>&1`) if you don’t mind the output from the first ifconfig command.


----------



## rcmcdonald (Apr 13, 2021)

Ambiguous output redirect on the one-liner.


----------



## SirDice (Apr 13, 2021)

vbman213 said:


> Ambiguous output redirect on the one-liner.


Don't try to use bourne shell redirects on a C shell.


----------



## rcmcdonald (Apr 13, 2021)

wow learned something today, thanks


----------

