Ray Parrish | 21 Jan 2010 11:58

using awk on a variable

Hello,

I have the following code which is not working -

           ReturnValue="`ls -als "$Package"`"
           if [[ "$ReturnValue" != "" ]]
                then
                     DateTime="`awk "$ReturnValue" { print $7, $8 }`"
                     echo "$DateTime"
                     return
           fi

The error returned is as follows -

awk: 592 -rwxr-xr-x 1 root root 601056 2009-01-29 04:51 gedit
awk: 
             ^ syntax error

I imagine that somehow I am passing the variable to awk incorrectly. I 
need the 7th, and 8th arguments of the string
variable $ReturnValue, and it will contain the string shown in the error 
message above.

Can someone please enlighten me as to the proper way of doing this?

Thanks, Ray Parrish
Bret Hughes | 21 Jan 2010 16:28

Re: using awk on a variable


Ray Parrish wrote:
> Hello,
>
> I have the following code which is not working -
>
>           ReturnValue="`ls -als "$Package"`"
>           if [[ "$ReturnValue" != "" ]]
>                then
>                     DateTime="`awk "$ReturnValue" { print $7, $8 }`"
>                     echo "$DateTime"
>                     return
>           fi
>
> The error returned is as follows -
>
> awk: 592 -rwxr-xr-x 1 root root 601056 2009-01-29 04:51 gedit
> awk:             ^ syntax error
>
> I imagine that somehow I am passing the variable to awk incorrectly. I 
> need the 7th, and 8th arguments of the string
> variable $ReturnValue, and it will contain the string shown in the 
> error message above.
>
> Can someone please enlighten me as to the proper way of doing this?
>
> Thanks, Ray Parrish
>
Ray, try this.  there are several things getting in the way of what you 
are trying to do with putting the stuff in a variable.  No newlines for 
(Continue reading)

Ray Parrish | 21 Jan 2010 17:06

Re: using awk on a variable

Bret Hughes wrote:
> 
> 
> Ray Parrish wrote:
>> Hello,
>>
>> I have the following code which is not working -
>>
>>           ReturnValue="`ls -als "$Package"`"
>>           if [[ "$ReturnValue" != "" ]]
>>                then
>>                     DateTime="`awk "$ReturnValue" { print $7, $8 }`"
>>                     echo "$DateTime"
>>                     return
>>           fi
>>
>> The error returned is as follows -
>>
>> awk: 592 -rwxr-xr-x 1 root root 601056 2009-01-29 04:51 gedit
>> awk:             ^ syntax error
>>
>> I imagine that somehow I am passing the variable to awk incorrectly. I 
>> need the 7th, and 8th arguments of the string
>> variable $ReturnValue, and it will contain the string shown in the 
>> error message above.
>>
>> Can someone please enlighten me as to the proper way of doing this?
>>
>> Thanks, Ray Parrish
>>
(Continue reading)

Ken Irving | 21 Jan 2010 19:08
Picon

Re: using awk on a variable

On Thu, Jan 21, 2010 at 02:58:48AM -0800, Ray Parrish wrote:
> Hello,
> 
> I have the following code which is not working -
> 
>           ReturnValue="`ls -als "$Package"`"
>           if [[ "$ReturnValue" != "" ]]
>                then
>                     DateTime="`awk "$ReturnValue" { print $7, $8 }`"
>                     echo "$DateTime"
>                     return
>           fi
> 
> The error returned is as follows -
> 
> awk: 592 -rwxr-xr-x 1 root root 601056 2009-01-29 04:51 gedit
> awk:             ^ syntax error
> 
> I imagine that somehow I am passing the variable to awk incorrectly.
> I need the 7th, and 8th arguments of the string
> variable $ReturnValue, and it will contain the string shown in the
> error message above.
> 
> Can someone please enlighten me as to the proper way of doing this?

'This' seems to be pretty complicated, with backtics at two different
levels protected by quotes to run different processes within each other.
Maybe all that can work, I don't know.

Your awk command doesn't appear well-formed to me.  It looks like it's
(Continue reading)

Ray Parrish | 22 Jan 2010 02:26

Re: using awk on a variable

Ken Irving wrote:
> On Thu, Jan 21, 2010 at 02:58:48AM -0800, Ray Parrish wrote:
>> Hello,
>>
>> I have the following code which is not working -
>>
>>           ReturnValue="`ls -als "$Package"`"
>>           if [[ "$ReturnValue" != "" ]]
>>                then
>>                     DateTime="`awk "$ReturnValue" { print $7, $8 }`"
>>                     echo "$DateTime"
>>                     return
>>           fi
>>
>> The error returned is as follows -
>>
>> awk: 592 -rwxr-xr-x 1 root root 601056 2009-01-29 04:51 gedit
>> awk:             ^ syntax error
>>
>> I imagine that somehow I am passing the variable to awk incorrectly.
>> I need the 7th, and 8th arguments of the string
>> variable $ReturnValue, and it will contain the string shown in the
>> error message above.
>>
>> Can someone please enlighten me as to the proper way of doing this?
> 
> 'This' seems to be pretty complicated, with backtics at two different
> levels protected by quotes to run different processes within each other.
> Maybe all that can work, I don't know.
> 
(Continue reading)

