1

I have a text file, mac.txt, with one line that looks like this:

4a:38:61:e1:71:7c

... and I have a bash script, mask.sh, that simply changes my MAC address:

#!/bin/bash

sudo ifconfig wlan0 down
sudo macchanger -m 16:dx:0b:rc:4a:32 wlan0
sudo ifconfig wlan0 up

And I'm trying to figure out how to write a bash script that will copy the MAC address from mac.txt and replace it with the MAC address in mask.sh, so that it looks like this:

#!/bin/bash

sudo ifconfig wlan0 down
sudo macchanger -m 4a:38:61:e1:71:7c wlan0
sudo ifconfig wlan0 up

I've tried a couple variations with sed, cut, and paste but am too embarrassed to show what I'd tried. I'm still pretty new to this.

4
  • Welcome! I would say that a simple sudo macchanger -m $(cat mac.txt) wlan0 will work. Commented Mar 12, 2020 at 23:35
  • Wow! I was way overthinking this. Thank you, sir. That does exactly what I need it to! Commented Mar 12, 2020 at 23:57
  • 1
    ... or even $(< mac.txt). See for example Understanding Bash's Read-a-File Command Substitution Commented Mar 13, 2020 at 0:44
  • Thanks! Checking that out now! Commented Mar 13, 2020 at 0:51

1 Answer 1

1

As schrodigerscatcuriosity commented, you could use bash's command substitution:

sudo macchanger -m $(cat mac.txt) wlan0

or, since you're just using cat on a file:

sudo macchanger -m $(< mac.txt) wlan0

These are explained in the bash manual under Command Substitution.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.