Linux-Shell脚本(1)

shell script介绍

1、shell script

1.1、定义

  • 其实就是纯文本文件,以固定的语法组织起来。
  • 可以编辑这个文件, 让这个文件帮我们 一次执行多个命令。
  • 可以通过一些运算与逻辑判断来帮我们达成某些较复杂的功能。

1.2、运行方式

shell.sh文件必须具备可读与可执行 (rx) 的权限

  • 直接执行:
    • 绝对路径:运行文件所在的绝对路径来执行指令,如/home/zkpk/apps/shell.sh
    • 相对路径:cd到脚本所在目录 ,使用 ./shell.sh 执行
  • bash执行:
    • bash shell.sh
    • sh shell.sh
  • source 执行
    • source shell.sh

1.3、脚本基本格式

#!/bin/bash
# Shows "Hello World!" in your screen.
echo -e "Hello World! \a \n"
exit 0

第一行:基本格式,告知系统执行方式,当采用非sh XXX.sh格式执行时,此行必须。

第二行:注释,#开头,后面的文字内容不参与程序执行。

第三行:基本的输出语句。在屏幕打印"Hello World!"。

第四行:脚本执行完毕的返回语句。可以使用echo $?来获取执行后返回值。

1.4、数值运算

格式:$((运算主体))

实例(乘法运算):

#!/bin/bash
echo -e "Input 2 numbers, I will cross them! \n"
read -p "first number: " firstnu               #键盘录入数字,存到firstnu中
read -p "second number: " secnu                #键盘录入数字2,存到secnu中
total=$(($firstnu*$secnu))                     #进行运算
echo -e "\nThe result of $firstnu x $secnu is ==> $total"        #输出结果

第四行$(($firstnu$secnu)) 为双重括号,若删去一层则以字符串进行运算

1.5、test判断命令

使用范例:test -e demo.txt

  • 判断某个文件类型
    • -e 该文件是否存在
    • -f 该文件是否存在且为文件(file)
    • -d 该文件名是否存在且为目录(directory)
    • -b 该文件是否存在且为一个 block device 装置
    • -c 该文件是否存在且为一个 character device 装置
    • -S 该文件是否存在且为一个 Socket 文件
    • -p 该文件是否存在且为一个 FIFO (pipe) 文件
    • -L 该文件是否存在且为一个连接文件
  • 判断文件权限
    • -r 检查该文件是否存在且具有可读的权限
    • -w 检查该文件是否存在且具有可写的权限
    • -x 检查该文件是否存在且具有可执行的权限
    • -u 检查该文件名是否存在且具有SUID的属性
    • -g 检查该文件名是否存在且具有SGID的属性
    • -k 检查该文件名是否存在且具有Sticky bit的属性
    • -s 检查该文件是否存在且为非空文件
  • 两个文件之间比较
    • -nt 判断file1 是否比 file2 新
    • -ot 判断file1 是否比 file2 旧
    • -ef 判断两个文件是否为同一个文件
  • 整数之间的判断
    • -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)
  • 判断字符串
    • test -z string 判断字符串是否为空?若 string 为空字符串,则为 true
    • test -n string 判断字符串是否非空?若 string 为空字符串,则为 false
    • test str1 = str2 判断str1是否等于str2,若相等,则返回 true
    • test str1 != str2 判断str1是否不等于str2,若相等,则返回 false

实例(判断文件类型,显示其权限):

#!/bin/bash
# Program:
# User input a filename, program will check
# 1. 输入文件名,并判断输入存在
echo -e "Please input a filename, I will check the filename's type and \ permission. \n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
# 2. 判断文件是否存在?不存在结束脚本
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0
# 3. 判断文件类型及属性
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"
# 4. 显示文件信息
echo "The filename: $filename is a $filetype"
echo "And the permissions are : $perm"

1.6、判断符号[]

  • [ 判断条件 ] 两端留有空格

“-o”表示两者条件满足其一即可

“-a”表示两者都得满足。

实例:

#!/bin/bash
#This program shows the user's choice
read -p "Please input (Y/N): " temp
[ "$temp" == "Y" -o "$temp" == "y" ] && echo "OK, continue" && exit 0

2、shell script参数

2.1、shell script 默认参数

如:调用语句如下

./shell.sh con1 con2 con3 con4
$0为./shell.sh  $1为con1  $2为con2  $3为con3 $4位con4

实例:

#!/bin/bash
echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> $1"
echo "The 2nd parameter ==> $2"

运行界面:

[zkpk@node05 test_linux]$ ./p4.sh 1 2
The script name is ==> ./p4.sh
Total parameter number is ==> 2
Your whole parameter is ==> '1 2'
The 1st parameter ==> 1
The 2nd parameter ==> 2

2.2、参数偏移shift

实例解释:

./p5.sh one two three four five six
#!/bin/bash
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
#shift一个变量
shift
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 #shift三个变量
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"

运行结果:

[zkpk@node05 test_linux]$ ./p5.sh one two three four five six
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2
Your whole parameter is ==> 'five six'

使用shift后,变量位置发生偏移,在第一次shift一个变量后,变量$1从one变成了two。以此类推。

你可能感兴趣的:(Linux-Shell脚本(1))