如何在bash命令中使用十六进制数

时间:2017-10-03 15:10:58

标签: bash hex

我可以看到许多地方解释如何在打印出来时如何在十六进制数和十进制数之间进行转换。 我不想打印它,我想在命令中使用十六进制值。

versionId

currentAdress=$((0x$currentAdress + 8)) # read the max_payload maxPayloadCmd="sudo /sbin/setpci -d $vendorId:$deviceId $currentAdress.B" maxPayload=`eval $maxPayloadCmd` currentAdress00范围内的字符串

当我打印FF时,我可以看到我有小数值,而不是maxPayloadCmd

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您可以printf%x一起使用以十六进制格式打印值。

maxPayloadCmd="sudo /sbin/setpci -d $vendorId:$deviceId $(printf '%02x.B' $currentAdress)"

顺便说一下,将命令存储在函数而不是变量中更自然。你应该avoid eval一般。

maxPayload() {
    sudo /sbin/setpci -d "$vendorId:$deviceId" "$(printf '%02x.B' "$currentAdress")"
}
# Call the function the same as any other command.
maxPayload

答案 1 :(得分:1)

这可能取决于您打印变量的方式:

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <div class="container">
    <form>
      <h2>Customer Information</h2>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label for="name" class="col-form-label">Name</label>
          <input type="text" class="form-control" id="name" placeholder="First and Last Name">
        </div>
        <div class="form-group col-md-6">
          <label for="phone" class="col-form-label">Phone Number</label>
          <input type="text" class="form-control" id="phone" placeholder="123-456-7890">
        </div>
      </div>
      <h2>Delivery Address</h2>
      <div class="form-group">
        <label for="Address" class="col-form-label">Address</label>
        <input type="text" class="form-control" id="Address" placeholder="1234 Main St">
      </div>
      <div class="form-group">
        <label for="Address2" class="col-form-label">Address 2</label>
        <input type="text" class="form-control" id="Address2" placeholder="Apartment, studio, or floor">
      </div>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label for="City" class="col-form-label">City</label>
          <input type="text" class="form-control" id="City" placeholder="Springfield">
        </div>
        <div class="form-group col-md-4">
          <label for="State" class="col-form-label">State</label>
          <input type="text" class="form-control" id="State" placeholder="MO">
        </div>
        <div class="form-group col-md-2">
          <label for="inputZip" class="col-form-label">Zip</label>
          <input type="text" class="form-control" id="inputZip" placeholder="65806">
        </div>
      </div>
      <h2>Order Details</h2>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label for="pizzaChoice" class="col-form-label">Choose a Pizza Type</label>
          <select id="pizzaChoice"></select>
        </div>
        <div class="form-group col-md-6">
          <label for="orderAmount" class="col-form-label">How Hungry Are You?</label>
          <select id="orderAmount"></select>
        </div>
      </div>
      <button id="submit" onclick="calcTotal()">Place Order</button>
    </form>
  </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script>
      var order = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
      select = document.getElementById('orderAmount');
      for(quant in order) {
        select.add(new Option(order[quant], quant));
      };
      var pizzaTypes = ['Cheese', 'Pepperoni', 'Veggie', 'Hawaiian', 'Supreme'],
      select = document.getElementById('pizzaChoice');
      for(pizzas in pizzaTypes) {
        select.add(new Option(pizzaTypes[pizzas], pizzas));
      };
      </script>
      <script>
      function calcTotal() {
          var cusName = document.getElementById("name").value;
          var phoneNum = document.getElementById("phone").value;
          var pizzasOdered = document.getElementById("orderAmount").value;
          var orderedPizzaType = document.getElementById("pizzaChoice");
          if (orderedPizzaType = pizzaTypes[0]) {
              itemTotal = 7.00;
              else if (orderedPizzaType = pizzaTypes[1]) {
                itemTotal = 8.00;
              }
              else if (orderedPizzaType = pizzaTypes[2]) {
                itemTotal = 8.50;
              }
              else if (orderedPizzaType = pizzaTypes[3]) {
                itemTotal = 9.50;
              }
              else if (orderedPizzaType = pizzaTypes[4]) {
                itemTotal = 10.00;
              }
          }
          var subTotal = itemTotal * pizzasOdered;
          var salesTaxRate = 0.076;
          var salesTax = (itemTotal * salesTaxRate);
          var orderTotal = subTotal + (subTotal * salesTaxRate);
          alert("Thank you for your order " + cusName + "! If we have any issues we'll call you at " + phoneNum + "<br>" +
                     "Here's your oder summary: " + pizzasOdered + orderedPizzaType + "pizzas were ordered." +
                     "Pizza Price: $" + itemTotal + "<br>" +
                     "Sub Total: $" + subTotal + "<br>" +
                     "Sales Tax: $" + salesTax + "<br>" +
                     "Total Due: $" + orderTotal);
      }
      document.getElementById("submit").addEventListener("click", calcTotal, false);
      document.getElementById("reset").addEventListener("click", clearForm, true);
    </script>
  </body>
</html>