【问题标题】:Bash: variable does not get populatedBash:变量没有被填充
【发布时间】:2012-10-23 10:17:06
【问题描述】:

最初来自:IF grep finds what it is looking fore do X else Y,但最初的问题已得到解答。

$@ 的值为“Load 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。

【问题讨论】:

  • 检查您的输入 ($@)。我刚刚加载了您的脚本并将其作为 ./script Load firefox/3.6.12 运行,我得到了您正在寻找的预期输出。
  • 必须是大脑冻结。这不起作用: echo "Load firefox/6.12.3" | grep -Eio '\s\w*'
  • 可能是你的系统。这会将“firefox”打印到我的标准输出。
  • 如此奇怪的罪过:# echo "Load firefox/6.12.3" | grep -Eio '[^/]*$' 给出:6.12.3。我尝试了几种不同的系统 RHEL 和 SLES
  • 解决方案,跳过 grep 并使用 awk/cut: echo "Load firefox/6.12.3" | awk '{打印 $2}' |剪切 -f1 -d”/”

标签: bash variables


【解决方案1】:

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

echo "find /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

find /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

【讨论】:

  • 错误在这一行 var1=$(echo "$@" | grep -Eio '\s\w*') 我用“firefox”手动填充变量并且它工作。正则表达式在名为 Expresso 的外部正则表达式程序中工作。
  • :) 我知道这就是为什么我用firefox在上面以粗体输出回显输出而不是所有空间添加将var1更改为var1=$(echo "$abc" | grep -Eio '\ s\w*'|sed -e 's/ //g')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多