1

I am trying to call another shell script from inside a shell script in unix (bash)

1st Shell script(script_1.sh):

VARIABLE_1=<VALUE>
<unix_code> 
<unix_code>
..
..
..
export VARIABLE_1
bash -x script_2.sh
bash -x script_3.sh

my requirement here is to pass the value of VARIABLE_1 to script_2.sh & script_3.sh. I was able to execute script_2.sh pass the argument to script_2.sh and it was working fine. Problem has started when i included script_3.sh. It gives me below error, when i execute the first shell script.

/usr/bin/sh: /usr/bin/sh: cannot execute binary file

Not sure why this happening. Also, Is this recommended way to pass arguments and call other shell scripts ? Please let me know

3
  • 1
    Does script_3.sh run properly when executed outside of script_1.sh? Commented Apr 19, 2017 at 7:21
  • Check that you can run /usr/bin/sh from the command-line. It looks like script_3.sh makes a call to /usr/bin/sh somewhere. Commented Apr 19, 2017 at 8:58
  • Can you show the full error output, in particular the xtrace (-x) output immediately preceding the error? Commented Apr 19, 2017 at 9:13

2 Answers 2

2

cannot execute binary file does not mean the file lacks execute permissions. In fact, running a script as "bash filename", does not need the executable permissions on the filename.

The error message is telling you that the script file is not an actual script file, but a binary file, probably an executable. If that is the case, do not do:

bash +x filename

just do:

filename

Additionally, to pass arguments to the script, you can do so like this:

scriptfile arg1 arg2 arg3

then refer inside your script as $1, $2, $3. Or $*to refer to all of them.

EDIT:

Also note that to execute a script you do not need to use bash scriptfile, you may just run scriptfile. In that case, scriptfile must have the executable permission (i.e., chmod +x scriptfile). Additionally, if the file is in the current directory, but the . is not part of the PATHvariable, the scriptfile will not be found. In that case you should execute your scriptfile as ./scriptfile

Your script would change to:

VARIABLE_1=<VALUE>
...
./script_2.sh "$VARIABLE_1"
./script_3.sh "$VARIABLE_1"

You no longer need to export the VARIABLE_1. The quotes are necessary to make sure the contents of VARIABLE_1 are passed as a single argument. script_2.sh and script_3.sh will see the value as $1. If the VARIABLE_1 has spaces in it, and you omit the quotes, each word will be pass as a separate value. That is, $1 will have the first word, $2the second, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the error message means that /usr/bin/sh cannot be executed. If it was the script name then it would say script_3.sh: script_3.sh cannot execute binary file
I wonder what the script_3.sh contents are. Maybe the error message is coming from something executed inside script_3.sh, not from the actual call of script_3.sh
that's my guess, but the OP has not answered my questions
-1

script_3.sh may not have permission to execute. Use chmod to provide appropriate permission and try again

Comments

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.