bash 脚本
周海汉/文bash脚本其实是格式脚本,并不像C语言那样自由。
比如,对于赋值
$ r=`ls -l test`$ echo $r-rwxrwxr-x 1 zhouhh zhouhh 176 03-16 16:35 test$ ret =`ls -l test`-bash: ret: command not found$ ret= `ls -l test`-bash: -rwxrwxr-x: command not found
对于C语言,赋值号左右都可以有空格,而在bash脚本中,因为有空格,导致了错误。如果左边有空格,会将变量当成命令执行。如果右边有空格,会将变量赋值为空格。
这容易引起的误会是不知道如何在bash中给变量赋值。其实只要将空格去掉就行了。
又如,对于if语句
#!/bin/basha="234"if[ $a=="234"]; then echo "ok"fi$ ./test1./test1: line 4: syntax error near unexpected token `then'./test1: line 4: `if[ $a=="234"]; then'
因为if后面没有空格
页:
[1]