shell 学习

shell脚本的try catch

{ # try

    command1 
    #save your output

} || { # catch
    # save log for exception 
}
docs


    http://bbs.chinaunix.net/thread-813011-1-1.html


1. 计算“指定路径”下的文件数量

find $dir -type f|wc -l
  解释:

  用法:

#!/bin/sh
dir=/opt/LNMP1   ---- 定义文件夹路径变量
num=`find $dir -type f|wc -l`   --- 执行命令
if(( $num > 0 ));then           -- if判断 文件个数 >0
echo "$dir file number is:$num"     --- 输出一段话
else                  
echo "$dir nofile"                 
fi

2.   判断是否文件是否存在


#!/bin/bash
 
if [ ! -f "$1" ];then
    echo "当前目录不存在文件: $1"
else
    echo "当前目录存在文件: $1"
fi
if [ -f file ];then
    echo "this file is exist."
fi

运行方法: ./test.sh filename  (test.sh为脚本名称,filename为文件名称)
                   查找目录为当前目

3. -shell bash判断文件或文件夹是否存在

    https://www.cnblogs.com/emanlee/p/3583769.html

#shell判断文件夹是否存在

#如果文件夹不存在,创建文件夹
if [ ! -d "/myfolder" ]; then
  mkdir /myfolder
fi

#shell判断文件,目录是否存在或者具有权限


folder="/var/www/"
file="/var/www/log"

# -x 参数判断 $folder 是否存在并且是否具有可执行权限
if [ ! -x "$folder"]; then
  mkdir "$folder"
fi

# -d 参数判断 $folder 是否存在
if [ ! -d "$folder"]; then
  mkdir "$folder"
fi

# -f 参数判断 $file 是否存在
if [ ! -f "$file" ]; then
  touch "$file"
fi

# -n 判断一个变量是否有值
if [ ! -n "$var" ]; then
  echo "$var is empty"
  exit 0
fi

# 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
  echo '$var1 eq $var2'
else
  echo '$var1 not eq $var2'
fi



#如果文件夹不存在,创建文件夹
if [ ! -d "/myfolder" ]; then
mkdir /myfolder
fi

这一段中"/myfolder" 是在根目录里有没有“myfolder”文件夹,如果没有后面的mkdir命令只有管理员权限才能使用


mkdir /myfolder 这命令的操作确实需要root权限。
此处用 “/myfolder” 只是个示例,用了说明如何判断文件夹是否存在。
您可以用 /home/your_user_name/myfolder 来测试。

这个当然,直接~/myfolder就好






------------------------------ shell  判断文件或者文件夹是否存在 ---------------------------

使用-e 判断,例如
if [ -e $FILE ] 
then
    echo $FILE 作为文件或文件夹是存在的
fi


判断文件是否存在
if [ -f filename ]
判断目录是否存在
if [ -d directory }



--------------------------  file -------------------


file命令:查看文件的类型

(2010-12-03 07:32:27)
转载▼
标签:
杂谈

分类: linux命令
file命令:查看文件的类型

在linux系统中,不存在扩展名,因此判断文件的类型不能像ms那样通过扩展名查看文件的类型.

语法:
file [选项][文件]

-b  列出辨识结果时,不显示文件名称
-c  详细显示指令执行过程。
-d  将任何默认的系统测试应用到文件
-z  探测压缩文件的类型
-L  直接显示符号连接所指向的文件的类型
-f <文件名称>从指定的文件中读取要分析的文件名列表

例:
1、查看天空之城
linux@server:~/Ubuntu One/music$ file 天空之城
天空之城: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, JntStereo
可以看出是一个mp3音乐

2、根据一个文件名称,查看列表里文件的类型
$ cat filename #查看文件里有两个文件名列表
update.iso
examples.desktop
$ file -f filename #显示filename文件中的文件类型
update.iso:       # ISO 9660 CD-ROM filesystem data 'CDROM                          '
examples.desktop: ASCII text
$ 

3、显示压缩文件的数据类型
有一些压缩文件以一定的文件扩展名,如.gz bz2等发布,很容易选择压缩工作,但没有给定扩展名的,要用file查一下,再选择压缩工具。

$ file -z filename.zip
filename.zip: data (Zip archive data, at least v2.0 to extract)


4、查看符号文件的类型和符号链接文件所指向实际文件的类型

