500-my-bash-functions.ssh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function now-time {
  2. date +%H%M%S
  3. }
  4. function now-date {
  5. date +%Y%m%d
  6. }
  7. function now-all {
  8. echo $(now-date)-$(now-time)
  9. }
  10. function psgrep {
  11. ps -ef | grep $1 | fgrep -v "grep $1"
  12. }
  13. # Network connections search
  14. function nwgrep {
  15. netstat -tulpen | grep $1 | fgrep -v "grep $1"
  16. }
  17. # History search
  18. function hgrep {
  19. history | grep $1 | sed '$d'
  20. }
  21. # Generate random number from $1 to $2
  22. function getrand {
  23. echo $(( $RANDOM % ($2 - $1 + 1) + $1 ))
  24. }
  25. function mkgrx {
  26. local i
  27. while [ -n "$1" ]; do
  28. if [ -z "$i" ]; then
  29. i=$1
  30. else
  31. i="$i\|$1"
  32. fi
  33. shift
  34. done
  35. echo $i
  36. }
  37. #
  38. # Show the actual ip address via checkip.dyndns.org
  39. #
  40. function checkip () {
  41. w3m -dump checkip.dyndns.org | sed 's/Current IP Address://g'
  42. }
  43. #
  44. #
  45. # This function I have taken from firehol, the iptables firewall script
  46. #
  47. #
  48. which_cmd() {
  49. local block=1
  50. if [ "a${1}" = "a-n" ]
  51. then
  52. local block=0
  53. shift
  54. fi
  55. unalias $2 >/dev/null 2>&1
  56. local cmd=`which $2 2>/dev/null| head -n 1`
  57. if [ $? -gt 0 -o ! -x "${cmd}" ]
  58. then
  59. if [ ${block} -eq 1 ]
  60. then
  61. echo >&2
  62. echo >&2 "ERROR: Command '$2' not found in the system path."
  63. echo >&2 " $(basename $0) requires this command for its operation."
  64. echo >&2 " Please install the required package and retry."
  65. echo >&2
  66. echo >&2 " Note that you need an operational 'which' command"
  67. echo >&2 " for $(basename $0) to find all the external programs it"
  68. echo >&2 " needs. Check it yourself. Run:"
  69. echo >&2
  70. echo >&2 " which $2"
  71. echo >&2
  72. #exit 1
  73. fi
  74. return 1
  75. fi
  76. eval $1=${cmd}
  77. return 0
  78. }