1、shell算术运算
1)加法
r=`expr 4 + 5`(注意! '4' '+' '5' 这三者之间要有空白)
r=$[ 4 + 5 ]
r=$(( 4 + 5 ))
echo $r

2)乘法
r=`expr 4 \* 5`
r=$(( 4 * 5 ))
r=$[ 4 * 5 ]
echo $r

3)除法
r=`expr 40 / 5`
r=$(( 40 / 5 ))
r=$[ 40 / 5 ]
echo $r

4)减法
r=`expr 40 - 5`
r=$(( 40 - 5 ))
r=$[ 40 - 5 ]
echo $r

5)求余数
r=$[ 100 % 43 ]
echo $r

6)乘幂(如2的3次方)
r=$(( 2 ** 3 ))
r=$[ 2 ** 3 ]
echo $r
注:expr 沒有乘幂

7)使用let命令
加法:
n=10
let n=n+1
echo $n #n=11

乘法:
let m=n*10
echo $m

除法:
let r=m/10
echo $r

求余数:
let r=m%7
echo $r

乘冪:
let r=2**2
echo $r

2、利用test指令的测试功能
1)关于某个档名的类型侦测(存在与否),如:test -e filename
-e  该『档名』是否存在?
-f  该『档名』是否为档案(file)?
-d  该『文件名』是否为目录(directory)?
-b  该『文件名』是否为一个 block device 装置?
-c  该『文件名』是否为一个 character device 装置?
-S  该『档名』是否为一个 Socket 档案?
-p  该『档名』是否为一个 FIFO (pipe) 档案?
-L  该『档名』是否为一个连结档?

2)关于档案的权限侦测,如:test -r filename
-r  侦测该文件名是否具有『可读』的属性?
-w  侦测该档名是否具有『可写』的属性?
-x  侦测该档名是否具有『可执行』的属性?
-u  侦测该文件名是否具有『SUID』的属性?
-g  侦测该文件名是否具有『SGID』的属性?
-k  侦测该文件名是否具有『Sticky bit』的属性?
-s  侦测该档名是否为『非空白档案』?

3)两个档案之间的比较,如:test file1 -nt file2
-nt  (newer than)判断file1是否比file2新
-ot  (older than)判断file1是否比file2旧
-ef  判断file2与file2是否为同一档案,可用在判断hard link的判定上。主要意义在判定两个档案是否均指向同一个inode

4)关于两个整数之间的判定,如:test n1 -eq n2
-eq  两数值相等(equal)
-ne  两数值不等(not equal)
-gt  n1大于n2(greater than)
-lt  n1小于n2(less than)
-ge  n1大于等于n2(greater than or equal)
-le  n1小于等于n2(less than or equal)

5)判定字符串的数据
test -z string  判定字符串是否为0?若string为空字符串,则为true
test -n string  判定字符串是否非为0?若string为空字符串,则为false。
注:-n亦可省略
test str1 = str2 判定str1是否等于str2,若相等,则回传true
test str1 != str2 判定str1是否不等于str2,若相等,则回传false

6)多重条件判定,例如:test -r filename -a -x filename
-a (and)两状况同时成立!例如test -r file -a -x file,则file同时具有r与x权限时才回传true。
-o (or)两状况任何一个成立!例如test -r file -o -x file,则file具有r或x权限时就可回传true。
! 反相状态,如test ! -x file,当file不具有x时回传true。

[root@linux scripts]# vi sh05.sh
#!/bin/bash
# Program:
#  Let user input a filename, the program will search the filename
#  1.) exist? 2.) file/directory? 3.) file permissions
# History:
# 2005/08/25  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#让使用者输入档名,并且判断使用者是否真的有输入字符串?
echo -e "The program will show you that filename is exist whichinput by you.\n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
#判断档案是否存在?
test ! -e $filename && echo "The filename $filename DO NOT exist" && exit 0
#开始判断档案类型与属性
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
#开始输出信息
echo "The filename: $filename is a $filetype"
echo "And the permission are : $perm"

3、利用判断符号[ ]的测试功能
   除了我们很喜欢使用的test之外,其实还可以利用判断符号[ ]来进行数据的判断。
[root@linux ~]# [ -z "$HOME" ]
[ "$HOME" == "$MAIL" ]
[□"$HOME"□==□"$MAIL"□] 
↑           ↑    ↑          ↑
注意:
1)在中括号[]内的每个组件都需要有空格键来分隔;
2)在中括号内的变量最好都以双引号来设定;
3)在中括号内的常数最好都以单或双引号来设定。
[root@linux scripts]# vi sh06.sh
#!/bin/bash
# Program:
#  This program will show the user's choice
# History:
# 2005/08/25  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0
echo "I don't know what is your choise" && exit 0

