shell脚本编程基础--``与''符号的区分

来源:互联网 发布:unity3d 2d角色朝向 编辑:程序博客网 时间:2024/05/19 00:39
一失足成千古恨,再回头已百年神!!!!!Fuck
在shell脚本编程时,编写统计当前目录下的文件数,小小的错误竟然出现在一个符号上,``与''符号的区分:
#!bin/sh
counter=0
for files in *                    # 替换符*表明对当前目录中所有文件循环
do
    counter=`expr $counter + 1`   #  expr:对表达式求值的命令
done                         # let counter=$counter+1
echo "There are $counter files in `pwd` we need to process"
运行结果是:
[root@localhost shell实验]# ls
hello.c  hello.sh  hi.sh  s2  s3  s4  s5  s6  s7  s8
[root@localhost shell实验]# sh s8
There are 10 files in /home/shell实验 we need to process

 

#!bin/sh
counter=0
for files in *                    # 替换符*表明对当前目录中所有文件循环

do

    counter='expr $counter + 1'   #  expr:对表达式求值的命令

done                         # let counter=$counter+1

echo "There are $counter files in `pwd` we need to process"

运行结果是:
[root@localhost shell实验]# sh s8
There are expr $counter + 1 files in /home/shell实验 we need to process

 

所以:

counter='expr $counter + 1' ,'' 单撇号不是对counter的运算,只是对counter简单的替换

counter=`expr $counter + 1`,``才是能够进行counter运算

原创粉丝点击