Skip to main content
Post Reopened by Stéphane Chazelas, Runium, Anthon, rahmu, Zelda
Try making my question clearer, as requested
Source Link
cbliard
  • 402
  • 5
  • 12

Bash has a niceis able to trace execution script commands with the -x command line option. Each command is then output to trace execution while debuggingstderr, prefixed by PS4 as stated in the man page.

 -x After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list. 

Albeit useful, the resolved executable path of each command is not given. So I have to figure out which executable has been run by guessing shell scriptsaliases and PATH value while reading execution trace.

For example, ruby has multiple command candidates on my workstation:

-x$ type Print-a commandsruby ruby andis their/home/cbliard/.rvm/rubies/ruby-2.1.0/bin/ruby ruby argumentsis as/home/cbliard/.rvm/bin/ruby ruby theyis are/usr/bin/ruby ruby executedis /home/cbliard/.rvm/bin/ruby 

It can be used when calling bashIf executing this script with /bin/bash -x script-file

#!/bin/bash echo -x"compute" ruby path/to/my/script/run_something.shrb echo "deploy" bundle install bundle exec cap deploy 

Or switched on and off duringTrace execution with set -x and set +xwill be

#!/bin/bash + echo "this"compute" compute + echoruby commandscript/run_something.rb [output isof notruby printed"script] #+ switchbundle oninstall setUsing -xrake (10.1.0) echoUsing "thisi18n one(0.6.9) [...] Your bundle is printedcomplete! Use before`bundle beingshow executed"[gemname]` to see where a bundled gem is installed. echo+ "thisbundle oneexec too"cap deploy # switch off triggering load callbacks set +x * 2014-01-14 09:12:42 executing `deploy' echo "but not* this2014-01-14 one"09:12:42 executing `deploy:update' [...] Finished: SUCCESS 

Running this little scriptI had some oddities with bash willresolved executables, mainly due to my usage of rvm. The output I would like is with fully resolved path, like this:

this+ [builtin] echo command"compute" compute + is/home/cbliard/.rvm/rubies/ruby-1.9.3-p484/bin/ruby notscript/run_something.rb [output printedof ruby script] + echo/home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle 'thisinstall Using onerake is(10.1.0) Using printedi18n before(0.6.9) [...] Your beingbundle executed'is complete! thisUse one`bundle isshow printed[gemname]` beforeto beingsee executedwhere a bundled gem is installed. + echo/home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle 'thisexec onecap too'deploy this one too triggering load callbacks + set +x* 2014-01-14 09:12:42 executing `deploy' but not this* one2014-01-14 09:12:42 executing `deploy:update' [...] Finished: SUCCESS 

Useful debugging output, but I'm missing a feature: knowing to which executableHaving the command has been resolved. My environment is using rvm. command would really help me debugging weird rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that /rubybundler get called but to be 100% sure that the good ruby has been called, I would like the full executable path to rubyissues.

I could do it by modifying the script and calling which here and there, but using a command-line option like The -x or something more hackish would really helpoption helps a lot but sometimes it is not enough. Is there any trick that helps knowing the full characteristics of the commands while running a bash script?. It is better if the original script does not have to be modified.

Bash has a nice -x option to trace execution while debugging shell scripts

-x Print commands and their arguments as they are executed. 

It can be used when calling bash with bash -x script-file

/bin/bash -x path/to/my/script.sh 

Or switched on and off during execution with set -x and set +x

#!/bin/bash echo "this echo command is not printed" # switch on set -x echo "this one is printed before being executed" echo "this one too" # switch off set +x echo "but not this one" 

Running this little script with bash will output:

this echo command is not printed + echo 'this one is printed before being executed' this one is printed before being executed + echo 'this one too' this one too + set +x but not this one 

Useful debugging output, but I'm missing a feature: knowing to which executable the command has been resolved. My environment is using rvm. rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that ruby get called but to be 100% sure that the good ruby has been called, I would like the full executable path to ruby.

I could do it by modifying the script and calling which here and there, but using a command-line option like -x or something more hackish would really help.

Bash is able to trace execution script commands with the -x command line option. Each command is then output to stderr, prefixed by PS4 as stated in the man page.

 -x After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list. 

Albeit useful, the resolved executable path of each command is not given. So I have to figure out which executable has been run by guessing shell aliases and PATH value while reading execution trace.

For example, ruby has multiple command candidates on my workstation:

$ type -a ruby ruby is /home/cbliard/.rvm/rubies/ruby-2.1.0/bin/ruby ruby is /home/cbliard/.rvm/bin/ruby ruby is /usr/bin/ruby ruby is /home/cbliard/.rvm/bin/ruby 

If executing this script with /bin/bash -x

#!/bin/bash echo "compute" ruby script/run_something.rb echo "deploy" bundle install bundle exec cap deploy 

Trace execution will be