4、Shell script的预设变量($0, $1...)
[root@linux scripts]# vi sh07.sh
#!/bin/bash
# Program:
#  The program will show it's name and first 3 parameters.
# History:
# 2005/08/25  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script naem is ==> $0"
[ -n "$1" ] && echo "The 1st paramter is ==> $1" || exit 0
[ -n "$2" ] && echo "The 2nd paramter is ==> $2" || exit 0
[ -n "$3" ] && echo "The 3th paramter is ==> $3" || exit 0

[root@linux scripts]# sh sh07.sh theone haha quot
The script naem is ==> sh07.sh
The 1st paramter is ==> theone
The 2nd paramter is ==> haha
The 3th paramter is ==> quot

5、条件判断式
1)利用if .... then
格式1:
if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的指令工作内容;
fi
例子:
[root@linux scripts]# vi sh06-2.sh
#!/bin/bash
# Program:
#  This program will show the user's choice
# History:
# 2005/08/25  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, interrupt!"
exit 0
fi
echo "I don't know what is your choise" && exit 0

格式2:
if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的指令工作内容;
else
当条件判断式不成立时,可以进行的指令工作内容;
fi

如果考虑更复杂的情况,则可以使用这个语法:
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi

例子:
[root@linux scripts]# vi sh06-3.sh
#!/bin/bash
# Program:
#  This program will show the user's choice
# History:
# 2005/08/25  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK, continue"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what is your choise"
fi

2)利用case ..... esac判断
格式:

case $变量名称 in
"第一个变量内容")
程序段
;;
"第二个变量内容")
程序段
;;
*)
不包含第一个变量内容与第二个变量内容的其它程序执行段
exit 1
;;
esac

例子:
[root@linux scripts]# vi sh08-2.sh
#!/bin/bash
# Program:
#  Show "Hello" from $1.... by using case .... esac
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case $1 in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> $0 someword"
;;
*)
echo "Usage $0 {hello}"
;;
esac

一般来说,使用『case $变量 in』或类似语法中,当中的那个 $变量 大致有两种取得的方式:
直接下达式:例如上面提到的,利用『script.sh variable』的方式来直接给予$1这个变量的内容,这也是在/etc/init.d目录下大多数程序的设计方式。
交互式:透过read这个指令来让使用者输入变量的内容。
 
3、function功能
   function也是拥有内建变量的~他的内建变量与shell script很类似,函数名称代表示$0,而后续接的变量也是$1, $2...
格式:
function fname() {
程序段
}

例子:
[root@linux scripts]# vi sh11-3.sh
#!/bin/bash
# Program:
#  Let user input one, two, three and show in screen.
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo "Your choice is $1"
}
echo "This program will print your selection !"
case $1 in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage {one|two|three}"
;;
esac

4、循环
1)while do done, until do done
格式:
while [ condition ]
do
程序段落
done
    这种方式中,while是『当....时』,所以,这种方式是当condition条件成立时,就进行循环直到condition条件不成立才停止。

until [ condition ]
do
程序段落
done
这种方式恰恰与while相反,是当condition条件成立时就终止循环,否则就持续进行循环的程序段

例子:
[root@linux scripts]# vi sh13.sh
#!/bin/bash
# Program:
#  Try to use loop to calculate the result "1+2+3...+100"
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
s=0
i=0
while [ "$i" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"

[root@linux scripts]# vi sh12-2.sh
#!/bin/bash
# Program:
#  Use loop to try find your input.
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
until [ "$yn" == "yes" ] || [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done

2)for(( ))...do....done
格式:
for (( 初始值; 限制值; 执行步阶 ))
do
程序段
done

例子:
[root@linux scripts]# vi sh14.sh
#!/bin/bash
# Program:
#  Try do calculate 1+2+....+100
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"

3)for...in...do...done
格式:
for var in con1 con2 con3 ...
do
程序段
done
1.  第一次循环时, $var 的内容为 con1 ;
2.  第二次循环时, $var 的内容为 con2 ;
3.  第三次循环时, $var 的内容为 con3 ;
4.  ....

例子:
[root@linux scripts]# vi sh16.sh
#!/bin/bash
# Program:
#  let user input a directory and find the whole file's permission.
# History:
# 2005/08/29  VBird  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#先看看这个目录是否存在?
read -p "Please input a directory: " dir
if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi
#开始测试档案
filelist=`ls $dir`
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done

5、shell script的追踪与debug
[root@linux ~]# sh [-nvx] scripts.sh
参数:
-n:不执行script,仅查询语法的问题;
-v:执行sccript前,先将scripts的内容输出到屏幕上;
-x:将使用到的script内容显示到屏幕上。