Cameron Simpson | 22 Jan 2010 02:49
Picon
Picon
Gravatar

Re: using awk on a variable

On 21Jan2010 02:58, Ray Parrish <crp@...> wrote:
| I have the following code which is not working -
| 
|           ReturnValue="`ls -als "$Package"`"

The first thing you should do is remove these bogus quotes.
Write this:

  ReturnValue=`ls -als "$Package"`

and the same for the DateTime assignment.

The line you have:
  ReturnValue="`ls -als "$Package"`"
is a string in three parts as follows:

  `ls -als 

in double quotes, then

  $Package

not in any quotes, then:

 `
in double quotes.

This means that the first backtick is a syntax error. Depending on the
shell, it may not get run or it may run the command:

(Continue reading)

Ken Irving | 22 Jan 2010 03:24
Picon

Re: using awk on a variable

On Thu, Jan 21, 2010 at 05:26:30PM -0800, Ray Parrish wrote:
> Ken Irving wrote:
> >On Thu, Jan 21, 2010 at 02:58:48AM -0800, Ray Parrish wrote:
> ...
> I guess I will not use your method, as I need to check to insure
> that ls actually returns something,
> and exit a loop if it does. I'm looping through all of the paths in
> the PATH environment variable, searching for the
> package's installed date, and time.
> 
>      while [[ "$1" != "" ]]; do
>           cd "$1"
>           ReturnValue="`ls -als "$Package"`"
>           if [[ "$ReturnValue" != "" ]]
>                then
>                     DateTime="`echo $ReturnValue | awk '{ print $7, $8 }'`"
>                     echo "$DateTime"
>                     return
>           fi
>           shift
>      done

Unless you're doing something more with $DateTime, these two lines:

                    DateTime="`echo $ReturnValue | awk '{ print $7, $8 }'`"
                    echo "$DateTime"

could be simply:

                    echo $ReturnValue | awk '{ print $7, $8 }'
(Continue reading)

Todd A. Jacobs | 23 Jan 2010 02:15
Favicon

Re: using awk on a variable

On Fri, Jan 22, 2010 at 12:49:58PM +1100, Cameron Simpson wrote:

>   ReturnValue=`ls -als "$Package"`

What happens to word-splitting when you don't enclose the entire result
in quotes? I'm playing around with this a bit, and it seems like:

    - Output from command-substitution is not re-split.
    - Quotes are needed to properly display word-splitting of the
      resulting variable.

This isn't quite what I expected. I tested with:

    set -x
    foo=$(echo foo; echo bar)
    echo $foo	# returns "foo bar"
    echo "$foo" # returns "foo<CR>bar"

but I got the exact same results with:

    foo="$(echo foo; echo bar)"

It would seem that the quotes are simply not necessary at all, when
trying to store the results of command substitution. That really, really
surprised me. :)

--

-- 
"Oh, look: rocks!"
	-- Doctor Who, "Destiny of the Daleks"
(Continue reading)

Cameron Simpson | 23 Jan 2010 02:45
Picon
Picon
Gravatar

Re: using awk on a variable

On 22Jan2010 17:15, Todd A. Jacobs <nospam@...> wrote:
| On Fri, Jan 22, 2010 at 12:49:58PM +1100, Cameron Simpson wrote:
| >   ReturnValue=`ls -als "$Package"`
| 
| What happens to word-splitting when you don't enclose the entire result
| in quotes?

Nothing. The word splitting phase has already taken place during the
parse, before the backticks are run. Which means it's perfectly safe.
The shell knows it's doing a variable assignment. The string from the
backticks gets put into the variable; job done.

You're probably being confused by this:

  echo `echo foo; echo bah`

This is parsed into:
  - a command line with some backticks; it runs from the first "echo"
    to the newline
  - _after_ deciding where the command it, the backticks get evaluated
    so that the command text is
      echo foo
      bah
    But that's not two commands, just one, because the shell already
    measured the start and end of the echo command earlier.
  - after whitespace word splitting, the command line strings are
    "echo" "foo" "bah" (not shell quotes:)
    and thus the echo command gets two arguments, "foo" and "bah"

| I'm playing around with this a bit, and it seems like:
(Continue reading)

Kaushal Shriyan | 23 Jan 2010 08:05
Picon

How can I get the newest (or oldest) file from a directory?

http://mywiki.wooledge.org/BashFAQ/099

Gmane