比较两个bash变量

时间:2016-11-13 05:34:06

标签: bash

我正在尝试编写一个简单的Bash脚本,它显示两个变量之间的差异,假设两个变量包含具有不同值的相同参数。查看$sam$pish个变量。它们是相同的,但driver_mode中的参数1$sam2中的$pish#!/bin/bash sam="driver_mode=2 firmware_path=/home/release/firmware/ onebox_zone_enabled=0x10001 ta_aggr=4 skip_fw_load=0 fw_load_mode=1 sdio_clock=40000 enable_antenna_diversity=0 coex_mode=1 obm_ant_sel_val=2 wlan_rf_power_mode=0 bt_rf_power_mode=0 zigb_rf_power_mode=0 country_code=840 bt_rf_tx_power_mode=1 bt_rf_rx_power_mode=0" pish="driver_mode=1 firmware_path=/home/release/firmware/ onebox_zone_enabled=0x10001 ta_aggr=4 skip_fw_load=0 fw_load_mode=1 sdio_clock=40000 enable_antenna_diversity=0 coex_mode=1 obm_ant_sel_val=2 wlan_rf_power_mode=0 bt_rf_power_mode=0 zigb_rf_power_mode=0 country_code=840 bt_rf_tx_power_mode=1 bt_rf_rx_power_mode=0" read -r -a array_old <<< "$sam" read -r -a array_new <<< "$pish" for index in "${!array_old[@]}"; do if [[ ${array_old[index]} -ne ${array_new[index]} ]]; then echo ${array_old[index]} 'to' ${array_new[index]} fi done 。为了进行这种比较,我将每个变量存储到一个单独的数组中,然后逐个比较每个元素。

      let getPinNameAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
      getPinNameAlert.addTextField(configurationHandler: { (textField) -> Void in
      // textField.placeholder = "Pin Name"
      textField.text = "Current Pin Name"
      textField.keyboardType = UIKeyboardType.default
      textField.clearsOnBeginEditing = true
    })

运行上面的bash脚本会给我一个错误:

  

./ test:第12行:firmware_path = / home / release / firmware /:语法错误:   预期的操作数(错误令牌是&#34; / home / release / firmware /&#34;)

1 个答案:

答案 0 :(得分:5)

-ne运算符是整数comparison operator。要比较字符串使用!=

for index in "${!array_old[@]}"; do
  if [[ ${array_old[index]} != "${array_new[index]}" ]]; then
    echo ${array_old[index]} 'to' ${array_new[index]}     
  fi
done