Linux下Shell编程

shell种类:bash、sh、csh、ksh、tcsh、zsh……
Linux默认的是bash
可以参看/bin目录中的内容

  shell可以把它理解为一种脚本编程语言,什么是shell程序呢? 简单的说shell程序就是一个包含若干行shell或者linux命令的文件。

  现在使用最多的shell脚本一般为sh,我们也可以直接用bash。实际上他们是同一个东西。

  对于基本的Linux操作,可以不需要知道怎样撰写shell脚本,但是应该可以看懂简单的shell脚本。
  shell脚本是一个可以使用的可执行程序,它汇整一些需要操作的一串连续的指令,与那些二进制的执行文件有相同的执行方式。
  linux的很多功能是靠一些脚本程序完成的。
  如果是系统管理员或Linux的开发人员,应该了解相应的脚本知识。请记住:bash既是一个命令,又是一个脚本的解释器,它可以执行一个命令组合成的程序。

脚本执行
scripts脚本执行时bash会根据下面的规则判断执行的步骤:
1、如果读取到一个回车符号( CR ),就尝试开始执行该行命令;
2、如同前面 bash命令提到的,指令间的多个空白会被忽略掉;
3、空白行也将被忽略掉,并且 tab 也是不会被理会的;
4、至于如果一行的内容太多,则可以使用 \ 来延伸至下一行;
5、此外,使用最多的 # 可做为批注.任何加在 # 后面的字符,将全部被视为批注文字而被忽略。

在撰写脚本的好习惯:
1、第一行宣告使用的 shell 为何?(如果不声明使用默认的shell解释,通常应该声明)

#!/bin/sh
#!/bin/bash

2、注明该 script 的内容功能、版本信息、作者、创建文件日期等
3、每一个大步骤的主要功能(也顺便提供自己未来修改时使用)

脚本执行的方法:
1、一个是将该脚本文件改成可以执行的属性,如chmod 755 scripts.file ,然后执行该脚本文件;
2、另一种则是直接以 sh或bash 这个执行文件来执行 script的内容,如:
sh scripts.file
bash scripts.file

建立第一个脚本文件

[root @test test]# vi test01.sh
#!/bin/bash
#  这个脚本 是在屏幕输出Hello ! How are you  ?
# Made by homeyw
hello=Hello\ \!\ How\ are\ you\ \?
echo $hello
[root @test test]# sh test01.sh
Hello !
How are you ?
注意:
1、所有在脚本中的东西,基本规则 ( 如变量设定规则 ) 需要与命令提示符下时
相同;
2、脚本的后缀最好为 .sh 提供他人的认识;
3、并非加上 .sh 就可以是执行文件,还需要查看其属性中是否有 x 这个属性。

比较单引号与双引号的区别的脚本:

[root @test test]# vi test02.sh
#!/bin/bash
# 这个脚本用途在于引用两个变量,顺便比较一下\ " 与 ' 的异同
# Made by homeyw
name=“homeyw"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2

卷标与运算符:
declare 声明变量内容命令
语法: [test @test test]# declare [-afirx]
参数说明:
-a :定义为数组 array
-f :定义为函数 function
-i :定义为整数 integer
-r :定义为只读
-x :定义为通过环境输出变量 (export命令)
范例:
[test @test test]# declare -i a=3
[test @test test]# declare -i b=5
[test @test test]# declare -i c= a b
[test @test test]# echo $c
15

示例:

[root @test test]# vi test03.sh
#!/bin/sh
# This program is used to "declare" variables
number1=2*3+5*13-32+25
declare -i number2=2*3+5*13-32+25
echo "Your result is ==> $number1"
echo "Your result is ==> $number2"
[root @test test]# sh test03-declare.sh
Your result is ==> 2*3+5*13-32+25
Your result is ==> 64

交互式脚本
read命令:
read 的功能就是依据您在键盘输入的内容存放到变量

[root @test test]# read name
homeyw <==这是键盘输入的结果
[root @test test]# echo $name
homeyw
[root @test test]# vi test04-read.sh
#!/bin/bash
# This program is used to "read" variables
#
echo "Please keyin your name, and press Enter to start."
read name
echo "This is your keyin data ==> $name"
[root @test test]# sh test04-read.sh
Please keyin your name, and press Enter to start.
homeyw
This is your keyin data ==> homeyw

定义一个脚本的参数的代号:

[root @test test]# myscript opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
[root @test test]# vi test05
#!/bin/bash
# This program will define what is the parameters
echo "This script's name => $0"
echo "parameters $1 $2 $3"
[root @test test]# sh test05 pa1 pa2 pa3
This script's name => test05
parameters pa1 pa2 pa3

脚本逻辑判断表达式
如何判定某个文件或目录,或者是如何判定程序应该朝向那个方向行进?
1、 关于文件与目录的检测
-f 常用。检测文件 是否存在
-d 常用。检测 目录是否存在
-L 检测是否为一个 symbolic link 的文件
-e 检测某个东西是否存在

2、关于文件的属性检测
-r 检测是否为可读的属性
-w 检测是否为可以写入的属性
-x 检测是否为可执行的属性
-s 检测是否为非空白文件

