那些年写过的shell-----判断用户变量

本文介绍了一个Bash脚本示例,展示了如何使用Bash的变量扩展特性来处理空值和默认值的情况,以及如何在变量为空时显示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /bin/bash
################################################
##Author:   freeking
##Created:  July 28th 2013
##Description   replace the word u want
################################################

var1="hello"
var2=
var3=

echo ${var1}
echo ${var2:-world} #if var2 exist and equal null return world
echo ${var2}        #but var2 still equal null

#if var2 exist and equal null put "world" to var2
echo ${var2:=world}

#if var3 exist and equal null put the next message
#catch the error for the undefine parameters
echo ${var1:?"the var3 is null"}
#echo ${var3:?"the var3 is null"}

#if var3 exist and equal null put the next message
echo ${var1:+2}
echo ${var3:+2}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值