博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell脚本:test命令 if-then for while 学习笔记
阅读量:4030 次
发布时间:2019-05-24

本文共 1540 字,大约阅读时间需要 5 分钟。

test 文件运算符:

-b file  如果文件为一个块特殊文件,则为真

-c file  如果文件为一个字符特殊文件,则为真

-d file  如果文件为一个目录,则为真

-e file  如果文件存在,则为真

-f file  如果文件为一个普通文件,则为真

-g file  如果设置了文件的 SGID 位,则为真

-G file  如果文件存在且归该组所有,则为真

-k file  如果设置了文件的粘着位,则为真

-O file  如果文件存在并且归该用户所有,则为真

-p file  如果文件为一个命名管道,则为真

-r file  如果文件可读,则为真

-s file  如果文件的长度不为零,则为真

-S file  如果文件为一个套接字特殊文件,则为真

-t fd   如果 fd 是一个与终端相连的打开的文件描述符(fd 默认为 1),则为真

-u file  如果设置了文件的 SUID 位,则为真

-w file  如果文件可写,则为真

-x file  如果文件可执行,则为真

if-then结构化命令 实例:

file=test1touch fileif [ -s file ]then        echo " The file file exits and has data in it"else        echo " The file file exits ,but  has not  data in it"fidate > fileif [ -s file ]then        echo "the file file has data in it"else        echo "the file file is also empty ,no data hummm"fiif [ 1.c -nt one2.c ]then        echo "1.c is newer than one2.c"else        echo "1.c is not newer than one2.c"fi

for命令:

for var in list

do
   commands
done 

实例:

list="one one2 one4 one5"list=$list" one6"//在已经存在的变量中添加新的数据for state in $listdo        echo "state is $state"done结果:./forteststate is onestate is one2state is one4state is one5state is one6
读取命令中的值:file="states"for state in 'cat $file'do        echo "visit word $state"done~
while命令

格式:

while the command

do
    other commands
done

实例:

var1=10 #赋值是不能有空格的 有空格就会出现问题 例如: /last: 第 3 行: [: -gt: 期待一元表达式等echo "var1 is $var1"while [ $var1 -gt 0 ]do        echo "the var1 is $var1"        var1=$[$var1 -1 ]done
结果:

the var1 is 10the var1 is 9the var1 is 8the var1 is 7the var1 is 6the var1 is 5the var1 is 4the var1 is 3the var1 is 2the var1 is 1

转载地址:http://zqobi.baihongyu.com/

你可能感兴趣的文章
机器学习中典型工作流程
查看>>
数据挖掘十大算法 and 算法概述
查看>>
机器学习中样本数据预处理
查看>>
机器学习中样本缺失值的处理方法
查看>>
机器学习中样本比例不平衡的处理方法
查看>>
机器学习中的文本处理
查看>>
K近邻分类
查看>>
Java集合
查看>>
Java泛型、反射、注解、Lambda表达式
查看>>
Spring框架入门
查看>>
Linear Regression及各种线型回归在正则化中的应用
查看>>
朴素贝叶斯算法
查看>>
逻辑回归
查看>>
感知机 - 支持向量机
查看>>
决策树算法(ID3、C4.5、CART)
查看>>
集成学习(Bagging、Boosting、Stacking)
查看>>
无监督学习
查看>>
K均值算法(K-means)
查看>>
机器学习中的损失函数
查看>>
机器学习中的性能度量
查看>>