第13章 学习shell script
由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示
第一个script
[root@localhost script]# cat -n sh01.sh
#!/bin/bash
#Program:
#This program shows "Hello World!" in your screen.
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH;
echo -e "Hello World! \a \n"
exit
第一行:#!/bin/bash 声明这个script使用的shell名称
第二三行:注释
第四行:环境变量的声明,这样程序执行时可以直接执行一些外部命令,而不必写绝对路径
第五行:使环境变量生效
第六行:主程序部分
第七行:使用exit命令让程序中断,并且传回一个数值给系统,执行完该script后,使用echo ¥?可以得到该值。
[root@localhost script]# vi sh01.sh
[root@localhost script]# ./sh01.sh
Hello World! //查看上面script返回给系统的值
[root@localhost script]# echo ¥?
简单的shell script练习
交互式脚本:变量内容由用户确定
[root@localhost script]# cat -n sh02.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH read -p "please input your first name:" firstname #tell user to input their name
read -p "please input your last name:" lastname
echo -e "\n Your full name is:$firstname $lastname"
[root@localhost script]# sh sh02.sh
please input your first name:wu
please input your last name:chao Your full name is:wu chao
[root@localhost script]#
随日期变化:利用日期创建文件
//查看script脚本
[root@localhost script]# cat sh03.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH echo -e "I will use 'touch' command to create 3 files."
read -p "please input your file name:" fileuser filename=¥{fileuser:-"filename"}
date1=¥(date -d -2day +%Y%m%d)
date2=¥(date -d -1day +%Y%m%d)
date3=¥(date +%Y%m%d) file1=¥{filename}¥{date1}
file2=¥{filename}¥{date2}
file3=¥{filename}¥{date3} touch "¥file1"
touch "¥file2"
touch "¥file3" //执行该脚本
[root@localhost script]# sh sh03.sh
I will use 'touch' command to create files.
please input your file name:testFile
[root@localhost script]# //查看结果
[root@localhost script]# ls -l testFile*
-rw-r--r--. root root 7月 : testFile20160717
-rw-r--r--. root root 7月 : testFile20160718
-rw-r--r--. root root 7月 : testFile20160719
[root@localhost script]#
注:¥()是执行里面的代码得到的结果。¥{}取变量的值。
数值运算
[root@localhost script]# cat sh04.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH echo -e "please input two numbers:"
read -p "first number:" n1
read -p "second number:" n2 total=¥((¥n1*¥n2))
echo -e "\n The result of ¥n1 * ¥n2 is ==> ¥total"
[root@localhost script]#
[root@localhost script]# sh sh04.sh
please input two numbers:
first number:
second number: The result of * is ==>
[root@localhost script]#
var=¥((运算内容))
用于计算
[root@localhost script]# echo ¥((*))
script的执行方式区别(source,sh script,./script)
利用直接执行的方式执行script
比如使用“sh sh01.sh”来执行脚本时,该脚本会使用一个新的bash环境(子进程)。即脚本实在子进程的bash环境执行的,当执行完后,子进程的变量不会传给父进程。
//查看script脚本中定义了firstname变量
[root@localhost script]# cat sh02.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH read -p "please input your first name:" firstname #tell user to input their name
read -p "please input your last name:" lastname
echo -e "\n Your full name is:$firstname $lastname" //直接执行该脚本
[root@localhost script]# sh sh02.sh
please input your first name:wu
please input your last name:chao Your full name is:wu chao //执行完上面的脚本后,在父进程尝试获取该变量,发现获取不到
[root@localhost script]# echo ¥firstname [root@localhost script]#
利用source执行脚本(父进程中执行)
//查看脚本,含有firstname变量
[root@localhost script]# cat sh02.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH read -p "please input your first name:" firstname #tell user to input their name
read -p "please input your last name:" lastname
echo -e "\n Your full name is:¥firstname ¥lastname" //source方法执行脚本
[root@localhost script]# source sh02.sh
please input your first name:wu
please input your last name:chao Your full name is:wu chao //父进程可以查看到该变量
[root@localhost script]# echo ¥firstname
wu
[root@localhost script]#
善用判断式
利用test命令的测试功能
判断文件是否存在
[root@localhost script]# test -e file1 && echo "exist" || echo "not exsit"
not exsit
[root@localhost script]# test -e sh01.sh && echo "exist" || echo "not exsit"
exist
[root@localhost script]#
常用的测试标注如下表:
文件类型判断
test -e filename
测试标志 | 代表意义 |
-e | 该文件名是否存在 |
-f | 该文件名是否存在且为文件 |
-d | 该文件名是否存在且为目录 |
-b | 该文件名是否存在且为block device设备 |
-c | 该文件名是否存在且为character device设备 |
-S | 该文件名是否存在且为一个Socket设备 |
-p | 该文件名是否存在且为一个FIFO设备 |
-L | 该文件名是否存在且为一个连接文件 |
文件权限检测
test -r filename
测试标志 | 代表意义 |
-r | 检测该文件名是否存在且有可读的权限 |
-w | 检测该文件名是否存在且有可写的权限 |
-x | 检测该文件名是否存在且有可执行的权限 |
-u | 检测该文件名是否存在且有SUID的权限 |
-g | 检测该文件名是否存在且有SGID的权限 |
-k | 检测该文件名是否存在且有Sticky bit的权限 |
-s | 检测该文件名是否存在且为非空白文件 |
两个文件之间的比较
test file1 -nt file2
测试标志 | 代表意义 |
-nt | file1是否比file2新 |
-ot | file1是否比file2旧 |
-ef | file1和file2是否为同一个文件,即是否指向同一个inode |
两个整数直接的判定
test n1 -eq n2
测试标志 | 代表意义 |
-eq | 两数值相等 |
-ne | 两数值不相等 |
-gt | n1>n2 |
-lt | n1<n2 |
-ge | n1>=n2 |
-le | n1<=n2 |
判断字符串数据
测试标志 | 代表意义 |
test -z string | 判断是否为空字符串,空为true |
test -n string | 判断字符串是否为非空,空为false |
test str1=str2 | 判断字符串是否相等 |
test str1!=str2 | 判断字符串是否不相等 |
多重条件判定
测试标志 | 代表意义 |
-a | 两个条件同时成立,如test -r file -a -x file表示file同时具有r与x权限 |
-o | 任意一个条件成立 |
! | 反向状态,如test ! -x file。当file不具有x权限时返回true |
实例:
[root@localhost script]# cat sh05.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH echo -e "please input a filename,i will check the file's type and permision.\n\n"
read -p "input a filename:" filename
test -z ¥filename && echo "you must input a filename" && exit test ! -e ¥filename && echo "¥filename is not exsit!" && exit
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 permissions are: ¥perm"
[root@localhost script]# sh sh05.sh
please input a filename,i will check the file's type and permision. input a filename:sh02.sh
the filename:sh02.sh is a regulare file
and the permissions are: readable writable
[root@localhost script]#
利用判断符号[ ]
[ -z "¥HOME" ] ; echo ¥?
注意中括号两端都有空格符。
当变量比较时,如下:
@localhost script]# s1="hello world"
[root@localhost script]# [ "¥s1" == "hello" ]
而不可以使用[ ¥s1 == "hello"]
实例:
[root@localhost script]# cat sh06.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH echo -e "please input a filename,i will check the file's type and permision.\n\n"
read -p "please input (Y/N):" yn
[ "¥yn" == "Y" -o "¥yn" == "y" ] && echo "OK,continue" && exit
[ "¥yn" == "N" -o "¥yn" == "n" ] && echo "On,interrupt" && exit
echo "I do not know your choice " && exit
[root@localhost script]# sh sh06.sh
please input a filename,i will check the file's type and permision. please input (Y/N):y
OK,continue
[root@localhost script]#
shell script的默认变量(¥0,¥1...)
脚本执行时,后面可以跟多个参数,如下:
sh scriptname.sh opt1 opt2 ...
则¥0表示执行的文件名,¥1表示第一个参数……
¥#:代表参数个数
¥@:代表"¥1","¥2","¥3","¥4"之意,每个变量独立
¥*:代表"¥1c¥2c¥3c¥4",c为分隔符,默认为空格
[root@localhost script]# cat sh07.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH echo -e "The script name is ==> ¥0"
echo -e "Total parameter number is ==> ¥#" [ "¥#" -lt ] && echo "The number of parameter is less than 2.Stop here." && exit
echo "Your whole parameter is ==> '¥@'"
echo "The 1st parameter is ==> ¥1" [root@localhost script]# sh sh07.sh one two three four
The script name is ==> sh07.sh
Total parameter number is ==>
Your whole parameter is ==> 'one two three four'
The 1st parameter is ==> one
[root@localhost script]#
条件判断式
利用if……then
if [ 条件判断式1 ]; then 执行操作 elif [ 条件判断式2 ]; then 执行操作 else 执行操作 fi
利用case...esac判断
当一个变量存在多个既定变量内容时,可以使用该语句针对不同的语句内容执行相应的操作。
case ¥变量名 in
“第一个变量内容”)
程序段
;;
“第二个变量内容”)
程序段
;;
*) #表示所有其他值得情况
程序段
esac
举例:
[root@localhost script]# cat sh08.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH case ¥ in
"hello")
echo "hello,how are you?"
;;
"")
echo "you must input parameters, ex>{¥0 someword}"
;;
*)
echo "Usage ¥0 {hello}"
;;
esac [root@localhost script]# sh sh08.sh wuchao
Usage sh08.sh {hello}
[root@localhost script]# sh sh08.sh hello
hello,how are you?
[root@localhost script]#
利用function功能
function的设置一定要在程序的前面
[root@localhost script]# cat sh08.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH function printit(){
echo -n "Your choice is "
} case ¥ in
"one")
printit;echo ¥ | tr 'a-z' 'A-Z'
;;
"two")
printit;echo ¥ | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ¥0 {one|two|three}"
;;
esac [root@localhost script]# sh sh08.sh one
Your choice is ONE
[root@localhost script]#
循环(loop)
while do done, until do done
while [ condition ] do 程序段落 done
或
until [ condition ] do 程序段落 done
for...do...done
for var in con1 con2 con3...
do
程序段落
done
示例1:循环遍历多个字符串
[root@localhost script]# cat sh08.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH function printit(){
echo -n "Your choice is "
} for animal in dog cat tiger
do
printit;echo "¥{animal}"
done [root@localhost script]# sh sh08.sh
Your choice is dog
Your choice is cat
Your choice is tiger
[root@localhost script]#
示例2:显示/etc/passwd的用户名和id信息
[root@localhost script]# cat sh08.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH function printit(){
echo -n "Your choice is "
}
users=¥(cut -d ':' -f1 /etc/passwd)
for username in ¥users
do
echo -n "id of user is : ";id ¥username #id指令用于获取用户的id信息
echo -e "username is :¥{username} \n"
done [root@localhost script]# sh sh08.sh
id of user is : uid=(root) gid=(root) 组=(root)
username is :root id of user is : uid=(bin) gid=(bin) 组=(bin)
username is :bin id of user is : uid=(daemon) gid=(daemon) 组=(daemon)
username is :daemon id of user is : uid=(adm) gid=(adm) 组=(adm)
username is :adm id of user is : uid=(lp) gid=(lp) 组=(lp)
username is :lp id of user is : uid=(sync) gid=(root) 组=(root)
username is :sync
示例3:遍历数值序列
[root@localhost script]# cat sh09.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH function printit(){
echo -n "Your choice is "
} network="192.168.247.129"
for sitenu in ¥(seq )
do
ping -c -w ¥{network}.¥{sitenu} &> /dev/null && result= || result= if [ "¥result"== ];then
echo "Server ¥{network}.¥{sitenu} is UP."
else
echo "Server ¥{network}.¥{sitenu} is DOWN."
fi
done [root@localhost script]# sh sh09.sh
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
Server 192.168.247.129. is UP.
for...do...done的数值处理
for ((初始值;限制值;执行步长))
do
程序段
done
示例
[root@localhost script]# cat sh10.sh
#!/bin/bash
PATH=/usr/local/java/jdk1..0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin
export PATH function printit(){
echo -n "Your choice is "
} read -p "please input a number,I will count for 1+2+2+...+yourinput:" nu s=
for ((i=;i<=¥nu;i=i+))
do
s=¥((¥s+¥i))
done echo "The result of 1+2+3+...+¥nu is ==> ¥s"
[root@localhost script]# sh sh10.sh
please input a number,I will count for +++...+yourinput:
The result of +++...+ is ==>
[root@localhost script]#
shell script的追踪与调试
sh [-nvx] script.sh
参数:
-n:不执行script,仅查询语法问题
-v:在执行script前,先将script的内容输出到屏幕
-x:将使用到的script输出到屏幕
第13章 学习shell script的更多相关文章
- 菜鸟教程之学习Shell script笔记(上)
菜鸟教程之学习Shell script笔记 以下内容是,学习菜鸟shell教程整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-shell.ht ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- 菜鸟教程之学习Shell script笔记(下)
菜鸟教程Shell script学习笔记(下) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- 菜鸟教程之学习Shell script笔记(中)
菜鸟教程Shell script学习笔记(中) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- 学习shell script
摘要:概述.script的编写.test命令.[]判断符号.默认变量($1...).if...then条件判断式. 一.概述 [什么是shell script] 针对shell所写的脚本,将多个命令汇 ...
- 菜鸟的《Linux程序设计》学习—shell script
1. 认识shell script shell script是利用shell的功能缩写的一个"程序",这个程序是使用纯文本文件,将一些shell的语法与命令(含外部命令)写在里面, ...
- 别人的Linux私房菜(13)学习Shell脚本
CentOS6.x以前版本的系统服务启动接口在/etc/init.d/目录下,存放了脚本. Shell脚本因调用外部命令和bash 的一些默认工具,速度较慢,不适合处理大量运算. 执行方式有:直接命令 ...
- 鸟哥的Linux私房菜之学习shell script
运行程序的时候一般都是创建一个子程序来执行,所以子程序中的变量什么的在当前的shell下没法使用,但是如果使用source来执行就可以在当前shell下执行程序 shift 1 去掉第一个参数,后面接 ...
- 第十二章 学习 shell脚本之前的基础知识
http://www.92csz.com/study/linux/12.htm [什么是shell] 简单点理解,就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具.实际上,在shell和 ...
随机推荐
- 韦东山linux学习之ubuntu 9.10 软件源 问题
跟着开发板视频学习,安装了ubuntu9.10,然而由于现在官方已经不再提供软件更新的服务,软件我一直安装不上,搞了两天终于解决了. 一.安装VMware,配置等等就不详细说了,安装好系统后,网能连上 ...
- CentOS 7.2重启网络报错 Failed to start LSB: Bring up/down
CentOS 7.2重启网络报错 Failed to start LSB: Bring up/down 我的虚拟机原本有两块网卡,一块叫eno16777736,另一块叫eno5033674.本来是正常 ...
- markupsafe._compat出错的解决办法
在windows下用pip进行安装的flask和freeze会在运行程序的时候出现报错 markupsafe._compat出错,那么此时找到对应的pip文件夹下自己创建一个_compat.py 然后 ...
- 20155231 java实验一 Java开发环境的熟悉
20155231 java实验一 Java开发环境的熟悉 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 完成实验.撰写实验 ...
- 20155301第十一周java课栈程序
20155301第十一周java课栈程序 内容一:后序表达式: abcde/-f+ 内容二:根据填充以下代码: import java.util.Scanner; public class MyDCT ...
- 20155322 2017-2018-1《信息安全系统设计》第九周 Linux命令:pwd命令学习与简单实现
pwd命令学习 功能 Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 格式 pwd [选项] 实例 用 pwd 命令查看默认工作目录的完整路径: 用 pwd 命令查 ...
- 分类问题中的“维数灾难” - robotMax
分类问题中的“维数灾难” - robotMax 在看机器学习的论文时,经常会看到有作者提到“curse of dimensionality”,中文译为“维数灾难”,这到底是一个什么样的“灾难”?本文将 ...
- 【替罪羊树】bzoj3224&luogu3369&cogs1829 [Tyvj 1728]普通平衡树
[替罪羊树]bzoj3224&luogu3369&cogs1829 [Tyvj 1728]普通平衡树 bzoj 洛谷 cogs 先长点芝士 替罪羊树也是一种很好写的平衡树qwq..替罪 ...
- cogs1713 [POJ2774]很长的信息
cogs1713 [POJ2774]很长的信息 原题链接 题解 把两串拼成A+'%'+B+'$'.跑后缀数组然后相邻两点i,i+1不在同一串里就用ht[i]更新答案. 好裸... Code // It ...
- javaweb(三十四)——使用JDBC处理MySQL大数据
一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像.声音.二进制文等. 在实际开发中,有时 ...