范例一:测试sh16.sh有无语法的问题?
[root@linux ~]# sh -n sh16.sh
   若语法没有问题,则不会显示任何信息。

范例二:将sh15.sh的执行过程全部列出来
[root@linux ~]# sh -x sh15.sh
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/vbird/bin
+ export PATH
+ for animal in dog cat elephant
+ echo 'There are dogs.... '
There are dogs....
+ for animal in dog cat elephant
+ echo 'There are cats.... '
There are cats....
+ for animal in dog cat elephant
+ echo 'There are elephants.... '
There are elephants....
   使用-x真的是追踪script的好方法,他可以将所有有执行的程序段在执行前列出来,如果是程序段落,则输出时最前面会加上+字号,表示他是程序代码,实际的输出则与standard output。

Shell脚本基础II的更多相关文章

  1. 详细介绍Linux shell脚本基础学习

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...

  2. shell脚本-基础

    shell脚本-基础 编程基础 程序是指令+ 数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell 程序提供了编程能力,解释执行. 计算运行二进 ...

  3. Linux shell脚本基础学习详细介绍(完整版)二

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  4. Linux shell脚本基础学习详细介绍(完整版)一

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...

  5. Shell脚本基础学习

    Shell脚本基础学习 当你在类Unix机器上编程时, 或者参与大型项目如k8s等, 某些框架和软件的安装都是使用shell脚本写的. 学会基本的shell脚本使用, 让你走上人生巅峰, 才怪. 学会 ...

  6. 什么是Shell?Shell脚本基础知识详细介绍

    这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...

  7. shell脚本基础知识

    虽然现在能在Linux系统下生存,但是自觉效率太低,和高手有很大的差距. 这就是关于Linux的知识太过匮乏,有很多事情知道该怎么做,但是就是没法在Linux下实现,为了提升工作效率,必须要接触Lin ...

  8. shell脚本 基础应用

    变量分为普通变量可只读变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ...

  9. 模块一:shell 脚本基础

    一.shell脚本介绍 (一)脚本案例及介绍: #!/bin/bash LOG_DIR=/var/log ROOT_UID=0 if ["$UID -ne "$ROOT_UID&q ...

随机推荐

  1. SharePoint 2010 用xsl文件定制列表样式

    有时候我们不希望列表用默认的方式显示,要我们自定义的方式定制.其中有一种方式是使用xsl文件. 在AllItems.aspx页面中,列表是以webpart的形式显示在页面上的,webpart类型是Xs ...

  2. 3. 戏说VHDL之入门游戏一:流水灯

    一.   流水灯 1.1流水灯原理 流水灯是每个学电子的入门“游戏” ,示意图如图1,其原理极其简单,但是可玩性却极强,可以就8个LED写出不同花样的程序.在1.2中我们列出两个不同思路的代码作为VH ...

  3. ASP.NET Web API 实例

    ASP.NET Web API 入门大杂烩 创建Web API解决方案,命名为VCoinWebApi,并且创建了同名的Project,然后,创建一个Empty Project:Models,创建一个W ...

  4. python的urllib2库详细使用说明

    一直以来技术群里会有新入行的同学提问关于urllib和urllib2以及cookielib相关的问题.所以我打算在这里总结一下,避免大家反复回答同样的问题浪费资源. 这篇属于教程类的文字,如果你已经非 ...

  5. 邻接表实现Dijkstra算法以及DFS与BFS算法

    //============================================================================ // Name : ListDijkstr ...

  6. 设计模式之Birdge(桥接)模式

    1.出现原因 1.同一个类型,有两个变化的维度(两个维度的抽象:一个抽象部分的抽象,一个实现部分的抽象) 2.如何应对这种“多维度的变化”?如何利用面向对象技术来使得同一类型可以轻松地沿着两个方向变化 ...

  7. Android开发随笔1

    由于对Android的不了解所以上网看视频学习 昨天: 配置安卓的开发环境,一开始想直接在www.android.com里下载相应的sdk工具整合包后来因为需要越墙便跟从同学那里要了一份sdk 装jd ...

  8. 《JavaScript高级程序设计》第3章 基本概念

    3.4 数据类型 3.4.1 typeof操作符 var message = 'some string'; console.log(typeof message); // 'string' conso ...

  9. UVALive - 6572 Shopping Malls floyd

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48416 Shopping Malls Time Limit: 3000MS 问题描述 We want to ...

  10. 链路层三种类型的MAC地址

    若需要转载,请注明出处. 我们知道,链路层都是以MAC地址来进行通信双方的地址标识的,如下图:在应用中根据接收方的多寡来进行划分,可分为以下三种: 单播(Unicast) 多播(Multicast) ...