variables, quotes and backticks - Please help

variables, quotes and backticks - Please help

Post by Ann » Fri, 05 Sep 2003 05:31:58



Hi.

I have an annoying problem I'm trying to solve the last couple of
days, unsuccessfully. Maybe you guys can help me.

I need to write a csh script which deals with files.

Inside a file, there are filenames, and I should do stuff with these
files.

The following command:

anna> set infiles = "`cut -d: -f2 $argv[1]`"

is supposed to insert the file names in the 2nd column of the file
$argv[1] (columns in this file are seperated by ':') into the list
infiles.

The problem is that the file $argv[1] name can be with whitespaces.
Example: " hello world "
In that case, the command does not work.

Please help.

Thanks alot.

---
Anna

 
 
 

variables, quotes and backticks - Please help

Post by Barry Margoli » Fri, 05 Sep 2003 05:43:47




>Hi.

>I have an annoying problem I'm trying to solve the last couple of
>days, unsuccessfully. Maybe you guys can help me.

>I need to write a csh script which deals with files.

Why does it have to be csh?  As you've discovered, it doesn't deal well
with quoting.

If you can switch to ksh you should be able to resolve your problem.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

variables, quotes and backticks - Please help

Post by Bill Marcu » Fri, 05 Sep 2003 13:04:12


On 3 Sep 2003 13:31:58 -0700, Anna

> Hi.

> I have an annoying problem I'm trying to solve the last couple of
> days, unsuccessfully. Maybe you guys can help me.

> I need to write a csh script which deals with files.

> Inside a file, there are filenames, and I should do stuff with these
> files.

> The following command:

> anna> set infiles = "`cut -d: -f2 $argv[1]`"

> is supposed to insert the file names in the 2nd column of the file
> $argv[1] (columns in this file are seperated by ':') into the list
> infiles.

> The problem is that the file $argv[1] name can be with whitespaces.
> Example: " hello world "
> In that case, the command does not work.

> Please help.

 set infiles = "`cut -d: -f2 \"$argv[1]\"`"

--
Commander Spiral Pyjama Pseudo-Rhinocerous Feline Thingamajig Bill Marcum
(the First)
Ozy and Millie Name Generator http://heifong.phase.org/omname.php

 
 
 

variables, quotes and backticks - Please help

Post by Juergen Hec » Sat, 06 Sep 2003 02:33:24



> Hi.

> I have an annoying problem I'm trying to solve the last couple of
> days, unsuccessfully. Maybe you guys can help me.

> I need to write a csh script which deals with files.

> Inside a file, there are filenames, and I should do stuff with these
> files.

> The following command:

> anna> set infiles = "`cut -d: -f2 $argv[1]`"

> is supposed to insert the file names in the 2nd column of the file
> $argv[1] (columns in this file are seperated by ':') into the list
> infiles.

> The problem is that the file $argv[1] name can be with whitespaces.
> Example: " hello world "
> In that case, the command does not work.

> Please help.

> Thanks alot.

> ---
> Anna

try

set infiles = `cut -d: -f2 $argv[1]:q`

Regards
Juergen

 
 
 

variables, quotes and backticks - Please help

Post by Ann » Sat, 06 Sep 2003 23:05:49



>  set infiles = "`cut -d: -f2 \"$argv[1]\"`"

