shell编程--遍历目录下的文件

简介:

假定目录text下有如下文件
      目录:dir_1、dir_2、dir_3

      文件:text_1、text_2

遍历目录下所有的文件是目录还是文件

if -- if类型:

#!bin/sh
for  file in  ./*
do
     if  test -f $file
     then
         echo $file 是文件
     fi
     if  test -d $file
     then
         echo $file 是目录
     fi
done

if --else 类型:

#!bin/sh
for  file in  ./*
do
     if  test -f $file
     then
         echo $file 是文件
     else
         echo $file 是目录
     fi
done

结果:

        

释义:

一.  # 为注释符,其后面内容不编译

二.  第一行 #!不是注释,是对shell的声明,表明用哪种类型的shell,以及路径所在。一般必须写。

     详细解释可以参考:http://blog.163.com/hashes@yeah/blog/static/16867631220101029847420/

三.控制结构:

     (一)if语句:

      1)if语句:

          if  条件

          then   

                命令

          fi 

       2)if ……else语句:

            if 条件

            then

                  命令

            else

                 命令

             fi

         (二)for语句:

            for 条件

            do

                命令

            done

         (三)while语句:

            while

            do

                 命令

            done

四.   * 所有的意思, ./是本目录的意思

      for ./*    本目录中的所有

五. test -f    测试是否是文本

     test -f    测试是否是目录

    

      

 




本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/archive/2012/12/19/2825376.html,如需转载请自行联系原作者

相关文章
|
3天前
|
Shell Android开发
Android系统 adb shell push/pull 禁止特定文件
Android系统 adb shell push/pull 禁止特定文件
16 1
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
31 3
|
9天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
28天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
13 0
|
1月前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
31 1
|
1月前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
25 0
|
1月前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
3月前
|
Shell
删除常规文件及隐藏文件shell脚本
删除常规文件及隐藏文件shell脚本
59 1
|
Shell Android开发 Apache
常用工具类,文件和内存的大小获取,shell脚本的执行
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.
769 0