2015-09-16 19 views
5

Bir süre okunan satır döngüsü içinde, bu değişken genişletme ${line/device name:}'u görüyorum. Komut dosyasını kendi giriş dosyamda çalıştırmayı denedim ve sadece satırı yazdırıyor.Bash değişken genişleme '/'

Bu genişletmenin ne yaptığını söyleyebilir misiniz?

+0

Hem cevaplar doğru, ama burada cevapları içerisine sonradan referans ve düzenleme için parametre açılımları üzerinde dokümantasyon var: http://www.gnu.org/software/bash/manual/html_node/ bash adam sayfasından Shell-Parameter-Expansion.html –

cevap

4

Değişken adı line'dur./dize yerine koyma içindir, yani "aygıt adı:" ise$line içinde kaldırılmıştır.

> line="a device name: some name" 
> echo ${line/device name:} 
a some name 

Ayrıca # ve %line yılında değiştirmeler için durmak alternatifler, başlar ve sonunu görebilirsiniz. Ayrıca, bu / ikamesinin bash'a özgü bir özellik olduğundan emin olun (ör. ash desteklemiyor, % ve # görünüşte taşınabilir), böylece komut dosyanızın başlangıcında #!/bin/sh yerine #!/bin/bash kullanmalısınız.

+0

'%' ve '#', [POSIX kabuk belirtiminin bir parçası] olarak gerçekten taşınabilir durumdadır (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02). – chepner

+0

Teşekkür ederiz. Mükemmel cevap! – Jimbo

4

device name: alt tabakası kaldırılmış olarak $line döndürür.

${parameter/pattern/string} 
     Pattern substitution. The pattern is expanded to produce a pattern just as in 
     pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all 
     matches of pattern are replaced with string. Normally only the first match is 
     replaced. If pattern begins with #, it must match at the beginning of the 
     expanded value of parameter. If pattern begins with %, it must match at the 
     end of the expanded value of parameter. If string is null, matches of pattern are deleted and the/following pattern may be omitted. If parameter is @ or 
     *, the substitution operation is applied to each positional parameter in turn, 
     and the expansion is the resultant list. If parameter is an array variable 
     subscripted with @ or *, the substitution operation is applied to each member 
     of the array in turn, and the expansion is the resultant list. 
+0

Teşekkür ederim. Mükemmel cevap! – Jimbo

İlgili konular