### Run Bash Script

An example of running a Bash Script on a Raspberry Pi using the jShell Library.
Bash Script rpiinfo.sh
#/bin/bash
#Note:
#do not forget to x after change: sudo chmod a+x rpiinfo.sh. Then run ./rpiinfo.sh (to test prior using jShell Library)
#Get CPU Temperature - divide by 1000 in the B4J application
cat /sys/class/thermal/thermal_zone0/temp > rpicputemp.inf
#Get Disc space used
df -H | grep 'root' | awk '{ print $5 }' > rpidiscspaceused.inf
#Get RAM used
free -o -h | head -n2 | tail -n1 | awk '{print $3}' | tr -d [=M=] > rpimemused.inf
#Get RAM free
free -o -h | head -n2 | tail -n1 | awk '{print $4}' | tr -d [=M=] > rpimemfree.inf
B4J Code Snippet
'Get Raspberry Pi Information via Bash Script.
'The Bash Scripts creates several information files (text format, extension .inf)
`Sub GetRasPiInfo
Try
shl.Initialize("shl", "./rpiinfo.sh", Null)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
Catch
Log(LastException.Message)
End Try
End Sub`
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
'Process successfully completed
'As an example start MQTT and update the RasPi information in Domoticz after reading the Bash Script output files.
If Success And ExitCode = 0 Then
Log("Shell Command Successfull: MQTT connection initiated")
'DOMOTICZ - Init mqttDomoticz with connection options
mqttDomoticz_Connect(mqttclientId)
Else
Log("Shell Command Error: " & StdErr)
End If
End Sub`

Changed: 2018-03-26 13:10, Category: Raspberry Pi Sample Projects, File: T6C238M1744.htm, ID: 1744