matejak / argbash

Bash argument parsing code generator
Other
1.4k stars 62 forks source link

Failed to execute process #45

Closed filmil closed 6 years ago

filmil commented 6 years ago

When using argbash's docker image to convert the file:

#!/bin/bash
#
# This is a rather minimal example Argbash potential
# Example taken from http://argbash.readthedocs.io/en/stable/example.html
#
# ARG_OPTIONAL_SINGLE([option], [o], [optional argument help msg])
# ARG_OPTIONAL_BOOLEAN([print], , [boolean optional argument help msg])
# ARG_POSITIONAL_SINGLE([positional-arg], [positional argument help  msg], )
# ARG_HELP([The general script's help msg])
# ARGBASH_GO
# [ <-- needed because of Argbash
echo "Value of --option: $_arg_option"
echo "print is $_arg_print"
echo "Value of positional-arg: 

using the script:

#!/bin/bash
docker run -it --rm -e PROGRAM=argbash \
  -v "$(pwd):/work" \
  matejak/argbash "$@"

to convert:

argbash test.sh > test_argbash.sh
chmod +x test_argbash.sh

one gets a file that fails to execute with bizarre errors:

Failed to execute process './test_argbash.sh'. Reason:
', which is not an executable command. the interpreter '/bin/bash

Looking into this a bit, and redirecting like so:

./test_argbash.sh  2>&1 | less

one gets something clearer:

Failed to execute process './test_argbash.sh'. Reason:
The file './test_argbash.sh' specified the interpreter '/bin/bash^M', which is not an executable command.
filmil commented 6 years ago

Looks like argbash produces DOS file endings for some reason. Here's the result of hexdump -C ./test_argbash.sh, clipped at first 4 rows.
Note the bytes 0d 0a after #!/bin/bash.

00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0d 0a 23 0d 0a  |#!/bin/bash..#..|
00000010  23 20 54 68 69 73 20 69  73 20 61 20 72 61 74 68  |# This is a rath|
00000020  65 72 20 6d 69 6e 69 6d  61 6c 20 65 78 61 6d 70  |er minimal examp|
00000030  6c 65 20 41 72 67 62 61  73 68 20 70 6f 74 65 6e  |le Argbash poten|

However, the input file has UNIX file endings. For hexdump -C ./test.sh, note that there's only 0a following #!/bin/bash.

00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 23 0a 23 20  |#!/bin/bash.#.# |
00000010  54 68 69 73 20 69 73 20  61 20 72 61 74 68 65 72  |This is a rather|
00000020  20 6d 69 6e 69 6d 61 6c  20 65 78 61 6d 70 6c 65  | minimal example|
filmil commented 6 years ago

Indeed, as a workaround:

dos2unix < test_argbash.sh > test_argbash2.sh
chmod +x test_argbash2.sh
./test_argbash2.sh

gives the expected output:

The general script's help msg
Usage: ./test_argbash2.sh [-o|--option <arg>] [--(no-)print] [-h|--help] <positional-arg>
    <positional-arg>: positional argument help  msg
    -o,--option: optional argument help msg (no default)
    --print,--no-print: boolean optional argument help msg (off by default)
    -h,--help: Prints help
FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: 'positional-arg'), but got only 0.

While this is easy to work around, I'd argue that argbash should probably work out of the box with the correct line endings. What do you think?

matejak commented 6 years ago

Thank you for your elaborate report, I have tried myself and I have discovered this:

filmil commented 6 years ago

Note that -o <path> is problematic if your output is outside of /work in a container, since the container currently has limited view of the host's filesystem (as it should be).

I think it could work by taking realpath of the -o parameter and then mounting that under a known path in a container. Not sure if I'll have the time to send you a pull request, though.

matejak commented 6 years ago

I have improved the documentation and added some code to produce warnings to the entrypoint in the current master. I will be happy about any pull request, but I think that I can close the issue.