shell 中”${b-2}” and “${b:-2}”

來源:互聯網
上載者:User

在看 學習shell中練習的代碼

#!/bin/bash
# User can keyin filename to touch 3 new files.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
export PATH

echo "I will use 'touch' command to create 3 files."
read -p "Please input the filename what you want: " fileuser

filename=${fileuser:+"filename"}

date1=`date --date='2 days ago' +%Y%m%d`
date2=`date --date='1 days ago' +%Y%m%d`
date3=`date +%Y%m%d`

file1="$filename""$date1"
file2="$filename""$date2"
file3="$filename""$date3"

touch $file1
touch $file2
touch $file3

中的
filename=${fileuser:+"filename"}對於:+不是很明白,然後查了下用法。

lx@ubuntu:~/bsh/scripts$ b=""lx@ubuntu:~/bsh/scripts$ c="Z"lx@ubuntu:~/bsh/scripts$ echo a=${a-1}, b=${b-2}, c=${c-3}a=1, b=, c=Zlx@ubuntu:~/bsh/scripts$ echo a=${a:-1}, b=${b:-2}, c=${c:-3}a=1, b=2, c=Zlx@ubuntu:~/bsh/scripts$ 

+----------------------------------------------------------------------+|Form     Meaning       +----------------------------------------------------------------------+|${variable:?word}   Complain if undefined or null       |${variable:-word}   Use new value if undefined or null       |${variable:+word}   Opposite of the above       |${variable:=word}   Use new value if undefined or null, and redefine. +----------------------------------------------------------------------+


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.