kvz / bash3boilerplate

Templates to write better Bash scripts
http://bash3boilerplate.sh
MIT License
2.13k stars 199 forks source link

Odd behaviour when example.sh is invoked with no args #101

Closed QwertyZW closed 6 years ago

QwertyZW commented 6 years ago

No sure if this behaviour is intended. I'm using bash4. This is what I had to do to work around it: https://github.com/QwertyZW/bash3boilerplate/commit/0cdc704a3d55661aa6ad17f9bd4271b0cdf4bedd

qwertyzw% ./example.sh       

(18-06-25 6:20:41) <1> [~/bash3boilerplate]  
qwertyzw% PS4='Line ${LINENO}: ' bash -x ./example.sh
Line 31: read -r -d '' __usage
Line 39: true
Line 42: read -r -d '' __helptext
Line 46: true
LLLine 49: dirname ./example.sh
LLine 49: cd .
LLine 49: pwd
Line 49: source /home/alkhishm/bash3boilerplate/main.sh
LLine 19: set -o errexit
LLine 21: set -o errtrace
LLine 23: set -o nounset
LLine 25: set -o pipefail
LLine 29: [[ /home/alkhishm/bash3boilerplate/main.sh != \.\/\e\x\a\m\p\l\e\.\s\h ]]
LLine 30: __i_am_main_script=0
LLine 32: [[ -n x ]]
LLine 33: [[ ./example.sh = \.\/\e\x\a\m\p\l\e\.\s\h ]]
LLine 34: __i_am_main_script=1
LLine 37: __b3bp_external_usage=true
LLine 38: __b3bp_tmp_source_idx=1
LLLLine 47: dirname ./example.sh
LLLine 47: cd .
LLLine 47: pwd
LLine 47: __dir=/home/alkhishm/bash3boilerplate
LLLine 48: basename ./example.sh
LLine 48: __file=/home/alkhishm/bash3boilerplate/example.sh
LLLine 49: basename /home/alkhishm/bash3boilerplate/example.sh .sh
LLine 49: __base=example
LLLine 50: printf %q /home/alkhishm/bash3boilerplate/example.sh
LLLine 50: (( 0 ))
LLine 50: __invocation=/home/alkhishm/bash3boilerplate/example.sh

(18-06-25 6:20:44) <1> [~/bash3boilerplate]  
qwertyzw% git checkout master
Previous HEAD position was 327cba7... Add magic variable that stores the full command line invocation (#99)
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

(18-06-25 6:20:48) <0> [~/bash3boilerplate]  
qwertyzw% ./example.sh 

 Option -f (--file) requires an argument

  -f --file  [arg] Filename to process. Required.
  -t --temp  [arg] Location of tempfile. Default="/tmp/bar"
  -v               Enable verbose mode, print script as it is executed
  -d --debug       Enables debug mode
  -h --help        This page
  -n --no-color    Disable color output
  -1 --one         Do just one thing

 This is Bash3 Boilerplate's help text. Feel free to add any description of your
 program or elaborate more on command-line arguments. This section is not
 parsed and will be added as-is to the help.
gdevenyi commented 6 years ago

Not sure what you're referring to here, HEAD doesn't do that:

$ git clone git@github.com:kvz/bash3boilerplate.git
Cloning into 'bash3boilerplate'...
X11 forwarding request failed on channel 0
remote: Counting objects: 1567, done.
remote: Total 1567 (delta 0), reused 0 (delta 0), pack-reused 1567
Receiving objects: 100% (1567/1567), 1.68 MiB | 5.85 MiB/s, done.
Resolving deltas: 100% (894/894), done.
10:43:18 [gdevenyi@monster:~] $ cd bash3boilerplate/
10:43:19 [gdevenyi@monster:~/bash3boilerplate] master ± ./example.sh 
10:43:20 [gdevenyi@monster:~/bash3boilerplate] master ± 
gdevenyi commented 6 years ago

What version of bash are you using? bash --version

gdevenyi commented 6 years ago

It looks like you had a debug mode enabled on your terminal somehow.

zbeekman commented 6 years ago

@gdevenyi they set PS4 and turned on -x: qwertyzw% PS4='Line ${LINENO}: ' bash -x ./example.sh so they are intentionally showing us the output.

@QwertyZW what is the problem? You expect the help text to print when invoking ./example.sh but it doesn't?

QwertyZW commented 6 years ago

Thanks for the fast response guys.

@zbeekman yep, this is what I was looking for