← 返回博客列表
发布于 2023.11.27 12:37:05 · 约 31 字

shell命令行参数的判断-和-

${1:?}没有参数就报错return

#!bin/bash
#file name: test1.sh

bar=${1:?param empty}
echo $bar
  • 执行bash test1.sh,报错
test1.sh: line 3: 1: param empty
  • 执行bash test1.sh hi,输出
hi

${1:-}没有则给默认值

#!bin/bash
#file name: test2.sh

bar1=${1:-}
echo bar1:[$bar1]

bar2=${1:-default value}
echo bar2:[$bar2]
  • 执行bash test2.sh,输出
bar1:[]
bar2:[default value]
  • 执行bash test1.sh hi,输出
bar1:[hi]
bar2:[hi]