+ echo "compute" compute + ruby script/run_something.rb [output of ruby script] + bundle install Using rake (10.1.0) Using i18n (0.6.9) [...] Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. + bundle exec cap deploy  triggering load callbacks  * 2014-01-14 09:12:42 executing `deploy' * 2014-01-14 09:12:42 executing `deploy:update' [...] Finished: SUCCESS 

I had some oddities with resolved executables, mainly due to my usage of rvm. The output I would like is with fully resolved path, like this:

+ [builtin] echo "compute" compute + /home/cbliard/.rvm/rubies/ruby-1.9.3-p484/bin/ruby script/run_something.rb [output of ruby script] + /home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle install Using rake (10.1.0) Using i18n (0.6.9) [...] Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. + /home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle exec cap deploy  triggering load callbacks * 2014-01-14 09:12:42 executing `deploy' * 2014-01-14 09:12:42 executing `deploy:update' [...] Finished: SUCCESS 

Having the resolved command would really help me debugging weird rvm/bundler issues. The -x option helps a lot but sometimes it is not enough. Is there any trick that helps knowing the full characteristics of the commands while running a bash script?. It is better if the original script does not have to be modified.

Post Closed as "Needs details or clarity" by rahmu, Anthon, slm, Zelda, jasonwryan
add words trace execution
Source Link
cbliard
  • 402
  • 5
  • 12

Bash has a nice -x option to debugtrace execution while debugging shell scripts

-x Print commands and their arguments as they are executed. 

It can be used when calling bash with bash -x script-file

/bin/bash -x path/to/my/script.sh 

Or switched on and off during execution with set -x and set +x

#!/bin/bash echo "this echo command is not printed" # switch on set -x echo "this one is printed before being executed" echo "this one too" # switch off set +x echo "but not this one" 

Running this little script with bash will output:

this echo command is not printed + echo 'this one is printed before being executed' this one is printed before being executed + echo 'this one too' this one too + set +x but not this one 

Useful debugging output, but I'm missing a feature: knowing to which executable the command has been resolved. My environment is using rvm. rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that ruby get called but to be 100% sure that the good ruby has been called, I would like the full executable path to ruby.

I could do it by modifying the script and calling which here and there, but using a command-line option like -x or something more hackish would really help.

Bash has a nice -x option to debug shell scripts

-x Print commands and their arguments as they are executed. 

It can be used when calling bash with bash -x script-file

/bin/bash -x path/to/my/script.sh 

Or switched on and off during execution with set -x and set +x

#!/bin/bash echo "this echo command is not printed" # switch on set -x echo "this one is printed before being executed" echo "this one too" # switch off set +x echo "but not this one" 

Running this little script with bash will output:

this echo command is not printed + echo 'this one is printed before being executed' this one is printed before being executed + echo 'this one too' this one too + set +x but not this one 

Useful debugging output, but I'm missing a feature: knowing to which executable the command has been resolved. My environment is using rvm. rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that ruby get called but to be 100% sure that the good ruby has been called, I would like the full executable path to ruby.

I could do it by modifying the script and calling which here and there, but using a command-line option like -x or something more hackish would really help.

Bash has a nice -x option to trace execution while debugging shell scripts

-x Print commands and their arguments as they are executed. 

It can be used when calling bash with bash -x script-file

/bin/bash -x path/to/my/script.sh 

Or switched on and off during execution with set -x and set +x

#!/bin/bash echo "this echo command is not printed" # switch on set -x echo "this one is printed before being executed" echo "this one too" # switch off set +x echo "but not this one" 

Running this little script with bash will output:

this echo command is not printed + echo 'this one is printed before being executed' this one is printed before being executed + echo 'this one too' this one too + set +x but not this one 

Useful debugging output, but I'm missing a feature: knowing to which executable the command has been resolved. My environment is using rvm. rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that ruby get called but to be 100% sure that the good ruby has been called, I would like the full executable path to ruby.

I could do it by modifying the script and calling which here and there, but using a command-line option like -x or something more hackish would really help.

Source Link
cbliard
  • 402
  • 5
  • 12

Bash option to get resolved executable path?

Bash has a nice -x option to debug shell scripts

-x Print commands and their arguments as they are executed. 

It can be used when calling bash with bash -x script-file

/bin/bash -x path/to/my/script.sh 

Or switched on and off during execution with set -x and set +x

#!/bin/bash echo "this echo command is not printed" # switch on set -x echo "this one is printed before being executed" echo "this one too" # switch off set +x echo "but not this one" 

Running this little script with bash will output:

this echo command is not printed + echo 'this one is printed before being executed' this one is printed before being executed + echo 'this one too' this one too + set +x but not this one 

Useful debugging output, but I'm missing a feature: knowing to which executable the command has been resolved. My environment is using rvm. rvm stands for Ruby Version Manager and is an utility to install multiple ruby environments. So I installed multiple ruby environments. When I run a shell-script remotely, I see that ruby get called but to be 100% sure that the good ruby has been called, I would like the full executable path to ruby.

I could do it by modifying the script and calling which here and there, but using a command-line option like -x or something more hackish would really help.