分页: 1 / 1
刚接触shell编程,求教一个问题
发表于 : 2011-05-27 8:26
由 lingdududu
题目:编写这样一段Shell脚本,给出一个目录名称,脚本可以显示这个目录下的文件的个数
输入:please input a absolute directory:/tmp
输出:文件的数目 files in /tmp
我这样写的:
echo -e "please input a absolute directory:\c"
read DIR
a=ls -al $DIR |wc -l
echo $a files int $DIR
输出的结果报错
./file: line3: -al: command not found
由于刚接触shell编程,不知道怎样改才能达到题目的效果,求救大家
Re: 刚接触shell编程,求教一个问题
发表于 : 2011-05-27 9:51
由 烟雨平生
echo -e "please input a absolute directory:\c"
read DIR
a=ls -al $DIR |wc -l
echo $a files int $DIR
命令替换,把 a=ls -al $DIR |wc -l
替换为
a=`ls -al $DIR|wc -l` 或者 a=$(ls -al $DIR|wc -l)
意思是:把`` 或者$()里面的命令执行的结果,作为值赋给变量a
注意:`` 是键盘左上角Esc键下面那个反引号,不是单引号,`` 跟$()是等价的,后者是最新的写法,建议用后者
Re: 刚接触shell编程,求教一个问题
发表于 : 2011-05-28 15:15
由 lingdududu
烟雨平生 写了:echo -e "please input a absolute directory:\c"
read DIR
a=ls -al $DIR |wc -l
echo $a files int $DIR
命令替换,把 a=ls -al $DIR |wc -l
替换为
a=`ls -al $DIR|wc -l` 或者 a=$(ls -al $DIR|wc -l)
意思是:把`` 或者$()里面的命令执行的结果,作为值赋给变量a
注意:`` 是键盘左上角Esc键下面那个反引号,不是单引号,`` 跟$()是等价的,后者是最新的写法,建议用后者
可以了,谢谢哦
Re: 刚接触shell编程,求教一个问题
发表于 : 2011-05-29 2:37
由 qiang_liu8183
[bash]#!/bin/bash
#
# Statistics the number of documents
#
if [ "$1" = "" ]; then
ls -AlR | grep "^-" | wc -l
else
if [ -d "$1" ]; then
ls -AlR "$1" | grep "^-" | wc -l
else
echo "lf: cannot access "$1": No such directory"
fi
fi[/bash]
代码: 全选
lq ~ $ pwd
/home/lq
lq ~ $ fn
2968
lq ~ $ fn /home/lq
2968