第17篇 shell编程基础(2)
shell study
1、Exit Status
If the command executed successfully (or true), the value of $? is zero. If the command failed for some reason, $? will contain a positive integer between 1 and 255, inclusive.A failed command usually returns 1.
2、Testing an Expression
The test command evaluates many kinds of expressions,from file properties to integers to strings.
1)file tests,A file’s existence can be tested with -e (or the nonstandard -a). The type of file can be checked with -f for a regular file, -d for a directory, and -h or -L for a symbolic link. for example:
test -f /etc/fstab ## true if a regular file
test -h /etc/rc.local ## true if a symbolic link
[ -x "$HOME/bin/hw" ] ## true if you can execute the file
[[ -s $HOME/bin/hw ]] ## true if the file exists and is not empty
2)Comparisons between integers use the -eq, -ne, -gt,-lt,-ge,and -le operators.
below is a example:
$ test 1 -eq 1
$ echo $?
3) String Tests,The = operator tests for equality, in other words,whether they are identical;
!= tests for inequality.The -z and -n operators return successfully if their arguments are empty or nonempty.
here ara some example:
test "$a" = "$b"
[ "$q" != "$b" ]
$ [ -z "" ]
$ echo $?
0 #判空
$ test -n ""
$ echo $?
1
3、[[ ... ]]: Evaluate an Expression
4、(( ... )): Evaluate an Arithmetic Expression
5、Conditional Execution
example:
read name
if [[ -z $name ]]
then
echo "No name entered" >&2
exit 1 ## Set a failed return code
fi
read number
if (( number > 10 ))
then
printf "%d is too big\n" "$number" >&2
exit 1
else
printf "You entered %d\n" "$number"
fi
6、Conditional Operators, && and ||
Lists containing the AND and OR conditional operators are evaluated from left to right. A
command following the AND operator (&&) is executed if the previous command is
successful. The part following the OR operator (||) is executed if the previous command
fails.
7、case
语法如下:
case WORD in
PATTERN) COMMANDS ;;
PATTERN) COMMANDS ;; ## optional
esac
8、Loop
1)while语法:
while <list>
do
<list>
done
实例:
n=1
while [ $n -le 10 ]
do
echo "$n"
n=$(( $n + 1 ))
done
2)util 很少用,跟while刚好相反(循环会条件fails),实例如下,
n=1
until [ $n -gt 10 ]
do
echo "$n"
n=$(( $n + 1 ))
done
3)for,实例:
for (( n=1; n<=10; ++n ))
do
echo "$n"
done
4)break
do
read x
[ -z "$x" ] && break
done
5)continue
for n in {1..9} ## See Brace expansion in Chapter 4
do
x=$RANDOM
[ $x -le 20000 ] && continue
echo "n=$n x=$x"
done
第17篇 shell编程基础(2)的更多相关文章
- 【转】Shell编程基础篇-上
[转]Shell编程基础篇-上 1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, Linux/UNIX系统的底层及基础应 ...
- 【转】Shell编程基础篇-下
[转]Shell编程基础篇-下 1.1 条件表达式 1.1.1 文件判断 常用文件测试操作符 常用文件测试操作符 说明 -d文件,d的全拼为directory 文件存在且为目录则为真,即测试表达式成立 ...
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
http://blog.csdn.net/xiaominghimi/article/details/7603003 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区 ...
- iOS开发网络篇—网络编程基础
iOS开发网络篇—网络编程基础 一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过 ...
- 【shell编程基础0】bash shell编程的基本配置
前面一篇“shell编程之变量篇”主要讲述下shell编程的变量的基本知识:设置变量的方式,自定义变量和环境变量的差别,变量的替换.删除.测试等. 这一篇主要是讲述在bash shell下的一些基本配 ...
- shell编程基础(转载)
Shell编程基础 原作者 Leal:请参阅页面底部的编者列表. 授权许可: 创作共享署名协议 GNU 自由文档许可证 注意:本文仍然在持续的修订之中,且错漏之处可能较多.如果能够阅读英语的话,可以考 ...
- Linux学习之二十一-shell编程基础
Shell编程基础 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个 ...
- 7-1 shell编程基础之二
shell编程基础之二 算数运算 bash中的算术运算:help let +, -, *, /, %取模(取余), **(乘方),乘法符号有些场景中需要转义 实现算术运算: (1) let var=算 ...
- 6-2 shell编程基础
shell编程基础 编程基础 Linus:Talk is cheap, show me the code 程序和编程风格 程序: 程序:算法+数据结构 数据:是程序的核心 算法:处理数据的方式 数据结 ...
随机推荐
- OCR训练数据生成方法
有的时候我们训练网络的时候,数据集在收集的过程中由于种种原因导致图像收集的不完整,比如某些种类很少,或者没有,这个时候我们就可以考虑自己生成数据集. 这个和data augmentation还不太一样 ...
- Graph_Master(连通分量_B_Trajan+完全图)
hdu_4635 题目大意:给出一张DAG(n个点,m条边),求出能加的最大边数,使得该图无重边,无自环,非强连通. 题解:这题题面很好理解,也没有什么很难的点,主要是如何求出最大边数需要动点脑筋.首 ...
- Java控制语句——分支、循环、跳转
分支语句(if语句,switch语句): 循环语句(for,while,do...while); 跳转语句(break,continue,return): 分支语句(if语句,switch语句) if ...
- Rancher + K8S RestApi使用
1前言 1.1使用的软件及版本 软件 版本号 Rancher 1.6stable Kubernetes 1.8.3 Docker 1.12.6 1.2 Rancher与K8S的RESTAPI差异 ...
- chrome关闭后还在进程中运行
1.网上搜到信息: 设置 “即使关闭浏览器也后台运行” 取消打勾 2.然后我找了一下,应该是这个选项:“关闭 Google Chrome 后继续运行后台应用” 3. 4. 5.
- CheckBox复选框控件
CheckBox复选框控件 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建LinearLayout布局 2 ...
- jenkins集成maven实现自动化接口测试
当时领导让研究这个,就总结了一个文档,现在记录在这里,给自己留个备份. 1.安装jenkins 地址http://updates.jenkins-ci.org/download/war/ 安装mave ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- css03层次选择器
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Java基础学习-extends继承(成员变量,局部变量,成员方法)
package extend; /*面向对象-继承: * 多个类的共同成员变量和成员方法.抽取到另一个类中(父类),我们多个类就可以访问到父类的成员了 * */ class Game{ String ...