cmd.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/sh
  2. #
  3. # This file is part of the flashrom project. It is derived from
  4. # board_status.sh in coreboot.
  5. #
  6. # Copyright (C) 2016 Google Inc.
  7. # Copyright (C) 2014 Sage Electronic Engineering, LLC.
  8. USE_CUSTOM_HOOKS=0
  9. if [ -n "$CUSTOM_HOOKS_FILENAME" ]; then
  10. USE_CUSTOM_HOOKS=1
  11. . "$CUSTOM_HOOKS_FILENAME"
  12. if [ $? -ne 0 ]; then
  13. echo "Failed to source custom hooks file."
  14. exit $EXIT_FAILURE
  15. fi
  16. fi
  17. # test a command
  18. #
  19. # $1: 0 ($LOCAL) to run command locally,
  20. # 1 ($REMOTE) to run remotely if remote host defined
  21. # $2: command to test
  22. # $3: 0 ($FATAL) Exit with an error if the command fails
  23. # 1 ($NONFATAL) Don't exit on command test failure
  24. test_cmd()
  25. {
  26. local rc
  27. local cmd__="$(echo $2 | cut -d ' ' -f -1)"
  28. local args="$(echo $2 | cut -d ' ' -f 2-)"
  29. if [ -e "$cmd__" ]; then
  30. return
  31. fi
  32. if [ "$1" -eq "$REMOTE" ] && [ -n "$REMOTE_HOST" ]; then
  33. ssh $REMOTE_PORT_OPTION root@${REMOTE_HOST} command -v "$cmd__" $args > /dev/null 2>&1
  34. rc=$?
  35. else
  36. command -v "$cmd__" $args >/dev/null 2>&1
  37. rc=$?
  38. fi
  39. if [ $rc -eq 0 ]; then
  40. return 0
  41. fi
  42. if [ "$3" = "1" ]; then
  43. return 1
  44. fi
  45. echo "$2 not found"
  46. exit $EXIT_FAILURE
  47. }
  48. # Same args as cmd() but with the addition of $4 which determines if the
  49. # command should be totally silenced or not.
  50. _cmd()
  51. {
  52. local silent=$4
  53. local rc=0
  54. if [ -n "$3" ]; then
  55. pipe_location="${3}"
  56. else
  57. pipe_location="/dev/null"
  58. fi
  59. if [ $1 -eq $REMOTE ] && [ -n "$REMOTE_HOST" ]; then
  60. if [ $silent -eq 0 ]; then
  61. ssh $REMOTE_PORT_OPTION "root@${REMOTE_HOST}" "$2" > "$pipe_location" 2>/dev/null
  62. rc=$?
  63. else
  64. ssh $REMOTE_PORT_OPTION "root@${REMOTE_HOST}" "$2" >/dev/null 2>&1
  65. rc=$?
  66. fi
  67. else
  68. if [ $USE_CUSTOM_HOOKS -eq 1 ]; then
  69. preflash_hook $1 "$2" "$3" $4
  70. fi
  71. if [ $silent -eq 0 ]; then
  72. $SUDO_CMD $2 > "$pipe_location" 2>/dev/null
  73. rc=$?
  74. else
  75. $SUDO_CMD $2 >/dev/null 2>&1
  76. rc=$?
  77. fi
  78. if [ $USE_CUSTOM_HOOKS -eq 1 ]; then
  79. postflash_hook $1 "$2" "$3" $4
  80. fi
  81. fi
  82. return $rc
  83. }
  84. # run a command
  85. #
  86. # $1: 0 ($LOCAL) to run command locally,
  87. # 1 ($REMOTE) to run remotely if remote host defined
  88. # $2: command
  89. # $3: filename to direct output of command into
  90. cmd()
  91. {
  92. _cmd $1 "$2" "$3" 0
  93. if [ $? -eq 0 ]; then
  94. return
  95. fi
  96. echo "Failed to run \"$2\", aborting"
  97. rm -f "$3" # don't leave an empty file
  98. return $EXIT_FAILURE
  99. }
  100. # run a command silently
  101. #
  102. # $1: 0 ($LOCAL) to run command locally,
  103. # 1 ($REMOTE) to run remotely if remote host defined
  104. # $2: command
  105. scmd()
  106. {
  107. _cmd $1 "$2" "" 1
  108. if [ $? -eq 0 ]; then
  109. return
  110. fi
  111. echo "Failed to run \"$2\", aborting"
  112. return $EXIT_FAILURE
  113. }