Simple bash script; what's wrong with this syntax?

前端 未结 3 1428
后悔当初
后悔当初 2021-01-24 08:51

I\'m reading bash script tutorials and this seems like it should work, but I clearly am missing something:

isframecount=0
framecount=0
while read p; do
  if [\"$         


        
相关标签:
3条回答
  • 2021-01-24 09:26
    if [ "$isframecount" -eq 0 ]
    

    Spaces are required on both sides of the square brackets.

    isframecount=1
    

    No dollar sign, no spaces around = in an assignment statement.

    0 讨论(0)
  • 2021-01-24 09:28

    Watch out for spaces where they matters and where there should be not. In this case, if ["$isframecount" -eq 0] should be if [ "$isframecount" -eq 0 ] (see the spaces after [ and before ]).

    The reason for this is that [ is actually the name of a program... See by yourself... Type ls /bin/[... Now, if there is no space, then bash will look for a program named ["0" or something similar, which most certainly does not exist in your path.

    Then, the opposite situation... There must be no space around the = in variable assignment. So $isframecount = 1 should be isframecount=1. Note that I also removed the dollar sign, that's the way to go.

    0 讨论(0)
  • 2021-01-24 09:31

    Two issues:

    Issue 1:

    You need to have spaces in test operator. Change the following line:

    if ["$isframecount" -eq 0]
    

    to

    if [ "$isframecount" -eq 0 ]
    

    Issue 2:

    $isframecount = 1

    There should be no $ sign before variable and no spaces in the assignment operator

    Change it to isframecount=1

    0 讨论(0)
提交回复
热议问题