Help with shell command construction

Hi,
After considerable experimentation I managed to get the following shell commands to work. In my configuration.yaml file I have:

shell_command:
  removevideos: 'find /config/www/videos/* -mtime +4 -exec rm {} \;'
  removesnapshots: 'find /config/www/snapshots/* -mtime +7 -exec rm {} \;'

I run this with a service call at 02:00 each morning and it removes the files I want it to.
You have no idea how long it took me to write this - but it’s a learning curve :slight_smile:

Now I want to do more complex tasks: identify, copy and rename files for example and I’m a bit stuck.
I am aware of the 60 second run-time limit, and I’ve read the documentation I can find.
So my questions are:
“Can I use any bash commands in these shell scripts?”
“How can I spread these scripts over multiple lines?”
“Where can I find a good primer for shell scripting that would help me?”

Many thanks
John

You should write a bash script to do those “identify, copy and rename” and add that to the shell_command section.

Here is an example which works for me:

# This command drops Linux buffers/cache and puts memory stats
# before and after the drop into a service response variable
shell_command:
  drop_caches: |-
    jfree() {
      free -m | grep Mem: | cut -d" " -f2- | jq -sc '[ (["total","used","free","shared","buff_cache","available"], . ) ] | transpose | map({(.[0]): .[1]}) | add'
    }
    before=$(jfree)
    sync
    echo 1 >/proc/sys/vm/drop_caches
    after=$(jfree)
    echo "${before}${after}" | jq -c '{before: ., after: input}'

# This script drops caches and shows buffer memory drop
script:
  shell_test:
    sequence:
      - service: shell_command.drop_caches
        response_variable: response
      - service: persistent_notification.create
        data:
          message: |
            {% set j = response.stdout | from_json %}
            {% set bb = j.before.buff_cache %}
            {% set ba = j.after.buff_cache %}
            Buffers memory dropped from {{bb}} to {{ba}}

The 1st script besides of dropping caches produces the following response which can be captured and used by a caller (actually, it is an object with 3 keys, where the first two are strings):

stdout: >-
  {"before":{"total":5930,"used":782,"free":4235,"shared":5,"buff_cache":912,"available":5040},"after":{"total":5930,"used":779,"free":4422,"shared":5,"buff_cache":729,"available":5043}}
stderr: ""
returncode: 0

The 2nd script calls this one and prints a message, converting the string response into json object and taking some keys from it to print:

Buffers memory dropped from 909 to 729

Hope it helps.