shell脚本

注意:脚本文件第一行必须是“#!/bin/bash”,指定命令解释器bash

1、执行shell脚本
创建脚本文件:/home/shell.sh
#!/bin/bash

echo hello linux
date +%Y/%m/%d-$H:%M:%S

在这里插入图片描述

在这里插入图片描述
此时,shell.sh对于所有者而言,只有读和写的权限,并不是可执行的脚本。

执行脚本有以下三种方式:
(1)直接执行(shell.sh文件必须具备可读与可执行(rx) 的权限)
修改shell.sh的权限:
在这里插入图片描述

绝对路径:运行/home/shell.sh 来执行指令
在这里插入图片描述

相对路径:cd到/home/ ,使用./shell.sh执行
在这里插入图片描述
在这里插入图片描述

(2)source 执行:source shell.sh
说明,此时可以删掉shell.sh脚本第一行的注释#!/bin/bash。
在这里插入图片描述

(3)bash执行或sh执行:bash shell.sh 或 sh shell.sh
在这里插入图片描述

任务1、显示当前用户的主目录
1)编写脚本 vim shell01.sh
#!/bin/bash
#show current user’s home directory

echo “current user’s home: $HOME”
在这里插入图片描述

2)设置权限:chmod u+x shell01.sh
在这里插入图片描述

3)执行脚本:./shell01.sh
在这里插入图片描述
切换到其他用户,看一看是否能够执行该脚本
在这里插入图片描述
不能执行该脚本

要使得切换到另一个用户,比如chen,也能执行该脚本,那么应该修改权限:chmod o+x shell01.sh(要求其他人也有执行权限)
在这里插入图片描述
(注意要回到root用户才可以)

在这里插入图片描述

任务2、编程实现两个数相乘

1)编写脚本:vim multiplication.sh
#!/bin/bash

read -p “a=” a
read -p “b=” b
product= ( ( a ∗ b ) ) e c h o " ((a*b)) echo " ((ab))echo"a * $b = $product"
在这里插入图片描述

2)设置权限:chmod u+x multiplication.sh
在这里插入图片描述

3)执行脚本:./multiplication.sh
在这里插入图片描述

输入两个实数,结果会报错:
在这里插入图片描述

问题:怎么才能计算两个实数的乘积呢?
1)编写脚本:vim multiplication02.sh
#!/bin/bash

read -p “a=” a
read -p “b=” b
product=echo "scale=2;$a*$b" | bc
echo “$a * $b = $product”
在这里插入图片描述

2)设置权限:chmod u+x multiplication02.sh
在这里插入图片描述
3)执行脚本:./multiplication02.sh
在这里插入图片描述

课堂练习:编写脚本operation.sh,可以实现加减乘除运算功能。输入两个整数a,b和一个运算符op(+、-、*、/)。

1)编写脚本:vim operation.sh

#!/bin/bash

read -p “a=” a
read -p “op=” op
read -p “b=” b

product=$((a $op b ) ) e c h o " b)) echo " b))echo"a $op $b = $product"
在这里插入图片描述

说明:$((a o p b ) ) 也 可 以 写 成 op b))也可以写成 opb))((a${op}b)),用花括号将op括起来,那么与a和b之间就可以不用空格了。

2)修改权限:chmod u+x operation.sh
在这里插入图片描述

3)执行脚本:./operation.sh
在这里插入图片描述

2、test命令
(1)判断文件类型
-e (exist)该文件是否存在? !!!
-f (file)该文件是否存在且为文件(file)?!!!
-d (directory)该文件名是否存在且为目录(directory)?!!!
-b (block device)该文件是否存在且为一个块设备?
-c (character device)该文件是否存在且为一个字符设备?
-S (Socket)该文件是否存在且为一个Socket文件?
-p (pipe)该文件是否存在且为一个FIFO(pipe)文件?
-L (link)该文件是否存在且为一个链接文件?
(2)判断文件权限
-r (readable)检查该文件是否存在且具有可读的权限?
-w (writable)检查该文件是否存在且具有可写的权限?
-x(executable)检查该文件是否存在且具有可执行的权限?
-s 检查该文件是否存在且为非空文件?
-u 检查该文件名是否存在且具有SUID的属性?
-g 检查该文件名是否存在且具有SGID的属性?

(3)判断字符串
test -z string 空
test -n string 非空
test str1=str2
test str1!=str2

任务:编写shell脚本,实现如下功能
•先给予提示:please input a filename:
•是否输入了文件名,如果为空提示:you must input a filename,并结束脚本。
•判断文件是否存在?不存在则结束脚本。
•判断文件类型以及统计文件都有哪些权限。
•输出文件类型和文件所有的权限。

1)编写脚本:vim shell02.sh
#!/bin/bash

program: enter a filename and return its type and permissions

echo “Enter a filename and check whether the file exists. If the file exists, return its type and permissions.”

read -p “Enter a filename: " filename
test -z $filename && echo “filename cannot be null” && exit 1
test ! -e KaTeX parse error: Expected 'EOF', got '&' at position 10: filename &̲& echo "filename does not exist” && exit 2

test -f $filename && filetype=“file”
test -d $filename && filetype=“directory”

test -r $filename && perms=“readable”
test -w KaTeX parse error: Expected 'EOF', got '&' at position 10: filename &̲& perms="perms writable"
test -x KaTeX parse error: Expected 'EOF', got '&' at position 10: filename &̲& perms="perms executable"

echo “$filename is a $filetype”
echo “permissions: $perms”
在这里插入图片描述
复制一行可以先按esc再按yy再按p
删除一行按dd

2)修改权限:chmod u+x shell02.sh
在这里插入图片描述

3)执行脚本:./shell02.sh
在这里插入图片描述

(4)两个文件之间的比较
-nt 判断file1 是否比file2 新 (nt: newer than)
-ot 判断file1 是否比file2 旧 (ot: older than)
-ef 判断两个文件是否为同一个文件(ef: equal file)

任务:判断文件test1.txt与test2.txt的新旧与异同。
[root@localhost home]# echo -e “I Love Linux\nYou Love Windows” > test1.txt
[root@localhost home]# echo -e “I learn java\nYou learn php” > test2.txt
在这里插入图片描述
1)编辑脚本:vim test.sh
#!/bin/bash

test test1.txt -nt test2.txt && echo test1.txt is newer than test2.txt
test test1.txt -ot test2.txt && echo test1.txt is older than test2.txt

test test1.txt -ef test2.txt && echo test1.txt and test2.txt are the same file
test ! test1.txt -ef test2.txt && echo test1.txt and test2.txt are different files
在这里插入图片描述

2)设置权限:chmod u+x test.sh
在这里插入图片描述
3)运行脚本:./test.sh
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值