hadoop.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Provides tab completion for the main hadoop script.
  17. #
  18. # On debian-based systems, place in /etc/bash_completion.d/ and either restart
  19. # Bash or source the script manually (. /etc/bash_completion.d/hadoop.sh).
  20. _hadoop() {
  21. local script cur prev temp
  22. COMPREPLY=()
  23. cur=${COMP_WORDS[COMP_CWORD]}
  24. prev=${COMP_WORDS[COMP_CWORD-1]}
  25. script=${COMP_WORDS[0]}
  26. # Bash lets you tab complete things even if the script doesn't
  27. # exist (or isn't executable). Check to make sure it is, as we
  28. # need to execute it to get options/info
  29. if [ -f "$script" -a -x "$script" ]; then
  30. case $COMP_CWORD in
  31. 1)
  32. # Completing the first argument (the command).
  33. temp=`$script | grep -n "^\s*or"`;
  34. temp=`$script | head -n $((${temp%%:*} - 1)) | awk '/^ / {print $1}' | sort | uniq`;
  35. COMPREPLY=(`compgen -W "${temp}" -- ${cur}`);
  36. return 0;;
  37. 2)
  38. # Completing the second arg (first arg to the command)
  39. # The output of commands isn't hugely consistent, so certain
  40. # names are hardcoded and parsed differently. Some aren't
  41. # handled at all (mostly ones without args).
  42. case ${COMP_WORDS[1]} in
  43. dfs | dfsadmin | fs | job | pipes)
  44. # One option per line, enclosed in square brackets
  45. temp=`$script ${COMP_WORDS[1]} 2>&1 | awk '/^[ \t]*\[/ {gsub("[[\\]]", ""); print $1}'`;
  46. COMPREPLY=(`compgen -W "${temp}" -- ${cur}`);
  47. return 0;;
  48. jar)
  49. # Any (jar) file
  50. COMPREPLY=(`compgen -A file -- ${cur}`);
  51. return 0;;
  52. namenode)
  53. # All options specified in one line,
  54. # enclosed in [] and separated with |
  55. temp=`$script ${COMP_WORDS[1]} -help 2>&1 | grep Usage: | cut -d '[' -f 2- | awk '{gsub("] \\| \\[|]", " "); print $0}'`;
  56. COMPREPLY=(`compgen -W "${temp}" -- ${cur}`);
  57. return 0;;
  58. *)
  59. # Other commands - no idea
  60. return 1;;
  61. esac;;
  62. *)
  63. # Additional args
  64. case ${COMP_WORDS[1]} in
  65. dfs | fs)
  66. # DFS/FS subcommand completion
  67. # Pull the list of options, grep for the one the user is trying to use,
  68. # and then select the description of the relevant argument
  69. temp=$((${COMP_CWORD} - 1));
  70. temp=`$script ${COMP_WORDS[1]} 2>&1 | grep -- "${COMP_WORDS[2]} " | awk '{gsub("[[ \\]]", ""); print $0}' | cut -d '<' -f ${temp}`;
  71. if [ ${#temp} -lt 1 ]; then
  72. # No match
  73. return 1;
  74. fi;
  75. temp=${temp:0:$((${#temp} - 1))};
  76. # Now do completion based on the argument
  77. case $temp in
  78. path | src | dst)
  79. # DFS path completion
  80. temp=`$script ${COMP_WORDS[1]} -ls "${cur}*" 2>&1 | grep -vE '^Found ' | cut -f 1 | awk '{gsub("^.* ", ""); print $0;}'`
  81. COMPREPLY=(`compgen -W "${temp}" -- ${cur}`);
  82. return 0;;
  83. localsrc | localdst)
  84. # Local path completion
  85. COMPREPLY=(`compgen -A file -- ${cur}`);
  86. return 0;;
  87. *)
  88. # Other arguments - no idea
  89. return 1;;
  90. esac;;
  91. *)
  92. # Other subcommands - no idea
  93. return 1;;
  94. esac;
  95. esac;
  96. fi;
  97. }
  98. complete -F _hadoop hadoop