Tôi mới bắt đầu nhưng đã thực hiện rất nhiều PHP và Javascript.BASH: Nó có hỗ trợ các biến điều kiện như var = "test" không? "1": "2"
Có một số loại tương đương với PHP này không?
$default = 10;
$var = (!$var) ? $default : $var;
Cảm ơn
Tôi mới bắt đầu nhưng đã thực hiện rất nhiều PHP và Javascript.BASH: Nó có hỗ trợ các biến điều kiện như var = "test" không? "1": "2"
Có một số loại tương đương với PHP này không?
$default = 10;
$var = (!$var) ? $default : $var;
Cảm ơn
Vâng, nó:
var=${var:-10}
Ngay cả với biến khác:
unset var
export def=99
echo ${var:-${def}} # gives '99'
export var=7
echo ${var:-${def}} # gives '7'
Có!
Từ trang người đàn ông:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word
is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of word
is assigned to parameter. The value of parameter is then substituted.
Positional parameters and special parameters may not be assigned to in this way.
${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to
that effect if word is not present) is written to the standard error and the shell, if it is not inter‐
active, exits. Otherwise, the value of parameter is substituted.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of
word is substituted.
$ default=10
$ var=${var:-$default}
$ echo $var
10
$ var=9
$ var=${var:-$default}
$ echo $var
9
đặt chính xác điều này: http://bash.pastebin.com/f4e14cd53 vào một tệp và thực thi nó không có gì liên quan đến cửa sổ bảng điều khiển. –
Tôi phải làm điều gì đó sai, điều đó thực sự làm việc –
btw, câu trả lời của bạn cũng tuyệt vời! Tôi chỉ thích các dấu ngoặc phụ;) –
hoàn hảo! Chính xác những gì tôi đang tìm kiếm. Cần phải sử dụng * các biến * khác để chắc chắn. –
Nói đúng cách, 'xuất 'không cần thiết ở đây. –