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. html <input>标签类型属性type(file、text、radio、hidden等)详细介绍

    html <input>标签类型属性type(file.text.radio.hidden等)详细介绍 转载请注明:文章转载自:[169IT-最新最全的IT资讯] html <inp ...

  2. 60.ISE PhysDesignRules ERROR

    PhysDesignRules:2100 - Issue with pin connections and/or configuration on block:<U_ila_pro_0/U0/I ...

  3. sublime mac快捷键

    ^是control ⌥是option 打开/前往 ⌘T 前往文件 ⌘⌃P 前往项目 ⌘R 前往 method ⌘⇧P 命令提示 ⌃G 前往行 ⌘KB 开关侧栏 ⌃ ` python 控制台 ⌘⇧N 新 ...

  4. 细究UTF-8,GB2312及ISO-8859-1区别

    各个国家和地区所制定的不同 ANSI 编码标准中,都只规定了各自语言所需的“字符”.比如:汉字标准(GB2312)中没有规定韩国语字符怎样存储.这些 ANSI 编码标准所规定的内容包含两层含义:1. ...

  5. 用setTimeout 代替 setInterval实时拉取数据

    在开发中,我们常常碰到需要定时拉取网站数据,如: setInterval(function(){ $.ajax({ url: 'xx', success: function( response ){ ...

  6. 开发EXTMVC框架前需要了解的基础知识整理

    1.组件选择器 目的:了解如何选择Extjs中的组件,就跟学习jquery时一定会先要学习:$()选择器一样. 常用场景:       1.在controller中的control事件中用到      ...

  7. 【BZOJ】【1912】【APIO2010】patrol巡逻

    树形DP 说是树形DP,其实就是求树的最长链嘛…… K=1的时候明显是将树的最长链的两端连起来最优. 但是K=2的时候怎么搞? 考虑第一次找完树的最长链以后的影响:第一次找过的边如果第二次再走,对答案 ...

  8. 版本控制 - SVN/TortoiseSVN

    研读了blog: 1. http://www.open-open.com/lib/view/open1346982569725.html  2. http://www.360doc.com/conte ...

  9. 操作集合的工具类Collections

    1       操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...

  10. <context:annotation-config> 跟 <context:component-scan>诠释及区别

    <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注 ...