Linux学习记录–shell script

shell script

shell script是利用shell的功能所写的一个程序,这个程序使用纯文本文件,将一些shell的语法和命令写在里面,搭配正则表达式,管道命令与数据流重定向等功能,达到我们想要的目的

shell script执行直接命令执行

shell script文件必须具备rx的权限,假设my.sh在 /root下

绝对路径

[root@bogon ~]# /root/my.sh

相对路径

[root@bogon ~]# . /my.sh

bash(sh)命令执行

shell script文件必须具备r的权限

root@bogon ~]# sh my.sh

source或.命令执行

source或. 命令执行与上面执行不同,上面执行shell 都会分配一个子进程来进行,source或. 命令就在本进程执行,也就是说一些非环境变量也能读取到

root@bogon ~]# source my.sh

shell script编写

前面提到shell script中可以使用shell命令,那么shell script格式是怎样的?

shellscript文件 my.sh

#!/bin/bash

#This is my first shell script

echo “hello world”

第1行:必须要以#!/bin/bash开头代表这个文件内的语法使用bash的语法

第2行:注释以#开头

第3行:shell 命令

shell script重要功能test测试命令

用来检测系统上面某些文件或者相关的属性

表示可读否 (但 root 权限常有例外)

-r

侦测该文件名是否存在且具有[可读]的权限?

-w

侦测该文件名是否存在且具有[可写]的权限?

-x

侦测该文件名是否存在且具有[可运行]的权限?

-u

侦测该文件名是否存在且具有[SUID]的属性?

-g

侦测该文件名是否存在且具有[SGID]的属性?

-k

侦测该文件名是否存在且具有[Sticky bit]的属性?

-s

侦测该文件名是否存在且为[非空白文件]?

-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)

-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

举例

shellscript文件内容:

#!bin/bashread -p “please input file/dir name: ” nameecho “your input is $name”test ! -e $name && echo “file is not exist! “&&exit 1test -d $name &&echo “file is directory”test -f $name &&echo “file is regulate file”test -r $name &&echo “you can read this file”test -w $name &&echo “you can write this file”test -x $name &&echo “you can exec this file”exit 0

执行结果

[root@localhost scripts]# sh test.shplease input file/dir name: nyour input is nfile is not exist![root@localhost scripts]# sh test.shplease input file/dir name: sh01.shyour input is sh01.shfile is regulate fileyou can read this fileyou can write this fileyou can exec this file[root@localhost scripts]# ll sh01.sh-rwxr–r– 1 root root 48 03-07 10:15 sh01.sh

测试命令

[]测试命令和test用法一直,只是使用时要注意空格符使用

在中括号内的每个组件又要有空格符分割

在中括号内的变量常量都要使用“”括起来

举例

shellscript文件内容:

#!/bin/bashread -p “please input y/n: ” yn[ -z “$yn” ] &&echo “your input is empty”&&exit 1[ “$yn” == “y” -o “$yn” == “Y” ]&&echo “it is ok”&&exit 0[ “$yn” == “n” -o “$yn” == “N” ]&&echo “it is not ok”&&exit 0echo “you input is $yn .please use input (y/n)”exit 2

执行结果

[root@localhost scripts]# sh [].shplease input y/n:your input is empty[root@localhost scripts]# sh [].shplease input y/n: Yit is ok[root@localhost scripts]# sh [].shplease input y/n: Ayou input is A .please use input (y/n)执行参数

可以在执行shell script时添加参数

sh var.sh first second third

$1 $2 $3

其中 first 作为第一个参数存储在$1中,依次类推。

几个重要的参数:

$#:获取参数的数量

$@:获取参数的整体内容

举例

shellscript文件内容:

#!/bin/bashecho “total parameter number is : $#”echo “whole parameter is : $@”echo “1st parameter is : $1″echo “2st parameter is : $2”

执行结果

[root@localhost scripts]# sh var.sh first second thirdtotal parameter number is : 3whole parameter is : first second third1st parameter is : first2st parameter is : second

if…..then

语法:

If [条件判断式 ] ;then

待执行的shell 命令

fi

举例

shellscript文件内容:

#!/bin/bashread -p “please input y/n: ” ynif [ -z “$yn” ];thenecho “your input is empty”exit 1fiif [ “$yn” == “y” -o “$yn” == “Y” ];thenecho “it is ok”exit 0fiif [ “$yn” == “n” -o “$yn” == “N” ];thenecho “it is not ok”exit 0fiexit 2

执行结果

[root@localhost scripts]# sh ifthen.shplease input y/n:your input is empty[root@localhost scripts]# sh ifthen.shplease input y/n: Yit is ok[root@localhost scripts]# sh ifthen.shplease input y/n: Nit is not okif …else

语法:

If [条件判断式 ] ;then

待执行的shell 命令

else

待执行的shell 命令

fi

if …elif..else

语法:

If [条件判断式 ] ;then

待执行的shell 命令

elif[ 条件判断式 ] ;then

待执行的shell 命令

else

待执行的shell 命令

fi

case …esac

语法:

case$变量 in

“第一个变量内容”)

待执行的shell 命令

;;

“第n个变量内容”)

待执行的shell 命令

;;

*) è最后一个变量内容. *代表所有

待执行的shell 命令

;;

esac

举例

shellscript文件内容:

#!/bin/bashcase $1 in”hello”)echo ” hello how are you!”;;””)echo “you must input parameter!”;;*)echo “parameter is hello,yours is $1”;;esac

在乎的是看风景的心情,旅行不会因为美丽的风景终止。

Linux学习记录–shell script

相关文章:

你感兴趣的文章:

标签云: