1. 什么是shell script

1.1 介绍:

什么是 shell script (程序化脚本) 呢?就字面上的意义,我们将他分为两部份。 在『 shell 』部分,我们在bash当中已经提过了,那是一个文字介面底下让我们与系统沟通的一个工具介面。那么『 script 』是啥? 字面上的意义, script 是『脚本、剧本』的意思。整句话是说, shell script 是针对 shell 所写的『剧本!』

什么东西啊?其实, shell script 是利用 shell 的功能所写的一个『程序 (program)』,这个程序是使用纯文字档,将一些 shell 的语法与命令(含外部命令)写在里面, 搭配正规表示法、管线命令与数据流重导向等功能,以达到我们所想要的处理目的。

shell script 更提供阵列、回圈、条件与逻辑判断等重要功能,让使用者也可以直接以 shell 来撰写程序,而不必使用类似 C 程序语言等传统程序撰写的语法呢!

1.2 shell script的良好习惯:

[root@www ~]# mkdir scripts; cd scripts
[root@www scripts]# vi sh01.sh
#!/bin/bash
# Program:
# This program shows "Hello World!" in your screen.
# History:
# 2005/08/23VBirdFirst release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0

在本章当中,请将所有撰写的 script 放置到你家目录的 ~/scripts 这个目录内, 未来比较好管理啦!上面的写法当中,鸟哥主要将整个程序的撰写分成数段,大致是这样:

1、第一行 #!/bin/bash 在宣告这个 script 使用的 shell 名称:

因为我们使用的是 bash ,所以,必须要以『 #!/bin/bash 』来宣告这个文件内的语法使用 bash 的语法!那么当这个程序被运行时,他就能够加载 bash 的相关环境配置档 (一般来说就是 non-login shell 的 ~/.bashrc), 并且运行 bash 来使我们底下的命令能够运行!这很重要的!(在很多状况中,如果没有配置好这一行, 那么该程序很可能会无法运行,因为系统可能无法判断该程序需要使用什么 shell 来运行啊!)

2、程序内容的说明:

整个 script 当中,除了第一行的『 #! 』是用来宣告 shell 的之外,其他的 # 都是『注解』用途! 所以上面的程序当中,第二行以下就是用来说明整个程序的基本数据。一般来说, 建议你一定要养成说明该 script 的:1. 内容与功能; 2. 版本资讯; 3. 作者与联络方式; 4. 建档日期;5. 历史纪录 等等。这将有助於未来程序的改写与 debug 呢!

3、主要环境变量的宣告:

建议务必要将一些重要的环境变量配置好,鸟哥个人认为, PATH 与 LANG (如果有使用到输出相关的资讯时) 是当中最重要的! 如此一来,则可让我们这支程序在进行时,可以直接下达一些外部命令,而不必写绝对路径呢!比较好啦!

4、主要程序部分

就将主要的程序写好即可!在这个例子当中,就是 echo 那一行啦!

5、运行成果告知 (定义回传值)

是否记得我们讨论一个命令的运行成功与否,可以使用 $? 这个变量来观察~ 那么我们也可以利用 exit 这个命令来让程序中断,并且回传一个数值给系统。 在我们这个例子当中,鸟哥使用 exit 0 ,这代表离开 script 并且回传一个 0 给系统, 所以我运行完这个 script 后,若接著下达 echo $? 则可得到 0 的值喔! 更聪明的读者应该也知道了,呵呵!利用这个 exit n (n 是数字) 的功能,我们还可以自订错误信息, 让这支程序变得更加的 smart 呢!

2. 什么是shell script

2.1 使用read输出所输入数据:

#!/bin/bash
# Program:
# This program is used to print your input
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input your xing:" xing
read -p "please input your ming:" ming echo "your name is:$xing $ming"

2.2 使用所输入文件名建立文件:

#!/bin/bash
# Program:
# This program is used to you input name tp create file or dir
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input a file name:" filename
file1="$filename""one"
file2="$filename""two" dir="$filename""three" touch $file1
touch $file2
mkdir $dir

2.3 计算数值:

#!/bin/bash
# Program:
# This program is used to two number*
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input your first number:" firnu
read -p "please input your second number:" secnu
total=$(($firnu*secnu))
echo "$fisnu*$secnu= $total"

3. 善用test判断式 script

3.1 判断文件:

文件名的判断:

test –b File 文件存在并且是块设备文件

test –c File 文件存在并且是字符设备文件

test –e File 文件存在

test –d File 文件存在并且是目录

test –f File 文件存在并且是普通文件

