0

最初来自:IF grep finds what it is looking for do X else Y但最初的问题得到了回答。

$@ 的值为“加载 firefox/3.6.12”

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

正则表达式很好,在我改变一些东西之前它已经工作过一次

编辑:有问题

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

如果我使用,我工作得很好

var1="firefox"

我一定在某个点上改变了一些东西。

编辑:解决方案

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

不再使用 Grep。

4

1 回答 1

0

尝试在运行之前回显 find - 看来您确实需要修剪 var1 var2 的文本

echo "查找 /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2"

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

查找 /app/ firefox -noleaf -maxdepth 1 -type l -o -type 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
于 2012-10-23T10:50:49.593 回答