代码之家  ›  专栏  ›  技术社区  ›  Keren

如何使变量大1

  •  1
  • Keren  · 技术社区  · 1 年前

    我正在尝试编写一个bash脚本,该脚本基本上与traceroute命令类似, 我写了以下代码:

    #! /bin/bash
    
    ttl=1
    dest_ip=$1
    
    #Send packets with increasing ttl and stop when the ping is sucsessfull
    until ["ping -t $ttl -c 1 $dest_ip" = "0"]; do
            echo "$ttl"
            (($ttl+=1))
    done
    

    我运行了这个并得到了错误 enter image description here

    2 回复  |  直到 1 年前
        1
  •  0
  •   markp-fuso    1 年前

    针对 make a variable larger by 1 问题

    错误消息显示 (($tt1+=1) 正在处理为 ((1+=1)) (即 $tt1 被中的值替换 tt1 )它正在生成错误消息( attempted assignment to non-variable ,即, 1 不是变量)。

    您希望按名称而不是按值引用变量,因此: ((tt1+=1)) ;备选方案: ((tt1++)) ((tt1=tt1+1))

        2
  •  0
  •   Keren    1 年前
    while !(ping -q -t $ttl -c 1 $dest_ip > output); do
        echo "$ttl"
        ttl=$((ttl+1))
    

    完成