菜鸟学Linux 第016篇笔记 bash脚本之文件测试,变量类型

简介:

菜鸟学Linux 第016篇笔记 bash脚本之文件测试,变量类型






测试方法

[ EXPression ]  命令测试法

` EXEpression ` 关键字测试

test EXEression


bash中常用的条件测试三种:

整数测试:(双目测试)

-gt 大于

-le 小于等于

-ne 不等于

-eq 等于

-ge 大于等于

-lt 小于

e.g.

INT1=63

INT2=77

[ $INT1 -eq $INT2 ]

[[ $INT -eq $INT2 ]]

test $INT1 -eq $INT2


文件测试:(单目测试)

-e FILE; 测试文件是否存在(existed)

-f file; 测试文件是否为普通文件;

-d FILE; 测试指定路径是否为目录(directory);

-r FILE; 测试当前用户对指定文件是否有读取权限;

-w FILE; 测试当前用户对指定文件是否有可写权限;

-x FILE; 测试当前用户对指定文件是否有可执行权限;


[ -e /etc/inittab ]

[ -x /etc/rc.d/rc.sysinit ]



多分支的if语句:

if 判断条件1;then

statement1

...

elif 判断条件1;then

statement2

...

elif 判断条件n;then

statementn

...

else

statement

...

fi

测试脚本是否有语法错误:

bash -n 脚本 含义:查脚本有无语法错误

bash -x 脚本 含义:逐步执行

定义脚本退出状态码

exit: 退出脚本

exit #

如果脚本没有明确定义退出状态码,那么,最后执行的一条命令的退出状态码

即为脚本退出状态码。


bash变量的类型:

本地变量(局部变量) 作用域:当前shell进程

环境变量  作用域:当前shell进程,即其子shell

位置变量 $1 $2 $3 ...

shift n 移位到第n+1个参数;

./script /etc/fstab /etc/inittab

$1 /etc/fstab 表示脚本第一个参数

$2 /etc/inittab 表示脚本第二个参数

特殊变量

$? 上一条命令的退出状态码

$# 参数的个数

$* 参数列表

$@ 参数列表

小练习:写脚本

script1

1、给定一个文件:

如果是一个普通文件,则显示之;

如果是一个目录,亦显示之;

否则,此为无法识别的文件。

script2 

能接受一个参数(文件路径)

判定:此参数如果是一个存在的文件,就显示OK;

否则就显示“No such file."

script3

1、给脚本传递两个参数(整数);

2、显示此两者之和,之积;

key:

script1 

#!/bin/bash

#

FILE=/etc/rc.xxx


if [ ! -e $FILE ]; then 

echo "No such file."

exit 6

fi


if [ -f $FILE ]; then

echo "Common file."

elif [ -d $FILE ]; then 

echo "Directory."

else

echo "Unrecognize file."

fi


script2

#!/bin/bash

#

FILE=$1


if [ $# -eq 0 ]; then

echo "You need to input a parameter."

exit 2

fi



if [ -e $FILE ]; then

echo "OK"

else 

echo "No such file."

fi


script3

#!/bin/bash

#

if [ $# -ne 2 ]; then

echo "Usage: command ARG1 ARG2."

exit 1

fi

echo "The sum is $[$1+$2]."

echo "The xj is $[$1*$2]."

本文转自Winthcloud博客51CTO博客,原文链接http://blog.51cto.com/winthcloud/1862941如需转载请自行联系原作者


Winthcloud

相关文章
|
11月前
|
Linux Shell
linux自动崩溃,模拟测试
该脚本创建一个 systemd 服务和定时器,在系统启动3分钟后触发崩溃。通过向 /proc/sysrq-trigger 写入 "c" 来实现内核崩溃,用于测试系统崩溃后的恢复机制。
266 4
|
8月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
442 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
8月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
933 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
9月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
511 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
9月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
581 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
10月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
674 18
|
9月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
688 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
828 0
|
9月前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
875 0
|
9月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
321 0