您的位置:首页 > 运维架构 > Linux

老李分享:《Linux Shell脚本攻略》 要点(三)

2015-12-15 17:37 761 查看

老李分享:《Linux Shell脚本攻略》 要点(三)

1、生产任意大小的文件
[root@localhost dd_test]#
[root@localhost dd_test]# dd if=/dev/zero of=junk.data bs=1k count=10
10+0 records in
10+0 records out
10240 bytes (10 kB) copied, 0.00137023 s, 7.5 MB/s

2、文件系统相关测试
[ -f $file_var ]: 给定的变量包含正常的文件路径或文件名,则返回真
[ -d $var ]: 给定的变量是目录,则返回真。
[ -e $var ]: 给定的变量包含的文件存在,则返回真。
[ [ -z $str1 ]]: 如果str1包含的是空字符串,则返回真。
[ [ -n $str1 ]]: 如果str1包含的是非空字符串,则返回真。
-gt: 大于
-lt: 小于
-ge: 大于或等于.
-le: 小于或等于.

3、文件权限
[root@localhost program_test]# chmod 777 cnts.sh

4、批量生成任意大小的文件
[root@localhost touch_more]# cat create_morefile.sh
#!/bin/bash
for name in {1..100}.txt
do
touch $name
dd if=/dev/zero of=$name bs=1k count=1
done5、生成符号链接文件
[root@localhost touch_more]# ln -s 100.txt 100_symbol.txt
[root@localhost touch_more]# ll -al 100*
lrwxrwxrwx. 1 root root 7 Jan 2 00:24 100_symbol.txt -> 100.txt
-rw-r--r--. 1 root root 1024 Jan 2 00:22 100.txt

查找符号链接的文件
方法一:
[root@localhost touch_more]# ls -al | grep '^l' | awk '{print $9}' //特征标记,以l开头。
100_symbol.txt
方法二:
[root@localhost touch_more]# find ./ -type l
./100_symbol.txt

打印符号链接指向文件的名称:
[root@localhost touch_more]# ls -al 100_symbol.txt | awk '{ print $11 }'
100.txt

6、遍历文件,分类型统计文件
[root@localhost touch_more]# cat filestat.sh#!/bin/bash if [ $# -ne 1 ];then echo $0 basepath; exit 1fipath=$1 declare -A statarray; while read line;do ftype=$(file -b "$line") let statarray["$ftype"]++;done < <(find $path -type f -print) //以子进程统计文件名 echo ===================FILE types and counts =============== for ftype in "${!statarray[@]}"; //数组表do echo $ftype : ${statarray["$ftype"]}done6、实时观看不断增长的文件
[root@localhost touch_more]# tail -f filestat.sh

7、目录切换
[root@localhost program_test]# cd -
/home/yxx/program_test/touch_more
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  软件测试开发