2014-01-21 51 views
0

我不擅長Linux,如果問題很基本,我很抱歉。關於在bash中使用文本剝離斜槓

我需要一個bash shell命令,它將從/ home/barco/ext/linux-crp/bin/linux-x86-32 /中返回/ home/barco/ext/linux-crp。

我讀參數擴展但無法得到任何提示。

任何幫助將不勝感激。

回答

0

不完全清楚自己想要什麼是基於,但

> string="/home/barco/ext/linux-crp/bin/linux-x86-32/"; echo "${string%/*/*/*}" 
> /home/barco/ext/linux-crp 

將返回路徑向上兩級(假設路徑/結束,足夠長)

${parameter%word} 
${parameter%%word} 
    Remove matching suffix pattern. The word is expanded to produce 
    a pattern just as in pathname expansion. If the pattern matches 
    a trailing portion of the expanded value of parameter, then the 
    result of the expansion is the expanded value of parameter with 
    the shortest matching pattern (the ``%'' case) or the longest 
    matching pattern (the ``%%'' case) deleted. If parameter is @ 
    or *, the pattern removal operation is applied to each posi‐ 
    tional parameter in turn, and the expansion is the resultant 
    list. If parameter is an array variable subscripted with @ or 
    *, the pattern removal operation is applied to each member of 
    the array in turn, and the expansion is the resultant list. 
+0

謝謝很多!是的,這是我需要的... –