0

I have a main script1 where i call functions that are inside script2, which i import with source. When the function inside script2 is called, it calls another function in script2, but commands error out with "X command not found". Am i importing the file wrong or is there something else going on?

script1.sh:

#!/bin/bash

source ./script2.sh
get
# the script crashes due to jq command not found

script2.sh:

#!/bin/bash

get(){
    jq ... 
    echo "So far so good, command goes through."
    dosomething
}
dosomething(){
    echo "Here the command fails with jq command not found :("
    jq ...
}
Pedro Gonzalez
  • 95
  • 2
  • 11
  • Are you sure `jq` is installed on your system? what does the `which jq` command displays in a terminal? Then, note that in every script, a failing command does not prevent from running the subsequent commands. So you'd always need to "protect" your code by writing something like `jq … || { rv="$?"; echo "jq error" >&2; exit "$rv"; }` or just using the famous option `set -e`. – ErikMD Nov 28 '21 at 16:06
  • `Am i importing the file wrong or is there something else going on?` something else. – KamilCuk Nov 28 '21 at 16:19
  • The command in the get() that runs succesfully is the very same command that is in dosomething(). I used jq as an example. It runs sucessfully in the first function and then when it gets to the second function it no longer recognizes it. – Pedro Gonzalez Nov 28 '21 at 16:25
  • 1
    Do you change the `PATH` variable at any point in either script? – Gordon Davisson Nov 28 '21 at 18:45
  • I do not use PATH anywhere. – Pedro Gonzalez Nov 28 '21 at 20:56
  • 1
    The problem has to be in something you haven't mentioned here. I'd try to create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) -- that is, start with copies of your existing scripts and start removing things you think aren't related to the problem. If you remove something and the problem goes away, put it back because it *is* related. When you've removed as much as possible, edit those complete scripts into the question, so we can examine them in full and see what's going on. – Gordon Davisson Nov 29 '21 at 00:08
  • Man I do feel dumb now. I went over the script again, I am assigning various project_path like variables and I do assign PATH at some point. Thank you. – Pedro Gonzalez Nov 29 '21 at 13:21

1 Answers1

0

I did not notice that I used PATH variable at some point which created the trouble. Thank you for pointing that one out Gordon Davisson.

Pedro Gonzalez
  • 95
  • 2
  • 11