test –p File 文件存在并且是一个命名管道

test –S File 文件存在并且是一个套接字

test –L File 文件存在并且是一个符号链接(同-h)

文件档案权限的侦测:

test –r File 文件存在并且可读

test –w File 文件存在并且可写

test –x File 文件存在并且可执行

test –u File 文件存在并且设置了它的set-user-id位

test –g File 文件存在并且是设置了组ID

test –k File 文件存在并且设置了sticky位

test –s File 文件存在并且大小大于0

文档之间的比较:

test File1 –ef File2 两个文件具有同样的设备号和i结点号

test File1 –nt File2 文件1比文件2 新

test File1 –ot File2 文件1比文件2 旧

3.2 判断整数:

test 整数1 –eq 整数2 整数相等

test 整数 1 –ge 整数2 整数1大于等于整数2

test 整数1 –gt 整数 2 整数1大于整数2

test 整数1 –le 整数 2 整数1小于等于整数2

test 整数1 –lt 整数 2 整数1小于整数2

test 整数1 –ne 整数 2 整数1不等于整数2

3.3 判断字符串:

test –n 字符串 字符串的长度非零则为true

test –z 字符串 字符串的长度为零则为true

test 字符串1=字符串 2 字符串相等则为true

test 字符串1 !=字符串2 字符串不等则为true

3.4 判断表达式:

if test (表达式为真)

if test !表达式为假

test 表达式1 –a 表达式 2 两个表达式都为真

test 表达式1 –o 表达式2 两个表达式有一个为真

#!/bin/bash
# Program:
# This program will show you filetype and permission
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input a file name:" filename
test -z "$filename" && echo"please input something!!!" exit 0
test ! -e "$filename" && echo"the file $filename DO NOT exist!!!" && exit 0 test -f "$filename" && filetype="is a regular file"
test -d "$filename" && filetype="is a directory"
test -p "$filename" && filetype="is a PIPO file"
test -b "$filename" && filetype="is a block file"
test -c "$filename" && filetype="is a character file"
test -S "$filename" && filetype="is a socket file"
test -L "$filename" && filetype="is a link file" test -r "$filename" && per="$per readable"
test -w "$filename" && per="$per writable"
test -x "$filename" && per="$per executable"
test -u "$filename" && per="$per UID"
test -g "$filename" && per="$per GID"
test -k "$filename" && per="$per stick bit" echo "the file $filename you have input $filetype,and the permission is $per" #!/bin/bash
# Program:
# This program will use -a -o to show filetype and permission
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input a file name:" filename
[ -z "$filename" ] && echo"please input something!!!" exit 0
[ ! -e "$filename" ] && echo"the file $filename DO NOT exist!!!" && exit 0 test -r "$filename" -o -w "$filename" && per1="readable or writable" [ -r "$filename" -a -x "$filename" ] && per2="readable and executable" echo "the file $filename is $per1,$per2"

3.5 用[]来代替test:

#!/bin/bash
# Program:
# This program will use [] to show filetype and permission
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input a file name:" filename
[ -z "$filename" ] && echo"please input something!!!" exit 0
[ ! -e "$filename" ] && echo"the file $filename DO NOT exist!!!" && exit 0 [ -f "$filename" ] && filetype="is a regular file"
[ -d "$filename" ] && filetype="is a directory"
[ -p "$filename" ] && filetype="is a PIPO file"
[ -b "$filename" ] && filetype="is a block file"
[ -c "$filename" ] && filetype="is a character file"
[ -S "$filename" ] && filetype="is a socket file"
[ -L "$filename" ] && filetype="is a link file" [ -r "$filename" ] && per="$per readable"
[ -w "$filename" ] && per="$per writable"
[ -x "$filename" ] && per="$per executable"
[ -u "$filename" ] && per="$per UID"
[ -g "$filename" ] && per="$per GID"
[ -k "$filename" ] && per="$per stick bit" echo "the file $filename you have input $filetype,and the permission is $per"

4. 条件判断 script

4.1 if判断:

使用if/else:

#!/bin/sh

read -p "please input (y/Y n/N):" yn

if [ "$yn" = "y" -o "$yn" = "Y" ]; then
echo "ok go on please"
exit 0
fi
if [ "$yn" = "n" -o "$yn" = "N" ]; then
echo "oh ,bye bye"
exit 0
fi
echo "sorry,i don't know your choice $yn"

使用if/elif/else:

