Start bash from sketch (Linux)

Hello,
I would like to execute a bash file (compress.sh) from within a processing sketch. The bash file is stored in the same folder as the sketch. I tried exec("compress.sh");, but it did not work.
Can you help me?

How would you run ‘compress.sh’ if you were to do it from the command line? On a mac I would have to start with ‘/bin/sh’; may be different on linux; bottom line you need to know where bash lives. Then you usually have to enter the entire path of your ‘compress.sh’ file, eg sketchPath/compress.sh. So you would wind up with something like this: exec(“/bin/sh”,“pathToYourFile”). It can sometimes be pretty tricky to get just the right syntax, so you have to just keep experimenting until you get it. An alternative is to use Runtime.getRuntime().exec(cmd); where cmd is the string like above. This is usually done in a try…catch construct which will give you an error message in case it doesn’t work.

Reference:https://stackoverflow.com/questions/28012532/running-bash-script-from-processing

Hi @leclerke,

what exactly do the compress.sh ? I bet you can do and track it in your processing (java) code…
It is not recommended to execute such things like shell scripts automatically from code.
Imaging one change the content of the script to something harmful :slight_smile:

Cheers
— mnse

My sketch is recording data to a table which shall be available to my colleagues in a daily file. To realize this, I rename it with a timestamp like 240620_0918_ais.csv and copy the file to a shared one drive folder.
And the sketch runs on a remote place on a really old laptop that runs linux mint now (originally vsita!), so I transmit the data using an old smartphone. So I want to keep the volume of the data as small as possible. So I compress (zip) it before it is copied to the onedrive folder.
This is the bash. With “sh compress.sh” I can execute it from terminal:

#!/bin/bash
filename=$(date "+%g%m%d_%H%M%S_AIS_Record.7z")
source=/home/nb-ais/Schreibtisch/AIS_Recorder/ais_records/ais_record.csv
achive=/home/nb-ais/Schreibtisch/AIS_Recorder/ais_records/archive/$filename
onedrive=/home/nb-ais/OneDrive/AIS_Records_Mellum
7z a $achive $source
cp -r "$achive" "$onedrive"
onedrive --synchronize
#rm $quelle

exec(“/bin/sh”, “/home/nb-ais/Schreibtisch/AIS_Recorder/compress.sh”); did not work eigther.

The code in the reference should work; it does on my Linux system. For your problem Runtime.getRuntime().exec() is going to work better.

Thaks svan, I’ll try that tomorrow.

Ok, the example from refecence works for me too, but now I have a new problem:
I get errors: “7z not found”.
How do I have to call “7z” or the “onedrive” command within the bash so I can execute them.
I tried “/bin/7z” but didn’t work eigther.

I get this error message on my macos system:


You now may have more of a bash syntax question and I’m not sure I can help you with this.

A file with extension 7z is a compressed file:

You can’t execute it.

Do you want to open it to extract or view contents?

Other utilities may also handle a 7z.

I do not have a Linux box at the moment to test any of this.

:)

Try downloading and extracting 7z to your sketch folder. That should allow you to create a xxx.7z file without error, but you’ll still have the onedrive error to resolve. One possible way around this would be to use cp -r filePath folderPath and not use onedrive. This works on my Linux system and also on mac.

The thing is it works when I call the batch file from terminal, so it can’t be that wrong. The 7z line compresses the .csv-file into a folder.
I then also copy the file to a onedrive folder, which then gets synchonized by calling the onedrive-command. That also works within the bash file, when I execute it from a terminal window.
Called by the sketch the system seems not to be able to execute the installed programs like 7zip or onedrive. So properbly there has to be some additional information in the bash to execute these programs…

All the files that your process requires need to be in the sketch folder. That includes 7z so you will have to download and extract 7z to your sketch folder. I don’t think you can use a call to onedrive with this technique. The following pattern works for me on both Linux and MacOS:

#!/bin/bash
source=/path/to/sketch/data.csv
archive=/path/to/sketch/archive
./7zz a $archive $source
cp -r /path/to/sketch/archive.7z /path/to/folder/