2012-10-23 93 views
0

原籍:IF grep finds what it is looking fore do X else Y但原來的問題得到回答。擊:變量不填充

$ @擁有的「加載的Firefox/3.6.12」值

var1=$(echo "[email protected]" | grep -Eio '\s\w*') # Gets the application name and put it into var1 
echo $var1 # is not printed should be "firefox" 
var2=$(echo "[email protected]" | grep -o '[^/]*$') # Gets version name and put it into var2 
echo $var2 # prints fine 

正則表達式是好的,它之前的一次前我改變了一些東西的工作

編輯: 有有毛病

var1=$(echo "[email protected]" | grep -Eio '\s\w*') #expected result is: firefox 

我工作正常,如果我使用

var1="firefox" 

我一定改變的東西一些點。

編輯: SOLUTION

echo "Load firefox/6.12.3" | awk ‘{print $2}’ | cut -f1 -d」/」 

的grep不再使用。

+0

檢查輸入($ @)。我剛剛加載腳本並將其作爲./腳本運行。加載firefox/3.6.12,我得到了您期望的輸出。 –

+0

必須有一個腦凍結。這是行不通的: echo「Load firefox/6.12.3」| grep -Eio'\ s \ w *' – Fredrik

+0

可能是您的系統。在我的標準輸出上打印「firefox」。 –

回答

0

嘗試呼應了查找運行它 - 它似乎你需要修剪的VAR1 VAR2

回聲「查找/應用/ $ VAR1 -noleaf -maxdepth 1型-1-鄰型文本d | grep的$ VAR2"

if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then 

查找/應用/ 火狐 -noleaf -maxdepth 1型-1-鄰型d | grep的3.6.12

這樣試試:

var1=${var1//[[:space:]]} 
var2=${var2//[[:space:]]} 
if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then 
+0

錯誤在這一行 var1 = $(echo「$ @」| grep -Eio'\ s \ w *') 我用「firefox」手動填充變量,有效。正則表達式在一個名爲Expresso的外部正則表達式中工作。 – Fredrik

+0

:)我知道這就是爲什麼我在上面加粗輸出回波輸出與Firefox無論如何,而不是所有的空間增加的變動VAR1至VAR1 = $(回聲「$ ABC」 | grep的-EIO「\ S \ W *」 | sed的-e's // // g') – Vahid