#!/bin/bash
read -p "please input (y/Y n/N):" yn if [ "$yn" = "y" -o "$yn" = "Y" ]; then
echo "ok go on please"
exit 0
elif [ "$yn" = "n" -o "$yn" = "N" ]; then
echo "oh ,bye bye"
exit 0
else
echo "sorry,i don't know your choice $yn"
fi

使用if和$1,$0

#!/bin/bash

if [ "$1" == "one" ]; then
echo "this is the first paramter"
elif [ "$1" == "" ]; then
echo "you should input a paramter,just like >$0 someword"
else
echo "only one is allowed to input"
fi

使用if和netstat -tlun| grep ""

#!/bin/sh
echo "this script will to find which service have started"
#to find www service
testing='netstat -tlun| grep ":80"'
if [ -n "$testing" ];then
echo "www server has started"
fi #to find vsftpd service
testing='netstat -tlun| grep ":20"'
if [ -n "$testing" ];then
echo "vsftpd server has started"
fi #to find ssh service
testing='netstat -tlun| grep ":22"'
if [ -n "$testing" ];then
echo "ssh server has started"
fi #to find mail service
testing='netstat -tlun| grep ":25"'
if [ -n "$testing" ];then
echo "mail server has started"
fi

4.2 case判断:

使用case和$1输出

#!/bin/bash

case $1 in
"one")
echo "the paramter is one"
;;
"two")
echo "the paramter is two"
;;
"three")
echo "the paramter is three"
;;
*)
echo "you only can input {one|two|three}"
;;
esac

4.3 函数功能:

#!/bin/bash

function printinfo()
{
echo "you choice is"
}
case $1 in
"one")
printinfo; echo $1 | tr -s 'a-z' 'A-Z'
;;
"two")
printinfo; echo $1 | tr -s 'a-z' 'A-Z'
;;
esac #!/bin/bash function printinfo()
{
echo "you choice is $1,$2"
}
case $1 in
"one")
printinfo "the first one" "successful"
;;
"two")
printinfo "the second one" "successful"
;;
esac

4.4 循环控制(while):

#!/bin/bash
# Program:
# user input y/Y to stop while
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH while [ "$yn" !="y" -a "$yn" !="Y" ]
do
read -p "please input y/Y to stop while:" yn
done

4.5 循环控制(until):

#!/bin/bash
# Program:
# user input y/Y to stop until
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH untile [ "$yn" !="y" -o "$yn" !="Y" ]
do
read -p "please input y/Y to stop while:" yn
done

4.6 循环控制(for):

while、until和for循环进行数据处理

#!/bin/bash
# Program:
# use while to add 1 to 100
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH i=0
s=0 while [ $i -lt 100 ]
do
i=$(($i)+1)
s=$(($s)+($i))
done
echo "1+2+3...+$i=$s" #!/bin/bash
# Program:
# use until to add 1 to 100
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH i=0
s=0 while [ $i = 100 ]
do
i=$(($i)+1)
s=$(($s)+($i))
done
echo "1+2+3...+$i=$s" #!/bin/bash
# Program:
# use for to add 1 to 100
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH i=0
s=0 for ((i=0;i=100;i++))
do
s=$(($s)+($i))
done
echo "1+2+3...+$i=$s"

使用for循环输出动物信息

#!/bin/bash
# Program:
# use for to display some information
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH for animal in cat dog pig
do
case $animal in
"cat")
echo "$animal miao miao jiao"
;;
"dog")
echo "$animal wang wang jiao"
;;
"pig")
echo "$animal pig jiao"
;;
esca
done

使用for循环显示输入目录下所有文件的属性

sp filename可以用来读取某一个文档,然后把其中的某几行拷贝到正在编辑的shell script。

#!/bin/bash
# Program:
# This program will show you filetype and permission
# History:
# 创建时间 创建人 版本信息
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "please input a directory:" dir
if [ -z "$dir" -a ! -d "$dir"];then
echo "you have input nothing or $dir is NOT a dirrctory!"
exit 0
fi
file='ls $dir'
for filename in $file
do
test -f "$dir/$filename" && filetype="is a regular file"
test -d "$dir/$filename" && filetype="is a directory"
test -p "$dir/$filename" && filetype="is a PIPO file"
test -b "$dir/$filename" && filetype="is a block file"
test -c "$dir/$filename" && filetype="is a character file"
test -S "$dir/$filename" && filetype="is a socket file"
test -L "$dir/$filename" && filetype="is a link file" test -r "$dir/$filename" && per="$per readable"
test -w "$dir/$filename" && per="$per writable"
test -x "$dir/$filename" && per="$per executable"
test -u "$dir/$filename" && per="$per UID"
test -g "$dir/$filename" && per="$per GID"
test -k "$dir/$filename" && per="$per stick bit" echo "the file $dir/$filename you have input $filetype,and the permission is $per" filetype=""
per=""
done

