File Write Script

This example writes to a file using the FileIO classes into the Yún's filesystem. A shell script file is created in /tmp, and is executed afterwards.

Hardware Required

  • Arduino Yún


There is no circuit for this example.

image developed using Fritzing. For more circuit examples, see the Fritzing project page


Include the FileIO header, for communicating with the filesystem.
#include <FileIO.h>

In setup(), initialize Bridge, Serial communication, and FileSystem. Wait for an active serial connection, and call a custom function uploadScript() which will upload your file, before starting loop().

void setup() {

  while(!Serial);  // wait for Serial port to connect.
  Serial.println("File Write Script example\n\n");



loop() will just execute your script every 5 seconds by calling another custom function, runScript().

void loop() {

Your uploadScript() function will create a shell script in the Linux file system that will check the network traffic of the WiFi interface. Create the file and open it by creating an instance of the File@ class, and calling indicating where you would like to create the script. You'll store the script in "/tmp", which resides in RAM, to preserve the limited number of FLASH memory read/write cycles.

void uploadScript() {
  File script ="/tmp/", FILE_WRITE);

Write the contents of the script to the file with File.print(). begin by printing the header, "#!/bin/s", then the utility ifconfig. @ifconfig@ is a command line utility for controlling network interfaces. you'll be looking at the WiFi interface, which is referred to as "wlan0". The utility grep will search the output of ifconfig. You're looking for the number of bytes received , so search for the keywords "RX bytes" and close the file.

  script.print("ifconfig wlan0 | grep 'RX bytes'\n");
  script.close();  // close the file

Instantiate a Process to make the script executable. chmod@ is a command that will change the modes of files. By sending the chmod@@ command and filepath, you can make your shell script run like an application.

Process chmod;
  chmod.begin("chmod");      // chmod: change mode
  chmod.addParameter("+x");  // x stays for executable

The runScript() function will create a Process that runs the script and prints the results to the Serial Monitor. Create a named Process, and start your script by calling Process.begin(filepath) and

void runScript() {
  Process myscript;

Create a string to hold the output, and read the output into it

String output = "";

  while (myscript.available()) {
    output += (char);

Remove the blank spaces at the beginning and end of the string and print it to the serial monitor :


The complete sketch is below :

SORRY, There is an error at our code repository, please inform to

See Also

  • Bridge Library - Your reference to the Bridge Library
  • Bridge – Simple REST style calls to access analog and digital pins
  • Consolle Ascii Table – A complete ASCII table printed to the Console
  • Console Pixel – Turn an LED on and off through the Console
  • Console Read - Read data coming from bridge using the function
  • Data Logger - Log data from three analog sensors to an SD card.
  • Http Client - A basic HTTP client that connects to the internet and downloads content.
  • Mailbox Read Message - How to read the messages queue, called Mailbox, using the Bridge library.
  • Process - How to run linux processes using an Arduino Yún.
  • Shell Commands - How to run linux shell commands using an Arduino Yún.
  • Temperature Web Panel - How to serve data from an analog input via the Arduino Yún's built-in webserver.
  • Time check - Gets the time from Linux via Bridge then parses out hours, minutes and seconds.
  • WiFi Status - Prints information about the status of your wifi connection.
  • Serial Terminal - Use the Yún's 32U4 processor as a serial terminal for the Linux side on the Yún.

Last revision 2015/08/11 by SM