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. jQuery 获取 select 值和文本

    jQuery("#select1").val();是取得选中的值, jQuery("#select1").text();就是取得的文本.

  2. JAVA类与对象(十)-----抽象类

    在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对象之外, ...

  3. 实例详细说明linux下去除重复行命令uniq

    地址:http://blog.51yip.com/shell/1022.html 一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以就要去除掉.linux下有其他命令可以去除重复行,但 ...

  4. 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈

    代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...

  5. 数码管字符产生器GenSym 1.0发布

    本软件可以实现以下功能: 1.支持共阴极和共阳极数码管的字符代码的生成. 2.支持C语言和ASM语言方式产生字符串代码的序列. 3.可定制数码管的最高位和最低位的代码产生次序. 4.支持记忆功能,可以 ...

  6. W3C和IE中的事件处理函数

    在上一篇文章中提到了关于传统的JS中注册事件对象的一些缺点和问题,下面是关于DOM2级的现代事件绑定.本文中设计到的HTML文件在文章最后 一.W3C事件处理函数 “DOM2 级事件”定义了两个方法, ...

  7. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  8. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  9. jquery 常用组件的小代码

    获得所有复选框的值 function getAllValue() { var str=""; $("input[name='checkbox']:checkbox&quo ...

  10. Swift-1-基本概念

    // Playground - noun: a place where people can play // 通过代码快速了解swift常用知识,需要一定object-c基础 import UIKit ...