linux 基础8-shell script的更多相关文章

  1. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  2. Linux基础之-shell script(变量,运算符,流程控制,函数)

    一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...

  3. Linux基础篇–shell脚本编程基础

    本章内容概要  编程基础  脚本基本格式  变量  运算  条件测试  配置用户环境 7.1 编程基础程序:指令+数据程序编程风格:   过程式:以指令为中心,数据服务于指令   对象式:以数据为中心 ...

  4. Linux基础(五) Shell函数

    Shell 函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action ...

  5. linux基础6-bash shell编程

    1. type [-ta] name 一般情况下,type命令被用于判断另外一个命令是否是内置命令,但是它实际上有更多的用法. 1.1.判断一个名字当前是否是alias.keyword.functio ...

  6. Linux基础之Shell与变量

    一.提出问题 在平时的工作中,我们经常会碰到设置环境的问题,例如将应用的执行路径添加到PATH中,方便程序的执行:在Linux中更多的时候是跟shell打交道,很多通过shell启动的应用或者服务都需 ...

  7. Linux基础(三)Shell test 命令

    Shell test 命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt ...

  8. Linux程序编写shell script的格式

    #!/bin/bash #program # 在此处写下此程序的作用 #History: #此处写下写此程序的时间 作者 版本号 PATH=/bin:/sbin:/usr/bin:/usr/sbin: ...

  9. Linux 基础知识----shell

    1.file title: #!/bin/bash 2.input: echo $1 echo $2 3.if # ifif [ "$1" = "N" ]the ...

  10. 【原创】Linux基础之Shell脚本常用命令

    #!/bin/sh 1 取脚本参数 $# 参数个数$0 当前脚本名$1 第1个参数$n 第n个参数$* 所有参数$@ 所有参数$? 上个命令的状态$$ 当前pid 2 日期 $ dateWed Mar ...

随机推荐

  1. C# using语句 中间出现exception后 using仍然会dispose

    C# using语句,中间出现exception后,using仍然会dispose,因为: using语句 相当于 try-finally{dispose()}

  2. 到处抄来的SUCTF2019 web wp

    0x01 EasySQL 这是一个考察堆叠注入的题目,但是这道题因为作者的过滤不够完全所以存在非预期解 非预期解 直接构造 *,1 这样构造,最后拼接的查询语句就变成了 select *,1||fla ...

  3. 【计算机视觉】BING: Binarized Normed Gradients for Objectness Estimation at 300fps

    BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...

  4. 最新 东方网java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.东方网等10家互联网公司的校招Offer,因为某些自身原因最终选择了东方网.6.7月主要是做系统复习.项目复盘.LeetCo ...

  5. vue中的$listeners属性作用

    一.当组件的根元素不具备一些DOM事件,但是根元素内部元素具备相对应的DOM事件,那么可以使用$listeners获取父组件传递进来的所有事件函数,再通过v-on="xxxx"绑定 ...

  6. [转帖]hdfs hbase hive hbase适用场景

    hdfs hbase hive hbase适用场景 https://www.cnblogs.com/liyulong1982/p/6001822.html Hive 不想用程序语言开发MapReduc ...

  7. Linux下载安装mysql5.7教程

    首先下载mysql 的安装包,可以去官网下载,网址:https://dev.mysql.com/downloads/mysql/ 然后下滑,在这个地方选择你想要安装的版本: 这里我选择的5.7.点击进 ...

  8. GC(Garbage Collection)

    GC(Garbage Collection) GC背景 ​  创建对象会消耗内存,如果不回收对象占用的内存,内存使用率会越来越高,最终出现OutOfMemoryError(OOM) ​  在C++中专 ...

  9. VMware安装windows7系统

    1.进入VMware系统,选择创建新的虚拟机 2.进入安装页面,选择自定义安装 3.选择虚拟机硬件兼容性,选择与自己软件相匹配的硬件兼容性 4.选择下一步后,选择稍后安装操作系统 5.选择客户机操作系 ...

  10. vue.js中 ,回车键实现登录或者提交表单!

    vue的功能非常强大,但是我们作为一个后端开发人员,前端的东西不一定都弄的很明白,今天就给大家介绍一个回车提交表单的真实案例,达到回车登录的效果! @ keyup.enter 实现的效果 <in ...