leafee98-blog/content/posts/shell-expansions/if-null.sh
2023-02-03 22:24:18 +08:00

29 lines
1.5 KiB
Bash

#!/usr/bin/env bash
normal_v="O"; null_v=""; unset unset_v
echo -e "Pre-define: normal_v="O"; null_v=""; unset unset_v\n"
printf "\${var:+X} normal_v:%1s null_v:%1s unset_v:%1s\n" "${normal_v:+X}" "${null_v:+X}" "${unset_v:+X}"
printf "\${var+X} normal_v:%1s null_v:%1s unset_v:%1s\n\n" "${normal_v+X}" "${null_v+X}" "${unset_v+X}"
printf "\${var:-X} normal_v:%1s null_v:%1s unset_v:%1s\n" "${normal_v:-X}" "${null_v:-X}" "${unset_v:-X}"
printf "\${var-X} normal_v:%1s null_v:%1s unset_v:%1s\n\n" "${normal_v-X}" "${null_v-X}" "${unset_v-X}"
printf "\${var:=X} normal_v:%1s null_v:%1s unset_v:%1s\n" "${normal_v:=X}" "${null_v:=X}" "${unset_v:=X}"
printf " After normal_v=%1s null_v=%1s unset_v=%1s\n" "${normal_v}" "${null_v}" "${unset_v}"
normal_v="O"; null_v=""; unset unset_v # reset variables
printf "\${var=X} normal_v:%1s null_v:%1s unset_v:%1s\n" "${normal_v=X}" "${null_v=X}" "${unset_v=X}"
printf " After normal_v=%1s null_v=%1s unset_v=%1s\n\n" "${normal_v}" "${null_v}" "${unset_v}"
normal_v="O"; null_v=""; unset unset_v # reset variables
printf "\${var:?X} ---\n"
printf " normal_v : ${normal_v:?X}\n"
printf " null_v : \${null_v:?X} # skip, or the script will be interrupted\n"
printf " unset_v : \${unset_v:?X} # skip, or the script will be interrupted\n"
printf "\${var?X} ---\n"
printf " normal_v : ${normal_v?X}\n"
printf " null_v : ${null_v?X}\n"
printf " unset_v : \${unset_v?X} # skip, or the script will be interrupted\n"