linux@server:/$ file initrd.img 
initrd.img: symbolic link to `boot/initrd.img-2.6.35-23-generic-pae'
linux@server:/$ file -L initrd.img 
initrd.img: gzip compressed data, from Unix, last modified: Thu Dec  2 21:04:37 2010


------------------------------------------------------------------------------------------------------


3.1.6  file命令实例:查看文件类型
file命令用于接收一个文件作为参数并执行某些测试,以确定正确的文件类型。
下面这个例子是使用file命令确定一个文件类型的基本方法:
$ file /etc/inittab   
/etc/inittab: ASCII English text  
 
$ file /etc/init.d/network   
/etc/init.d/network: Bourne-Again shell script text executable  
 
$ file /usr/bin/file  
/usr/bin/file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped  
 
$ file /etc  
/etc: directory 
使用-i选项,可以MIME类型的格式显示文件类型的信息:
$ file -i /etc/inittab   
/etc/inittab: text/plain; charset=us-ascii  
 
$ file -i /etc/init.d/network   
/etc/init.d/network: application/x-shellscript  
 
$ file -i /usr/bin/file  
/usr/bin/file: application/x-executable, for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped  
 
$ file -i /etc  
/etc: application/x-not-regular-file 
使用-N选项,输出的队列可以以在文件名之后无空白填充的形式显示,其格式对比如下:
$ file -N *  
a2ps.cfg: ASCII English text  
a2ps-site.cfg: ASCII English text  
acpi: directory  
adjtime: ASCII text  
aliases: ASCII English text  
aliases.db: writable, regular file, no read permission  
aliases.ORIG: ASCII English text  
alsa: directory  
alternatives: directory  
anacrontab: ASCII text  
ant.conf: ASCII text  
ant.d: directory  
anthy-conf: ASCII text  
asound.state: ASCII text, with very long lines  
at.deny: writable, regular file, no read permission  
audisp: directory  
audit: directory  
auto.agl: ASCII text  
autofs_ldap_auth.conf: writable, regular file, no read permission  
 
$ file *  
a2ps.cfg:                                                 ASCII English text  
a2ps-site.cfg:                                            ASCII English text  
acpi:                                                     directory  
adjtime:                                                  ASCII text  
aliases:                                                  ASCII English text  
aliases.db:                                              writable,regular file, no read permission  
aliases.ORIG:                                             ASCII English text  
alsa:                                                     directory  
alternatives:                                             directory  
anacrontab:                                               ASCII text  
ant.conf:                                                 ASCII text  
ant.d:                                                    directory  
anthy-conf:                                               ASCII text  
asound.state:                                             ASCII text, with very long lines  
at.deny:                                                  writable, regular file, no read permission  
audisp:                                                   directory  
audit:                                                    directory  
auto.agl:                                                 ASCII text  
autofs_ldap_auth.conf:                                    writable, regular file, no read permission 


4. Android shell命令行中过滤adb logcat输出的几种方法

      http://www.jb51.net/article/90325.htm

Android shell命令行中过滤adb logcat输出的几种方法


我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率。本文就具体介绍几种在shell命令行中过滤adb logcat输出的方法。
       1、只显示需要的输出(白名单)
       最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep 强大的正则表达式匹配。简单的匹配一行当中的某个字符串,例如 MyApp:
       adb logcat | grep MyApp
       adb logcat | grep -i myapp #忽略大小写。
       adb logcat | grep --color=auto -i  myapp #设置匹配字符串颜色。更多设置请查看 grep 帮助。
      进阶一点可以使用 grep 的正则表达式匹配。例如上一个例子会匹配一行中任意位置的 MyApp,可以设置为仅匹配 tag。默认的 log 输出如下,如果修改过输出格式相应的表达式也要修改。
       I/CacheService(  665): Preparing DiskCache for all thumbnails.
       可以看出 tag 是一行开头的第三个字符开始,根据这点写出表达式:
       adb logcat | grep "^..MyApp"
       根据这个格式也可以设置只显示某个优先级的 log,再匹配行首第一个字符即可。例如仅显示 Error 级别 tag 为 MyApp 的输出:
       adb logcat | grep "^E.MyApp"
       当然也可以匹配多个,使用 | 分割多个匹配表达式,要加转义符。例如要匹配 tag 为 MyApp 和 MyActivity 的输出:
       adb logcat | grep "^..MyApp\|^..MyActivity"
       adb logcat | grep -E "^..MyApp|^..MyActivity"  #使用 egrep 无须转义符
       2、过滤不需要的输出(黑名单)
       还是使用 grep,用法也跟上面的一样,加一个 -v 即可。例如要过滤 tag 为 MyApp 和 MyActivity 的输出:
       adb logcat | grep -v "^..MyApp\|^..MyActivity"
       adb logcat | grep -vE "^..MyApp|^..MyActivity"  #使用 egrep 无须转义符
       3、显示同一个进程的所有输出
       有时一个程序里面的 tag 有多个,需要输出该程序(同一个 PID)的所有 tag;仅使用 tag 过滤有时也会漏掉一些错误信息,而一般错误信息也是和程序同一个 PID。还是通过 grep 实现,思路是先根据包名找到 pid 号,然后匹配 pid。写成 shell 脚本如下,参数是程序的 java 包名(如 com.android.media)。
  #!/bin/bash
  packageName=$1
  pid=`adb shell ps | grep $packageName | awk ‘{print $2}'`
  adb logcat | grep –color=auto $pid
       4、从当前开始显示
       logcat 有缓存,如果仅需要查看当前开始的 log,需要清空之前的。
       adb logcat -c && adb logcat
       5、过滤 log 文件
       有时需要分析 log 文件,过滤 log 文件还是使用 grep。例如 log 文件为 myapp.log,要匹配 tag 为 MyApp 和 MyActivity 的输出,然后输出到 newmyapp.log:
       cat myapp.log | grep "^..MyApp\|^..MyActivity" > newmyapp.log
       Windows 下推荐使用Notepad++,一个免费强大的记事本,支持正则表达式查找替换。可以高亮显示匹配内容,也可以删除不需要的内容。
       以上的技巧主要用到了 grep,其实 logcat 本身也有过滤功能,可以根据 tag、优先级过滤 log,具体请参考 Android 官方文档Reading and Writing Logs。如果喜欢使用图形界面,请参考Using DDMS,DDMS 里面的 logcat 也可以同样过滤。
以上就是Android shell命令行中过滤adb logcat输出的几种方法的资料整理





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值