shell:与内核通信的工具;
script:脚本;
shell script:针对shell所写的脚本。
利用shell功能所写的一个程序。
纯文本文档。
包含shell的语法和命令(含外部命令);
搭配正则表达式、管道命令与数据流重定向
学习shell script的好处?(主要用于服务器相关、系统管理相关)
自动化管理
追踪和管理重要任务
简单入侵侦测功能
连续命令脚本化
简易的数据处理
跨平台支持,易学易用
编写第一个shell程序纯文本
分析运行:从上到下、从左到右
命令与选项间的多个空白会被忽略
空白行也会被忽略,tab视作空格
如果读取到回车,则尝试运行
一行内容过多,可以用“"来延伸至下一行
#为注释,其后的数据会被忽略
/home/teacher/demo.sh
直接命令运行
demo.sh文件比如具有可读和可运行(rx)权限。
绝对路径:/home/teacher/demo.sh
相对路径:工作目录在/home/teacher时,./demo.sh
以bash程序来运行:
bash demo.sh或sh demo.sh 来运行
echo -e "Hello World!n"
ll -d first.sh
#!/bin/bash:解释器
#:注释
程序主要部分
运行结果告知: exit n
编写shell程序的良好习惯
Script的说明
注释
缩进
Shell Script练习
1.对话式脚本
变量内容由使用者确定;
read
#!/bin/bash read -p "please write first name:" fn read -p "please write last name:" ln echo "your full name is $fn$ln" exit 0
2.创建随日期变化的文件脚本 (?)
3.数值运算
#!/bin/bash read -p "please input a:"a read -p "please input b:"b var=$(($a+$b)) echo $var exit 0Shell的判断式
$?
&&
||
判断目录是否存在:ls 数据重定向 $?
利用命令:test
利用符号:[ ]
test -e /home && echo "exist" || echo "not exist"
test需要通过$?、&&、||显示结果。
test案例
1.这个文件是否存在,如果不存在则返回一个file does not exist的信息并中断程序。
2.如果这个文件存在,则判断它是文件还是目录,并相应输出filename is a regular file或filename is a directory。
3.判断一下,运行者的身份对这个文件或者目录所拥有的权限,并进行输出。
[][ -z "$HOME" ] ; echo $?
[ "$HOME" == "$MAIL" ]
在中括号[]内的每个组件都需要由空格符分割;
在中括号内的变量,最好都以双引号括起来;
在中括号内的常量,最好都以单括号或双括号括起来。
[ "$name" == "my" ]; echo $?
#!/bin/bash #Program: # This program shows the user's choice. read -p "Please input(Y/N):" yn [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue." && exit 0 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh,interrupt." && exit 0 echo "I don't know what your choice is " && exit 0Shell Script的输入变量
/path/to/script opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
$#:参数的个数
$@:"$1" "$2" "$3" "$4"
$*:"$1 $2 $3 $4"
案例
程序的文件名是是吗?
共有多少个参数?
如果参数个数小于2则告知使用者参数数量太少?
全部参数的内容是什么?
第一个参数是什么?
第二个参数是什么?
#!/bin/bash #Program: # This program shows the script name,parameters... echo "Script name: $0" echo "Total parameter number: $#" [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0 cat <条件判断 单层、简单的条件判断
if [ 条件判断式 ];then
当条件判断成立时,可以运行的内容;
fi
&&:AND;
||:OR;
[ "$yn" == "Y" -o "$yn" == "y" ];
[ "$yn" == "Y" ] || [ "$yn" == "y"];
多重、复杂的条件判断
if [ 条件判断式 ];then
当条件判断成立时,可以运行的内容;
else
当条件判断不成立时,可以运行的内容;
fi
if [条件判断式 -1];then
当条件判断1成立时,可以运行的内容;
elif [ 条件判断式 -2 ]; then
当条件判断2成立时,可以运行的内容;
else
当条件判断1与2均不成立时,可以运行的内容;
fi
case...esac
case $变量名 in
"第一个变量内容”)
程序段
;;
"第二个变量内容”)
程序段
;;
*)
不包含第一个变量与第二个变量内容的其他程序段
;;
esac
函数
function fname()
{
程序段
}
#!/bin/bash #Program: # case and function function printit() { echo -n "Your choice is" } echo "This program will print your choice!" case $1 in "one") echo "Your choice is ONE" ;; "two") printit; echo $1 | tr 'a-z' 'A-Z' ;; "three") printit; echo $1 | tr 'a-z' 'A-Z' ;; *) echo "Usage $0 {one|two|three)" ;; esac循环while do done
until do done
for do done
不定循环while do done
while [ 条件 ](条件成立时持续循环)
do
程序段
done
until do done
util [ 条件 ](条件不成立时持续循环)
do
程序段
done#!/bin/bash #Program: # while while [ "$yn" != "yes" -a "$yn" !="YES" ] do read -p "Please input yesYES to stop this program:"yn done echo "OK!You input the correct answer."#!/bin/bash #Program: # until until [ "$yn" == "yes" -o "$yn" == "YES" ] do read -p "Please input YES/yes to stop this program:"yn done echo "OK!You input the correct answer."固定循环for do done
for var in con1 con2 con3 ...
do
程序段
done
#!/bin/bash #Program: # for in for animal in dog cat elephant do echo "There are ${animal}s..." done#!/bin/bash #Program: # for in sep network="192.168.31" for i in $(seq 1 100) do ping -c 1 $(network).$(1) >/dev/null 2>&1 && res=0 || res=1 if [ [ $res == 0 ] ];then echo "Server ${network}.${i} is UP." else echo "Server ${network}.${i} is DOWN." fi donefor do done的数值处理
for ((初始值;限制值;运行步阶))
do
程序段
done#!/bin/bash #Program: # for numbers read -p "Please input a number, I will count for 1+2+...+your_input:" n s=0 for((i=1;i<=$n;i=i+1)) do s=$(($s+$i)) done echo "The result of '1+2+...+$n" is:$s"