I tried that, it didn't work.
(the error: Unmatched `.)

The solution I found in the end was to copy the file into a new file
with a non-space name. (which is A VERY BAD SOLUTION)

I hoped maybe somebody had this problem and solved it in a better way.

Next time, ONLY PERL.

Thanks anyway,

---
Anna

 
 
 

variables, quotes and backticks - Please help

Post by Ann » Sat, 06 Sep 2003 23:11:52


Thanks for the reply, but unforchenately it does not do what I need :(

In the 2nd column can contain spaces (example to a line: `hi:hello world`)
In this case, your command won't work.
I thought about putting "" around your statement, but it still didn't work.

Thanks for the help anyway

---
Anna



> > Hi.

> > I have an annoying problem I'm trying to solve the last couple of
> > days, unsuccessfully. Maybe you guys can help me.

> > I need to write a csh script which deals with files.

> > Inside a file, there are filenames, and I should do stuff with these
> > files.

> > The following command:

> > anna> set infiles = "`cut -d: -f2 $argv[1]`"

> > is supposed to insert the file names in the 2nd column of the file
> > $argv[1] (columns in this file are seperated by ':') into the list
> > infiles.

> > The problem is that the file $argv[1] name can be with whitespaces.
> > Example: " hello world "
> > In that case, the command does not work.

> > Please help.

> > Thanks alot.

> > ---
> > Anna

> try

> set infiles = `cut -d: -f2 $argv[1]:q`

> Regards
> Juergen

 
 
 

variables, quotes and backticks - Please help

Post by Willia » Sun, 07 Sep 2003 01:34:08




Quote:

> >  set infiles = "`cut -d: -f2 \"$argv[1]\"`"

> I tried that, it didn't work.
> (the error: Unmatched `.)

Looks OK to me, are you sure you typed a backtick in both
places, and didn't accidentally use a single quote? -Wm
 
 
 

variables, quotes and backticks - Please help

Post by Barry Margoli » Sun, 07 Sep 2003 01:35:55








>> >  set infiles = "`cut -d: -f2 \"$argv[1]\"`"

>> I tried that, it didn't work.
>> (the error: Unmatched `.)

>Looks OK to me, are you sure you typed a backtick in both
>places, and didn't accidentally use a single quote? -Wm

I got the same error with the simpler test case:

% set foo = "`echo \"abc\"`"

I'm using tcsh 6.05.02 (I know it's probably an ancient version).

AFAIK, C shell doesn't support escaping of quotes inside quotes.  Consider:

% echo "\"a"
Unmatched ".

So in the backtick cases, what's happening is that the first quoted string
is ending at the first \", with no match for the initial backtick.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

variables, quotes and backticks - Please help

Post by Chuck Dillo » Sun, 07 Sep 2003 02:01:27




>> set infiles = "`cut -d: -f2 \"$argv[1]\"`"

> I tried that, it didn't work.
> (the error: Unmatched `.)

try this trick I learned long ago...

set DQ='"'
set infiles = "`cut -d: -f2 ${DQ}$argv[1]${DQ}`"

Generally speaking, when you run into nested quoting a strategy that
*might* work with csh/tcsh is to hide some of the complexity in a variable.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

variables, quotes and backticks - Please help

Post by Ian Fitche » Sun, 07 Sep 2003 02:21:06



> Generally speaking, when you run into nested quoting a strategy that
> *might* work with csh/tcsh is to hide some of the complexity in a

 As most of us would write, the strategy is to use Bourne/Korn/Bourne
 Again shell.

 Hey, but what do we know?

Cheers,

        Ian

 
 
 

variables, quotes and backticks - Please help

Post by Chuck Dillo » Sun, 07 Sep 2003 03:43:49




>>Generally speaking, when you run into nested quoting a strategy that
>>*might* work with csh/tcsh is to hide some of the complexity in a

>  As most of us would write, the strategy is to use Bourne/Korn/Bourne
>  Again shell.

>  Hey, but what do we know?

> Cheers,

>         Ian

I agree with you when it comes to scripting but I'm not going to
stonewall someone asking a specific question.

This issue comes up in the composition of aliases used in an
interactive session of csh/tcsh as well.  It's in aliases that I use
the technique.  But I suppose you don't approve of any use of csh/tcsh.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

variables, quotes and backticks - Please help

Post by Barry Margoli » Sun, 07 Sep 2003 04:28:28




Quote:>This issue comes up in the composition of aliases used in an
>interactive session of csh/tcsh as well.  It's in aliases that I use
>the technique.  But I suppose you don't approve of any use of csh/tcsh.

I use tcsh interactively.  I try to keep aliases to a minimum, though.
Most of the things that people seem to do with aliases can be done with
scripts.  I only use aliases when they're the only way to do it, e.g. the
command needs to do something to the current shell's environment or I'm
defining one of tcsh's built-in aliases like "cwdcmd".

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

variables, quotes and backticks - Please help

Post by Chris Matter » Sun, 07 Sep 2003 05:31:11




>> set infiles = "`cut -d: -f2 \"$argv[1]\"`"

> I tried that, it didn't work.
> (the error: Unmatched `.)

> The solution I found in the end was to copy the file into a new file
> with a non-space name. (which is A VERY BAD SOLUTION)

> I hoped maybe somebody had this problem and solved it in a better way.

> Next time, ONLY PERL.

You don't have to be quite that drastic, although perl is nice
scripting language, to be sure.  "Next time, NOT csh!" would be
appropriate though.  You wouldn't be having these problems in
sh/ksh/bash.

                 Chris Mattern

 
 
 

variables, quotes and backticks - Please help

Post by Ian Fitche » Sun, 07 Sep 2003 05:34:39



> I agree with you when it comes to scripting but I'm not going to
> stonewall someone asking a specific question.

 Nor would I.  I tried to recall my csh from, ooh, 13/14 years ago and
 failed miserably to solve Anna's problem.  A problem that is
 trivially soluble in the Bourne or derivative shells.

 My point, though hardly expressed eloquently, was that for the
 benefit of people trying to use Unix well we should indicate where
 shells', and in this case, the [t]csh, limitations lie.  If setting
 an alias provokes a storm of "isn't the csh's quoting system funny"
 then those limits lie not far beyond simply running programs at the
 command line.  On the other hand, recently in this group, doing weird
 things inside Bourne shell aliases makes me say: use a function.  If
 someone asked about managing complex numbers in the shell most of us
 would reply Perl/C/C++ etc..

 I'd rather Anna was able to make progress in solving her problem and
 if that requires shifting to a more useful shell then that's the
 advice we should offer.  If you can solve her problem in [t]csh, all
 well and good, "but look how easy it is in the Bourne shell," should
 follow swiftly.

Quote:> the technique.  But I suppose you don't approve of any use of csh/tcsh.

 When I started out I used the csh and was quite happy with it, even
 writing some little scripts.  But as I looked around scripting was,
 by and large, being done in the Bourne shell.  I hated the syntax
 compared to the csh.  Now, a good deal older and maybe just a little
 wiser I realise that the uncomfortable syntax pays of in terms of the
 elegance and programmability of the shell.  Progress.  For someone
 starting out, csh is fine -- "just don't expect to write too many
 scripts," I'd say.

Cheers,

        Ian

 
 
 

variables, quotes and backticks - Please help

Post by Juergen Hec » Sun, 07 Sep 2003 10:18:36



> Thanks for the reply, but unforchenately it does not do what I need :(

> In the 2nd column can contain spaces (example to a line: `hi:hello world`)
> In this case, your command won't work.
> I thought about putting "" around your statement, but it still didn't work.

> Thanks for the help anyway

> ---
> Anna

% cat datafile
hi:hello world:end
hallo:world
%
% cat doit.csh
#! /bin/csh -f
set infiles = "`cut -d: -f2 $argv[1]`"
echo number of infiles: $#infiles
foreach f ($infiles:q)
   echo $f
end

% doit.csh datafile
number of infiles: 2
hello world
world

Regards
Juergen

 
 
 

1. backticks, awk and quoting - oh my...

Hi,

could someone please help me with my quoting problems?
I'm trying to write a bourne shell function this way:

MAXLOAD=0.5

check_load()
{
    # get a list of machines (just names, without the domain part)
    HOSTS=`sed -ne 's/^[ &$]*\([_a-zA-Z0-9][_a-zA-Z0-9]*\).*/\1/p' $1`

    # awk removes anything from machine names up to "load average:"
    # and prints the machine names with load below $MAXLOAD
    BELOW=`rup -l $HOSTS | awk 'sub(/[^_a-zA-Z0-9].*load average: /, "
") && $2 <= $MAXLOAD { print $1 }'`

But get wrong results, because $MAXLOAD is not expanded (and
awk thinks: "$2 <= 0"). So I try to change the last line to:

    BELOW=`rup -l $HOSTS | awk 'sub(/[^_a-zA-Z0-9].*load average: /, "
") && $2 <= ' $MAXLOAD ' { print $1 }'`

But get:

    awk: cmd. line:2: sub(/[^_a-zA-Z0-9].*load average: /, " ") && $2 <=
    awk: cmd. line:2:                                                  
^ unexpected newline

(is it because sh think that MAXLOAD is "0.5\n"?) So I add quotes:

    BELOW=`rup -l $HOSTS | awk 'sub(/[^_a-zA-Z0-9].*load average: /, "
") && $2 <= ' "$MAXLOAD" ' { print $1 }'`

But get the same error. So I swap single and double quotes:

    BELOW=`rup -l $HOSTS | awk "sub(/[^_a-zA-Z0-9].*load average: /, \"
\") && \$2 <= $MAXLOAD { print \$1 }"`

but it seems that some variables are expanded too early:

    awk: cmd. line:1: sub(/[^_a-zA-Z0-9].*load average: /, " ") &&  <=
0.5 { print /local/lib/boclu11-16.config }
    awk: cmd. line:1:                                               ^
parse error

What could I do? Thank you in advance

Regards
Alex

2. Problem with compiling linux-2.0.17

3. use of quotes in assignment of variable to variable

4. Linux market share by distribution

5. Problem assigning output to variable when using backticks and reading from STDIN

6. Application Troubleshooting: Alternate Methods of Debugging

7. Need csh quoting help, Please!

8. Memory leaking

9. Better variable quoting

10. Bourne shell dosen't seem to rescan for quotes in variables.

11. How do you print out a variable that contains quotes

12. Quoting and variables with rsh in sh.

13. Single quotes in variable