2

I'm new to stackoverflow and bash scripting, so go easy on me! I've been struggling with a bash script I've been writing: when I try to call a function 'main' from my script like so:

variable=$("main -t $path/$i")

I get the error "main -t ./folder: No such file or directory"; any ideas?

Thanks in advance!

EDIT: Thanks Jkbkot, I'm now calling it like this:

variable=$(main -t "$path/$i")

The original error is sorted but something is still up: 'variable' seemingly isn't being assigned the value echoed in the function, though calling the function manually prints the correct value. Why might this happen?

EDIT: It seems I'm calling and echoing correctly, but when calling 'main' it seems to behave differently when called recursively to the initial call. For example it runs fine up to:

variable=$(main -t "$path/$i") #A line within 'main'

Then begins again, as expected, but this time it stops as soon as it comes across a 'break', apparently breaking out of the entire function call rather that just the 'case' it's currently in. Is there some quirk to 'break' in bash that I'm unaware of?

NOTE: Unfortunately, the script is an assignment from my university, and many of its students and teachers use this website, so publicly posting my solution would likely have negative consequences.

11
  • It would help to see your actual code, esp. the function main. For example the following works: #!/bin/bash main() { echo "$1-bar" } variable=$(main foo) echo $variable The output is foo-bar as expected. Maybe your fucntion main outputs to stderr instead of stdout? Commented Dec 20, 2013 at 23:32
  • As I've added above, I can't really post the full code. How would I know which I'm outputting to? I'm just using 'echo'. Commented Dec 21, 2013 at 14:04
  • Try to run the snippet from my previous comment and see for yourself that it works as expected. You may change the last echo as echo VARIABLE:$variable to avoid any confusion output of which echo you see. Wrt stderr, you can try to redirect stderr to stdout: variable=$(main -t "$path/$i" 2>&1) If it works then your function outputs to stderr. Also, you can enable "debugging" by putting set -x at the beginning of your script (on a line after the hashbang). Commented Dec 21, 2013 at 14:46
  • Finally got back on this, and the above snippet worked. I had a look through with debugging on and found what appears to be the problem, which I've written above. Commented Jan 1, 2014 at 15:48
  • 1
    All fixed, thanks friend. Commented Jan 1, 2014 at 19:11

1 Answer 1

5

You have to call it without the quotes:

variable=$(main -t $path/$i)

and as @janos says, you might need the quotes around the variables in case they might contain spaces, etc.:

variable=$(main -t "$path/$i")
Sign up to request clarification or add additional context in comments.

3 Comments

careful, he might very well need the quotes inside: variable=$(main -t "$path/$i")
true, he might need the quotes around the variables only
Thanks both of you, but I've hit a different problem, detailed above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.