当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 2 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : shell程序的应用
帖子发表于 : 2016-12-30 11:43 

注册: 2016-12-30 11:18
帖子: 1
系统: win10
送出感谢: 0 次
接收感谢: 0 次
我想编写一个shell程序来检测位置参数个数,下面是我的程序。
$cat ex2
#bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $#=0
then ls
else
for i
do
ls -l $i|grep '∧d'
done
执行的结果不正确,请大神指教 :Cry


页首
 用户资料  
 
2 楼 
 文章标题 : Re: shell程序的应用
帖子发表于 : 2016-12-30 12:49 
头像

注册: 2011-03-20 13:12
帖子: 4522
地址: 山东省寿光县
系统: Debian
送出感谢: 105
接收感谢: 997
代码:
#!/bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $# = 0; then
    ls
else
    for i in ./*; do
        if test -d ${i}; then
            ls -l $i
        fi
    done
fi


代码:
#!/bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $# = 0; then
    ls
else
    for i in ./*; do
        if test -d ${i}; then
            ls -ld $i
        fi
    done
fi


代码:
#!/bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $# = 0; then
    ls
else
    ls -l | grep ^d
fi


代码:
#!/bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $# = 0; then
    ls
else
    ls -l */
fi


代码:
#!/bin/bash
#if no arguments, then listing the current directory
#otherwise,listing each subdirectory
if test $# = 0; then
    ls
else
    ls -ld */
fi


参考
http://unix.stackexchange.com/questions/86722/how-do-i-loop-through-only-directories-in-bash
http://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash-an-examination


_________________
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 2 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译