Install Hooks Driving me Insane

I have an install hook on my snap, This builds correctly and the install file is set to execute
the top line #! /bin/sh -e which on installation on the device throws an install hook error

  • Run install hook of “hark-iot-edge” snap if present (run hook “install”: /bin/sh: 0: Illegal option -)

I don’t understand what is the problem as the shell script can run on device. and if i remove this header I get exec errors

run hook “install”: cannot snap-exec: exec format error

can you share the snap with us?

if the snap is

on edge, try fixing the file so it doesn’t have DOS line endings, some shells are picky about that.
Also, make sure the last line also ends with a newline, for the same reason.

In general I’d recommend running shellcheck (from the snap, for example) on any shell scripts as part of your test suite.

1 Like
$ shellcheck meta/hooks/install

In meta/hooks/install line 1:
#!/bin/sh -e
            ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 2:

^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 3:
# get the hostname to set that in the config file as well
                                                         ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 4:
HOSTNAME=$(hostname)
                    ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 5:

^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 6:
mkdir -p "$SNAP_COMMON/etc/iotedge"
                                   ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 7:
mkdir -p "$SNAP_COMMON/var/lib/iotedge"
                                       ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 8:
mkdir -p "$SNAP_COMMON/run"
                           ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 9:

^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 10:
cat "$SNAP/etc/iotedge/config.yaml" | \
    ^-----------------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.


In meta/hooks/install line 12:
    -e "s@management_uri: \"unix:///var/run/iotedge/mgmt.sock\"@management_uri: \"unix://$SNAP_COMMON/run/mgmt.sock\"@" \
    ^-- SC2215: This flag is used as a command name. Bad line break or missing [ .. ]?


In meta/hooks/install line 13:
    -e "s@workload_uri: \"unix:///var/run/iotedge/workload.sock\"@workload_uri: \"unix://$SNAP_COMMON/run/workload.sock\"@" \
    ^-- SC2215: This flag is used as a command name. Bad line break or missing [ .. ]?


In meta/hooks/install line 14:
    -e "s@management_uri: \"fd://iotedge.mgmt.socket\"@management_uri: \"unix://$SNAP_COMMON/run/mgmt.sock\"@" \
    ^-- SC2215: This flag is used as a command name. Bad line break or missing [ .. ]?


In meta/hooks/install line 15:
    -e "s@workload_uri: \"fd://iotedge.socket\"@workload_uri: \"unix://$SNAP_COMMON/run/workload.sock\"@" \
    ^-- SC2215: This flag is used as a command name. Bad line break or missing [ .. ]?


In meta/hooks/install line 16:
    -e "s@hostname: \"<ADD HOSTNAME HERE>\"@hostname: \"$HOSTNAME\"@" \
    ^-- SC2215: This flag is used as a command name. Bad line break or missing [ .. ]?


In meta/hooks/install line 17:
    > "$SNAP_COMMON/etc/iotedge/config.yaml"
    ^-- SC2188: This redirection doesn't have a command. Move to its command (or use 'true' as no-op).
                                            ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 18:

^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 19:
# also disable the service before installing so that it doesn't cause the
                                                                         ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .


In meta/hooks/install line 20:
# installation to be aborted
                            ^-- SC1017: Literal carriage return. Run script through tr -d '\r' .

For more information:
  https://www.shellcheck.net/wiki/SC1017 -- Literal carriage return. Run scri...
  https://www.shellcheck.net/wiki/SC2188 -- This redirection doesn't have a c...
  https://www.shellcheck.net/wiki/SC2215 -- This flag is used as a command na...
1 Like

Once I get rid of the bad line endings,

$ shellcheck meta/hooks/install  

In meta/hooks/install line 10:
cat "$SNAP/etc/iotedge/config.yaml" | \
    ^-----------------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

For more information:
  https://www.shellcheck.net/wiki/SC2002 -- Useless cat. Consider 'cmd < file...
3 Likes

Okay thanks. I had an incline that it could be line endings but got sidetracked and forgot :grin:

1 Like

Yes this worked a treat. I knew it would have been something silly. Big Help!!!

1 Like

In case someone else wanders to this thread like I did and finds out like me that shellcheck doesn’t solve their problem, I needed to add a newline at the end of my script. The text editor I was using didn’t do it automatically, and shellcheck didn’t flag it, but I still got cannot snap-exec: exec format error when the hook ran. I finally figured out it was the missing newline at the end that was causing the problem.