当前位置: 首页 > news >正文

shell_39.Linux参数测试

参数测试
在 shell 脚本中使用命令行参数时要当心。如果运行脚本时没有指定所需的参数,则可能会出问题:

$ ./positional1.sh 
./positional1.sh: line 5: ((: number <= : syntax error: 
operand expected (error token is "<= ") 
The factorial of is 1 
$

当脚本认为位置变量中应该有数据,而实际上根本没有的时候,脚本很可能会产生错误消息。
这种编写脚本的方法并不可取。在使用位置变量之前一定要检查是否为空:

$ cat checkpositional1.sh 
#!/bin/bash 
# Using one command-line parameter 
# 
if [ -n "$1" ] 
then factorial=1 for (( number = 1; number <= $1; number++ )) do factorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial 
else echo "You did not provide a parameter." 
fi 
exit 
$ 
$ ./checkpositional1.sh 
You did not provide a parameter. 
$ 
$ ./checkpositional1.sh 3 
The factorial of 3 is 6 
$

http://www.lryc.cn/news/205614.html

相关文章:

  • 3D模型格式转换工具HOOPS Exchange助力SIMCON搭建注塑项目
  • Linux_虚拟内存机制
  • 淘宝官方开放平台API接口获得店铺的所有商品、商品id、商品标题、销量参数调用示例
  • Java Spring 通过 AOP 实现方法参数的重新赋值、修改方法参数的取值
  • Real3D FlipBook jQuery Plugin 3.41 Crack
  • Pytorch:model.train()和model.eval()用法和区别,以及model.eval()和torch.no_grad()的区别
  • Linux CentOS 8(firewalld的配置与管理)
  • C复习-指针
  • Runnable和Thread的区别,以及如何调用start()方法
  • 云音乐Android Cronet接入实践
  • Linux dup和dup2
  • Spring Boot实战 | 如何整合高性能数据库连接池HikariCP
  • Spring依赖注入
  • Linux下Jenkins自动化部署SpringBoot应用
  • 【git 学习】--- ubuntu18.04 搭建本地git服务器
  • JAVA电商平台免费搭建 B2B2C商城系统 多用户商城系统 直播带货 新零售商城 o2o商城 电子商务 拼团商城 分销商城
  • Android 13 Framework 裁剪
  • 【Axios封装示例Vue2】
  • k8s-----20、持久化存储--PV/PVC
  • python matplotlib 生成矢量图
  • 机器学习中常见的特征工程处理
  • Spring IOC 和 AOP
  • echarts插件-liquidFill(水球图)
  • c++ vscode cmake debug for mac
  • 17 结构型模式-享元模式
  • 创建Secret(手动)
  • 基于PHP的线上购物商城,MySQL数据库,PHPstudy,原生PHP,前台用户+后台管理,完美运行,有一万五千字论文。
  • Lua 事件触发机制(注册,触发)
  • c++ 并发与多线程(12)线程安全的单例模式-1
  • Python学习笔记--迭代