How to run BASH script in my Android?
Asked Answered
N

7

24

My same BASH script is working in Fedora/CentOS.

But I am testing one Android eee pad transformer. enter image description here

Where i have terminal access and i wrote a small test script. But its not working, how can i fix it? what am i doing wrong?

/data/data/berserker.android.apps.sshdroid/home $ cat test.sh 
#!/bin/bash
var=`ifconfig -a`;
echo $var;

/data/data/berserker.android.apps.sshdroid/home $ chmod +x test.sh 
/data/data/berserker.android.apps.sshdroid/home $ ./test.sh 
sh: ./test.sh: not found
/data/data/berserker.android.apps.sshdroid/home $ uname -a
Linux localhost 2.6.36.3-00004-g069b8b5 #1 SMP PREEMPT Wed May 11 22:14:22 CST 2011 armv7l GNU/Linux

/data/data/berserker.android.apps.sshdroid/home $ bash ./test.sh 
sh: bash: Permission denied

/data/data/berserker.android.apps.sshdroid/home $ ls -l /bin/bash
ls: /bin/bash: No such file or directory

/data/data/berserker.android.apps.sshdroid/home $ find / -name "bash"
find: /config: Permission denied
lots more.......
find: /proc/595/task/598/fd: Permission denied
......
find: /data: Permission denied
find: /root: Permission denied

Follow up:

This is my script now which works:

#!/bin/sh
echo "hello wassup, run me simply as './test.sh'";

or

#!/bin/bash
echo "hello wassup, run me using 'sh ./test.sh'";
Nu answered 11/11, 2011 at 13:20 Comment(4)
@Ocaso Protal: see above, its all transparently pasted.Nu
I'm far way of being an andriod expert, but this could be a problem with memory foot print and the use of a tiny shell to save space on flash an ram. What does ls -l /bin/bash show? Is it a symlink? If yes, what is the destination of the symlink? I think the /bin/bash is symlink to a shell, which isn't capable of beeing fully bash compliant.Dominickdominie
have you tried a simple 'sh' script? Instead of /bin/bash use /bin/sh - and see what happens. It's possible that android simply don't have a proper implementation of bash.Gossoon
chmod doesn't accept +x on some Android distros . Try chmod 777.Roa
G
11

May be it will work when calling interpreter with a script?

$ bash ./test.sh

I saw, that although it is specified #!/bin/bash error was posted by sh - may be it do wrong.

UPD

$ sh ./test.sh
Godber answered 11/11, 2011 at 13:30 Comment(4)
it seems, that there is no bash interpreter, and sh is used by default. Try please to use it: $ sh ./test.sh . Also, shell interpreter you work in may have no such functionality as it is in bash.Godber
But i want to use BASH. Like the way i use in Fedora/Centos.Nu
Try to find it, for example, with $ which bashGodber
thanks i am temporary using your way $sh ./test.sh or i can also change the line above to /bin/sh that way at-least working. But its very disturbing how Android it needs to be.Nu
P
32

in Android the shell is located in /system/bin/sh not /bin/sh like it is on most Unix-like systems. So even if you change #!/bin/bash to #!/bin/sh it will still not work. you'll have to use #!/system/bin/sh

Android is not a GNU/Linux distribution so you can't expect that all scripts that run on GNU/Linux to also work on Android.

Permanence answered 11/11, 2011 at 14:36 Comment(3)
This information is not correct. I simply modified the script to #!/bin/sh and it works.Nu
for the moment i am using it with #!/bin/sh and its working. But if do $sh ./test.sh as mentioned by @Asten that also works.Nu
I tried #!/bin/sh but my first command su didn't work. Modified to #!/system/bin/sh and viola, I got the root authorization prompt!Barghest
G
11

May be it will work when calling interpreter with a script?

$ bash ./test.sh

I saw, that although it is specified #!/bin/bash error was posted by sh - may be it do wrong.

UPD

$ sh ./test.sh
Godber answered 11/11, 2011 at 13:30 Comment(4)
it seems, that there is no bash interpreter, and sh is used by default. Try please to use it: $ sh ./test.sh . Also, shell interpreter you work in may have no such functionality as it is in bash.Godber
But i want to use BASH. Like the way i use in Fedora/Centos.Nu
Try to find it, for example, with $ which bashGodber
thanks i am temporary using your way $sh ./test.sh or i can also change the line above to /bin/sh that way at-least working. But its very disturbing how Android it needs to be.Nu
W
6

Most Android devices don't have a bash interpreter installed. If you really need to run the script across Linux and Android, you could try using BusyBox but that will require rooting your device (and potentially voiding your warranty). Even then though, I don't know if the ifconfig utility is included in BusyBox.

I would strongly recommend using the Android SDK to write an app to do whatever your trying to accomplish.

Wingless answered 11/11, 2011 at 14:25 Comment(2)
Do you mean, use Android SDK in my Fedora/CentOS? to remote Android.Nu
No, I mean use the bash script on Fedora/CentOS to do whatever your doing. Use Java\C\C++ with the Android SDK to replicate that functionality on Android.Wingless
D
3

As was stated, the Android OS (up to and including 4.0) does not include the BASH interpreter (just shell). While BusyBox is a great tool, I believe it's only a single executable that combines stripped-down-functionality-for-size versions of common UNIX utilities, but doesn't actually include the BASH interpreter.

For an Android compiled version of the BASH interpreter, refer to this Forum thread: http://forum.xda-developers.com/showthread.php?t=537827

Discursive answered 3/1, 2012 at 19:32 Comment(0)
R
1

You can install Busybox, which provides you with many utilities such as awk, file, etc... and Terminal Emulator.

  1. Create a shell file with #!/system/bin/sh as the first line (shebang)
  2. Now place the completed script under /system/xbin or /system/bin and run it from the Terminal Emulator

The information is an excerpt from this article : HOW TO RUN SHELL SCRIPTS ON ANDROID DEVICES

Roberts answered 19/4, 2016 at 10:44 Comment(0)
H
0

Making the bash script run is the least of it. The problem is how to do it without opening some terminal emulator and typing the path to the script. The only way is with TCL/TK:

1-) Download and install AndroWish at https://www.androwish.org/home/home
2-) Install Busybox from Google Store

Write a launcher:

launcher.tcl

#!/usr/bin/wish
exec sh /storage/sdcard0/yourscript.sh
exit 0

yourscript.sh

#!/system/bin/sh
[...]

Add a desktop shortcut to the AndroWish terminal by typing: borg shortcut add "MyScript" file://mnt/sdcard/launcher.tcl

Haplography answered 4/4, 2023 at 16:39 Comment(2)
Seems like a lot of effort to install TCL/TK just to run a shell script.Incoherent
This basically depends on how often you need to run the same script per day, if it can be done manually, so be it. But AndroWish is an APK.Haplography
T
0

Termux can be used. Termux is terminal emulator and Linux environment for Android device.

Triad answered 23/3 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.