0

Originally from: IF grep finds what it is looking fore do X else Y but the original question was answered.

$@ has the value of "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

The regex is fine, it has worked before one time before i changed some thing

Edit: There is something wrong with

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

I works fine if i use

var1="firefox"

I must have changed something a some point.

Edit: SOLUTION

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

Grep is no longer used.

Community
  • 1
  • 1
Fredrik
  • 299
  • 2
  • 5
  • 12
  • Check your input ($@). I just loaded your script and ran it as ./script Load firefox/3.6.12 and I got the expected output you are looking for. – sampson-chen Oct 23 '12 at 12:47
  • must have a brain freeze. This does not work: echo "Load firefox/6.12.3" | grep -Eio '\s\w*' – Fredrik Oct 24 '12 at 06:24
  • Might be your system. That prints "firefox" to stdout on mine. – sampson-chen Oct 24 '12 at 06:26
  • so strange sins: # echo "Load firefox/6.12.3" | grep -Eio '[^/]*$' Gives: 6.12.3. I have tried several different systems RHEL and SLES – Fredrik Oct 24 '12 at 08:35
  • Solution, skipped grep ans used awk/cut: echo "Load firefox/6.12.3" | awk ‘{print $2}’ | cut -f1 -d”/” – Fredrik Oct 24 '12 at 09:25

1 Answers1

0

try echoing out the find before running it - it does seem you need to trim the text for 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

so try :

var1=${var1//[[:space:]]}
var2=${var2//[[:space:]]}
if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then
V H
  • 8,382
  • 2
  • 28
  • 48
  • The error is in this line var1=$(echo "$@" | grep -Eio '\s\w*') I manually filled the variable with "firefox" and it worked. The regex worked in an external regexprogram called Expresso. – Fredrik Oct 23 '12 at 12:29
  • :) I know which is why I outputted the echo output with firefox in bold above anyhow rather than all the space addition change var1 to var1=$(echo "$abc" | grep -Eio '\s\w*'|sed -e 's/ //g') – V H Oct 23 '12 at 13:42