LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   strange behavior in a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/strange-behavior-in-a-bash-script-4175601259/)

chatumao 03-07-2017 02:18 PM

strange behavior in a bash script
 
hi there, i am having weird issues here.

my script goes sth like this:
script1.sh
#!/bin/bash
source ./script2.sh

my folder structure looks like this:
folder1/script1.sh
/script2.sh

i am executing like this:
Code:

$./script1.sh
it gives me:
script1.sh: line x: script2.sh: No such file or directory

the problem is not the script or syntax, because if i create a new file in folder1, say script1-1.sh, and copy paste the content of script1.sh to it, it executes just fine. i suspect bash or my shell emulator to be the culprit here.

while writing this i just realized that my problem is easily solved with a copy paste, but i'd rather want to know what is going on here and fix it.
my terminal is xfce4-terminal 0.6.3, bash version is 4.3.11(1) on linux mint xfce x86, the system is practically fresh save for some changes in the firefox configuration and changing the opacity of said shell emulator.
the system is up for some time now, though, about a week with some supends in between.
any help or pointers appreciated, thanks a lot!

EDIT: btw, since the system is practically fresh not much was done or saved on it, if someone is curious she or he could have a look at it through teamwiewer or ssh or whatever, i am not that much of an expert there.

rtmistler 03-07-2017 02:42 PM

Are you in folder1 when you run this script? Or is folder1 in your PATH variable?

The directory where script2.sh is located is important. Remember that ./script2.sh means to run that from the current working directory. Therefore if you are not in the directory where script2.sh is located, you will see exactly the complaint you are seeing.

Another thing to consider is to add
Code:

set -xv
after the #!/bin/bash line in your script to enable extra debugging. And then later comment that out using # at the start of the line so as to keep it there for future reference. The extra debug should inform you how it is attempting to run the script2.sh.

Please remember to use [code][/code] tags to enclose your code.

goumba 03-07-2017 03:08 PM

Quote:

Originally Posted by chatumao (Post 5680335)
hi there, i am having weird issues here.

my script goes sth like this:
script1.sh
#!/bin/bash
source ./script2.sh

my folder structure looks like this:
folder1/script1.sh
/script2.sh

i am executing like this:
Code:

$./script1.sh
it gives me:
script1.sh: line x: script2.sh: No such file or directory

From the directory layout you posted, script2.sh is in the parent of script1.sh? Is that correct? Or did LQ just lose the formatting? (BTW use code tags in the future for this).

If they're in the same directory, why are you even qualifying the directory to script2?

Code:

source script2.sh
should be enough if they are in the same directory.

chatumao 03-07-2017 03:12 PM

still not working
 
first of all, thanks for your reply.

i made sure my cwd is set correctly, which it was, and also activated the debugging options. unfortunately no luck with that. these only give me two additional lines after i execute
Code:

./script1.sh
+ ./script1.sh

which does not tell me anything tbh.
just for the hell of it i added
Code:

echo hello
before
Code:

source ./script2.sh
and after executing i still get the exact same output as before. it is as if bash or the emulator had some sort of cache and an old version of my script is being executed. i am no linux guru and such knowledge is beyond me, that's why i ask here. the only other explanation i could come up with would be faulty ram. but i don't believe it's that, and i cannot reboot my system atm anyways since i am in the progress of some stuff...

goumba 03-07-2017 03:20 PM

Quote:

Originally Posted by chatumao (Post 5680364)
first of all, thanks for your reply.

i made sure my cwd is set correctly, which it was, and also activated the debugging options. unfortunately no luck with that. these only give me two additional lines after i execute
Code:

./script1.sh
+ ./script1.sh

which does not tell me anything tbh.
just for the hell of it i added
Code:

echo hello
before
Code:

source ./script2.sh
and after executing i still get the exact same output as before. it is as if bash or the emulator had some sort of cache and an old version of my script is being executed. i am no linux guru and such knowledge is beyond me, that's why i ask here. the only other explanation i could come up with would be faulty ram. but i don't believe it's that, and i cannot reboot my system atm anyways since i am in the progress of some stuff...

Ok, shot in the dark, what happens if source do

Code:

source script2.sh
at a shell prompt?

I'm wondering if there is something in script2 itself causing this.

Of course, I'm thinking we're not seeing the actual scripts, and so we can't fully diagnose the problem.

chatumao 03-07-2017 03:23 PM

some other tries
 
yeah, lq seems to have lost the formatting there. structure should be like this
Code:

folder1/script1.sh
        script2.sh

meaning both scripts are in the same folder.
but as i posted before, that is not the problem at all. the script is simply not executed as it is, bash is executing an old version of it.

i closed the shell and opened another one, same error. i typed
Code:

bash
and tried it there, same thing. not sure, but i think this eliminated bash as the culprit?
any other terminals i could try on linux mint?

chatumao 03-07-2017 03:36 PM

ok, i solved it. sorry to have bothered you, i had two copies of script1.sh open and did not realize i changed the one i did not execute. that was pretty useless now, very sry to have wasted your time :(. but thanks anyways, i'm back on track now.

goumba 03-07-2017 04:20 PM

Quote:

Originally Posted by chatumao (Post 5680373)
ok, i solved it. sorry to have bothered you, i had two copies of script1.sh open and did not realize i changed the one i did not execute. that was pretty useless now, very sry to have wasted your time :(. but thanks anyways, i'm back on track now.

Glad you solved it.

I hate that "sorry to bother you" thing I've been seeing. That's what we're here for, is to help, if we can.

And don't worry... I just went through the same thing last night with a python script I was working on. ;)

chatumao 03-07-2017 04:35 PM

yeah, sometimes its the most obvious things that seem unsolvable... i guess i forgot that 99 out of 100 cases the problem is in front of the screen, not behind it:)
anyways, thanks for the help!


All times are GMT -5. The time now is 04:00 PM.