3、两个文件之间的判断与比较 ;例如 test file1 -nt file2
-nt 第一个文件比第二个文件新
-ot 第一个文件比第二个文件旧
-ef 第一个文件与第二个文件为同一个文件( link 之类的文件)

4、逻辑的 与(and) 或(or)
&& 逻辑的 AND 的意思
|| 逻辑的 OR

运算符号    代表意义
=           等于
!=          不等于
<           小于
>        大于
-eq       等于
-ne       不等于
-lt       小于
-gt       大于
-le       小于或等于
-ge       大于或等于
-a       双方都成立(and-o       单方成立(or-z       空字符串
-n       非空字符串

条件判断语句
条件判断一:if then fi 的方式 :
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then
执行内容程序
elif [ 条件判断三 ]
执行第二段内容程序
else
执行第三段内容程序
fi
  在中括号[ ]里面的是条件表达式,如果是复合表达式的条件判断(如若A及B则C
之类的逻辑判断),那么就需要在两个中括号之间加上 && (and)或者是 || (or)这
样的逻辑表达式才行!如果是多重选择的话,那么就需要以 elif 来新增另一个条
件;如果所有的条件都不适用,则使用 else 来进行最后的执行内容!
注意:
  在 [ ] 当中,只能有一个判断式;
  在 [ ] 与 [ ] 当中,可以使用 && 或 || 来组织判断式;
  每一个独立的组件之间都需要有空格键来隔开。
示例:

[root @test test]# vi test06.sh
#!/bin/bash
echo “Press y to continueread yn
if [ “$yn” = “y” ]; then
echo “script is running...” ;
else
echo “stop!" ;
fi
[root @test test]# sh test06.sh
Press y to continue
y
script is running...
[root @test test]$ sh test06.sh
Press y to continue
n
stop!
[root @test test]# vi test07.sh
#!/bin/bash
# This program is used to study if then
echo "Press y to continue"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo "script is running..." ;
else
echo "STOP!" ;
fi
[root @test test]# vi test08.sh
#!/bin/bash
# set parameters in the if then
# 需要加上 hello 这个参数才会显示正确的。
if [ "$1" = "hello" ]; then
echo "Hello! How are you ?"
elif [ "$1" = "" ]; then
echo "You MUST input parameters"
else
echo "The only accept parameter is hello"
fi

• **条件判断二:使用 case ...esac 的方式**
case 输入方式(string) in
输入方式一)
  程序执行段
    ;;
输入方式二)
  程序执行段
  ;;
  *)
  echo “Usage: {输入方式一|输入方式二}"
  exit 1
  esac
  
输入方式(string)的格式主要有两种:
1、直接输入:就是以“执行文件 + 字符串 ”的方式来执行
的, 字符串可以直接写成 $1 (在执行文件后面直接加入
参数的第一个参数)
2、交互式:就是由屏幕输出可能的项目,然后让使用者输
入,这个通常必须配合 read variable 然后 string 则写成 $
variable的格式!

直接输入方式:

[test @test test]# vi test09.sh
#!/bin/bash
echo "This program will print your selection!"
case $1 in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac

• 交互方式:

[root @test test]# vi test10.sh
#!/bin/bash
echo "Press your select one, two, three"
read number
case $number in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac

循环结构语句
①for (( 条件一; 条件二; 条件三 ))
②for variable in variable1 variable2 …..
③while [ condition1 ] && { || } [ condition2 ] …
④until [ condition1 ] && { || } [ condition2 ] …

[test @test test]# vi test11.sh
#!/bin/bash
# Using for and loop
#
declare -i s
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "The count is ==> $s"
[test @test test]# vi test12.sh
#!/bin/bash
# Using while and loop
#
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
[test @test test]# vi test13.sh
#!/bin/bash
# Using until and loop
#
declare -i i
declare -i s
until [ "$i" = "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
[test @test test]# vi test14.sh
#!/bin/bash
# using for...do ....done
#
LIST="Tomy Jony Mary Geoge"
for i in $LIST
do
echo $i
done
[test @test test]# vi test15.sh
#!/bin/bash
# Using for and loop to read the account of this li
nux server!
#
account=`cut -d ":" -f1 /etc/passwd | sort`
echo "The following is your linux server's accoun
t"
for i in $account
do
echo $i
done
[test @test test]# vi test16.sh
#!/bin/bash
# Using until
#
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
read yn
done
echo "Stop here"

综合示例:逻辑判断式
①先查看一下 /root/test/logical 这个名称是否存在;
②若不存在,则建立一个文件,使用 touch 来建立(建立的是0字节的一个文件),建立完成后离开;
③如果存在的话,判断该名称是否为文件,若为文件则将之删除后建立一个目录,目录名为 logical ,之后离开;
④如果存在的话,而且该名称为目录,则移除此目录!
⑤[test @test test]# vi logical.sh
• #!/bin/bash
if [ ! -e logical ]; then
touch logical
echo “Just make a file logical”
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo “remove file ==> logical”
echo “and make directory logical”
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo “remove directory ==> logical”
exit 1
else
echo “Does here have anything?”
fi

综合示例:显示主机上的端口是否开启

[test @test test]# vi port.sh
主要代码:
ssh=`netstat -an|grep LISTEN|grep :22`
if [ "$ssh" != "" ]; then
echo "SSH is running"
else
echo "SSH is NOT running"
fi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值