title: Cannot execute script in Termux that requires SU permissions
body: <p>So I don't usually post, instead spending hours trawling forums looking for an answer - but I'm at a complete loss.</p>
  
  <p>What I want to do:</p>
  
  <p>Quite simple really, I want a script that will automatically change the MAC address on my OnePlus 5, I already have all the commands I need to do this and can easily accomplish it <em>manually</em>, getting a script to do it however? Oh boy...</p>
  
  <p>What I have tried:</p>
  
  <p>These are the commands that, when run in termux manually, give me the result I need:</p>
  
  <pre><code>ip link set dev wlan0 down
  
  macchanger --random wlan0
  
  ip link set dev wlan0 up
  </code></pre>
  
  <p>Now the reason I don't use ip link set dev wlan0 address xx:xx:xx:xx:xx:xx is because then I have to enter an address, whereas macchanger can generate this automatically. (I don't have the Unix-foo to generate a MAC address programmatically).</p>
  
  <p>So I did the following, in chronological order:</p>
  
  <p>1) Created a script in nano with these commands.</p>
  
  <p>2) Made the script executable using chmod +x changeMAC.sh</p>
  
  <p>This gives an "Access Denied" error message when executed.</p>
  
  <p>3) Tried first going into SU, then executing the script, this gives a "macchanger" not found error as termux loses the environment when su is invoked. So I installed the tsu wrapper which allows you to call tsu and get su with the environment maintained, equivalent to su --preserve-environment.</p>
  
  <p>However this is where I started banging my head off the desk.</p>
  
  <p>If I do the following in the termux terminal:</p>
  
  <pre><code>tsu
  ./changeMAC.sh
  </code></pre>
  
  <p>The terminal just sits there, doing nothing. If I then type "exit" and hit return I get three "Access Denied" error messages.</p>
  
  <p>I've tried to circumvent the error:</p>
  
  <p>4) Using AutoInput to type into termux like a user but it refused to do so.</p>
  
  <p>5) Executing the shell commands directly from Tasker > Run Shell but I suspect this isn't working because the macchanger package isn't installed in the scope of the tasker shell.</p>
  
  <p>6) Using su --preserve-environment directly, but the same thing happens!</p>
  
  <p>So to summarise my script looks like this:</p>
  
  <pre><code>\#!/data/data/com.termux/files/usr/bin/sh
  
  sleep 1
  
  ip link set dev wlan0 down
  
  sleep 1
  
  macchanger --random wlan0
  
  sleep 1
  
  ip link set dev wlan0 up
  
  exit
  </code></pre>
  
  <p>but:</p>
  
  <p>1) Won't work with standard privaleges</p>
  
  <p>2) Doesn't appear to execute when tsu/su --preserve-environment is used, giving errors when the tsu instance is exited out of instead, almost asthough its <em>waiting</em> for the instance to end before executing...</p>
  
  <p>3) Works fine when executed manually from a tsu instance.</p>
  
  <p>I think I just do not know enough about the intricacies of Unix/Linux/Android to understand <em>why</em> this isn't working and therefore correct it.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>Best,</p>
  
  <p>H.</p>
  
  <p>Phone Details:</p>
  
  <p>OnePlus 5, rooted with Magisk</p>
  
  <p>ElementalX Kernel</p>
  
  <p>xXx NoLimits Custom ROM</p>
  
  <p>Edit 1:</p>
  
  <p>I have tried 'sudo' from <a href="https://gitlab.com/st42/termux-sudo" rel="nofollow noreferrer">https://gitlab.com/st42/termux-sudo</a> however this requires su, which then can't find macchanger and I'm back to square one.</p>