Page 1 of 1

How to call linux bash from micropython

Posted: Tue Nov 22, 2016 4:07 am
by skylin008
Hello,every one! I want to using efax software,How to call efax command line from micropython.Thank you very much!

Re: How to call linux bash from micropython

Posted: Tue Nov 22, 2016 7:17 am
by pythoncoder

Code: Select all

>>> import uos
>>> uos.system('ls -l')
total 40
-rw-r--r-- 1 adminpete adminpete 1006 Feb 20  2016 ffilib.py
drwxr-xr-x 2 adminpete adminpete 4096 Aug  7  2015 json
-rw-r--r-- 1 adminpete adminpete  196 Jul 29 11:27 pickle.py
drwxr-xr-x 2 adminpete adminpete 4096 Oct 13 07:18 __pycache__
-rw-r--r-- 1 adminpete adminpete   76 Oct 24 07:12 rats1.py
-rw-r--r-- 1 adminpete adminpete   95 Oct 24 07:14 rats2.py
-rw-r--r-- 1 adminpete adminpete   67 Oct 24 07:16 rats.py
-rw-r--r-- 1 adminpete adminpete 4886 Aug  7  2015 re.py
lrwxrwxrwx 1 adminpete adminpete   65 May 15  2016 upython -> /mnt/qnap2/data/Projects/MicroPython/micropython/unix/micropython
0

Re: How to call linux bash from micropython

Posted: Tue Nov 22, 2016 7:27 am
by skylin008
Thanks pythoncoder support.I will be try and check it out!

Re: How to call linux bash from micropython

Posted: Tue Nov 22, 2016 8:55 am
by skylin008
Hello pythoncoder,
when I run the uos.system function,the error logs as follow:

>>> import os
>>> import uos
>>> uos.system()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'system'


How to solve this issue,Thanks!

Re: How to call linux bash from micropython

Posted: Tue Nov 22, 2016 9:36 am
by platforma
The module is called "uos". You need to pass the command you're trying to call to the system() function.

Code: Select all

>>> import uos
>>> uos.system('uname -a')
Linux arch 4.7.6-1-ARCH #1 SMP PREEMPT Fri Sep 30 19:28:42 CEST 2016 x86_64 GNU/Linux
Are you, perhaps, trying to run this on pyboard?

Re: How to call linux bash from micropython

Posted: Tue Nov 22, 2016 5:28 pm
by pythoncoder
@platforma Good point. I did assume that the original post referred to the Unix build owing to the reference to Bash ;)

@skylin008 If you're running MicroPython on a Pyboard, ESP8266 or other hardware, you can't access a Bash prompt because these boards don't run Linux. They run MicroPython directly on the microprocessor; there is no operating system and no Bash shell. It will produce the error message you're seeing.

Re: How to call linux bash from micropython

Posted: Thu Dec 08, 2016 7:24 pm
by torwag
I guess what he wanted to do is to call a bash script on the host pc triggered by an event within e.g. a pyboard.
Easiest to to that would be to write a small python script for the host machine, which communicates via the virtual serial port with the pyboard. As soon as the pyboard sends a command, the script will execute the commands like shown in Peters example.