Shell脚本中自定义变量报错 command not found

蔚落 2023-07-18 04:52 64阅读 0赞

Shell中自定义变量 报错: command not found

以前都没能接触到编写shell脚本,今天尝试用shell完成简单的加法操作,却出现报错,具体内容如下:

  1. $cat add.sh
  2. #!/bin/bash
  3. a = $1
  4. b = $2
  5. (( c = $a + $b ))
  6. echo "$a + $b = $c"
  7. [hadoop@node100 XiaoF]$ sh add.sh 1 3
  8. add.sh: line 3: a: command not found
  9. add.sh: line 4: b: command not found
  10. add.sh: line 5: ((: c = + :
  11. syntax error: operand expected (error token is "+ ")
  12. + =

在这里插入图片描述

后来发现报错的原因是在于“=”左右两边我加上了空格,将“=”两边的空格去掉就可以了

  1. [hadoop@node100 XiaoF]$ vim add.sh
  2. [hadoop@node100 XiaoF]$ cat add.sh
  3. #!/bin/bash
  4. a=$1
  5. b=$2
  6. (( c=$a+$b ))
  7. echo "$a+$b=$c"
  8. [hadoop@node100 XiaoF]$ sh add.sh 34 55
  9. 34+55=89

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,64人围观)

还没有评论,来说两句吧...

相关阅读