5

Shellsheck is a static analysis tool for shell scripts which can be installed local on some Linux systems and can used without installation online for checking bash scripts for some errors.

Testing envirement:

  • Linux Mint (Ubuntu edition)
  • Given are a working bash script which which echo "I a echo from main file" and a source file which echo "I am a echo from source file".
  • Booth file are located in same folder.
  • tested with shellcheck 0.7.1-1, by local installed version.

main.sh

#!/bin/bash

source ./sourcefile.sh

echo "Output from main.sh"
echo
echo

fkt_output_from_sourcefile_sh
echo
echo

echo "For closing window, press Enter or Ctl + C"; read -r

sourcefile.sh

#!/bin/bash

fkt_output_from_sourcefile_sh() {
        echo "Output from sourcefile.sh"
    }

How did I run it on terminal:

shellcheck -x main.sh

Output on terminal (looks working fine):

Output from main.sh


Output from sourcefile.sh


For closing window, press Enter or Ctl + C

Error Message by check by shellcheck -x:

In /home/user/desktop/main.sh line 8:
    source ./sourcefile.sh
    ^-- SC1091: Not following: ./sourcefile.sh: openBinaryFile: does not exist (No such file or directory)

Possible solution (which did not work for me, probably depend on my wrong syntax):

Sample of not working solution:

Based on: "Tell ShellCheck where to find a sourced file (since 0.4.0):"

# shellcheck source=src/examples/config.sh
. "$(locate_config)"

Source:

main.sh

#!/bin/bash

    # shellcheck source=./sourcefile.sh
    source "$(find_install_dir)/sourcefile.sh"

    echo "Output from main.sh"
    echo
    echo

    fkt_output_from_sourcefile_sh
    echo
    echo

echo "For closing window, press Enter or Ctl + C"; read -r

sourcefile.sh :

#!/bin/bash

fkt_output_from_sourcefile_sh() {
        echo "Output from sourcefile.sh"
    }

Error message on terminal:

/home/user/desktop/main.sh: Line 4: find_install_dir: Command not found.
/home/user/desktop/main.sh: Line 4: /sourcefile.sh: File or folder not found
Output from main.sh


/home/user/desktop/main.sh: Line 10: fkt_output_from_sourcefile_sh: Command not found.


For closing window, press Enter or Ctl + C
user4601931
  • 4,982
  • 5
  • 30
  • 42
  • 3
    I checked this out, with the given information of `main.sh`, `sourcefile.sh` I ran `shellcheck -x main.sh` and it didn't give any warnings, also `./main.sh` ran smoothly. Maybe check installation? (my version: 0.7.2/pacman) – Mahdy Mirzade Aug 07 '21 at 09:02
  • 2
    Your example works for me under Windows with a Git Bash also. – Matthias Aug 07 '21 at 09:03
  • @Mahdy Mirzade, I replaced now my sample code by a on 3 PCs tested version. It can be my 1st sample code didnt show the error message on every systems. –  Aug 08 '21 at 10:14
  • @Matthias, I replaced now my sample code by a on 3 PCs tested version. It can be my 1st sample code didnt show the error message on every systems. –  Aug 08 '21 at 10:15

1 Answers1

3

I reviewed your situation better, and found this issue on shellcheck's github: https://github.com/koalaman/shellcheck/issues/1356 (This specific comment)

From what I understood, there are 2 solutions mentioned from developers:

1. Solving SC1091:
Because this error wasn't shown to me again with your edited code as well, and I don't see your shellcheck comment about SC1091 above source ./sourcefile.sh, I only can suggest you to add this comment in main.sh, and check again:

...
# shellcheck disable=SC1091
source ./sourcefile.sh
...

Then run:

$ shellcheck main.sh

2. Read this if shellcheck was installed using snap:
snap will block access to any other directories that are not in /home//media but looking into your logs it seems like your scripts are in /home/user so that's not a problem, it may be #1.

Mahdy Mirzade
  • 331
  • 3
  • 10
  • The follow working for me to mask the 1091 error: # shellcheck disable=SC1091 source ./sourcefile.sh The follow one, possible more cleane solution dont woork for me: # shellcheck source=sourcefile.sh; source ./sourcefile.sh A if you dont get the error message, it can be I need to update my shellcheck to your version 0.7.2 –  Aug 08 '21 at 11:04
  • 1
    @linda54, In my opinion this message can get ignored, this is because of file permissions and directory access to shellcheck and haskell libraries, according to issue page only ubuntu users had this problem which is the interesting part. Also according to the wiki you can ignore it with a [directive](https://github.com/koalaman/shellcheck/wiki/Directive). And also my suggestion is to check your directory to be readable for groups and not be encrypted/protected so libraries can work fine, also did you try the program with `sudo`? - you can use `ls -l` inside your scripts to check owner/group. – Mahdy Mirzade Aug 08 '21 at 13:57
  • it can be its possible to do it on follow way for people which know how to use it by a directive: source Tell ShellCheck where to find a sourced file (since 0.4.0): # shellcheck source=src/examples/config.sh . "$(locate_config)" See: https://github.com/koalaman/shellcheck/wiki/Directive –  Aug 08 '21 at 15:41
  • Mahdy Mirzade, If you splitt your answer in two answers, I can mark the working one "# shellcheck disable=SC1091" as working one. The "# shellcheck source=sourcefile.sh" dont work for me. –  Aug 13 '21 at 08:52
  • @linda54 That's wierd, but okay, updated my answer. – Mahdy Mirzade Aug 13 '21 at 10:01