Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I would like to do something when I find the '>' character in a file. Here is what I do : file1 contains : aaa >> bbb ccc >> ...
  1. #1
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29

    How to test '>' character in a tcsh script

    Hi,

    I would like to do something when I find the '>' character in a file.
    Here is what I do :

    file1 contains :
    aaa
    >>
    bbb
    ccc
    >>
    ddd

    schript.csh is :
    #!/bin/tcsh -f

    set fileIndex = `cat $argv[1]`

    foreach index ($fileIndex)
    if ($index == '>>') then
    echo 'Found'
    else
    if ($index == 'aaa') then
    echo 'aaa found'
    endif
    endif
    end

    exit 0

    I run :
    script.csh file1

    I get the following result :
    aaa found

    I was expecting :
    aaa found
    Found
    Found

    I could modify file1 to replace all '>' by another character and test for this new one, but I would like to avoid doing this.

    Could you please help me ?

    Regards.

    Eric.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Eric,

    Enclose the $index in double quotes.

    Code:
    if ("$index" == '>>') then
    Vic

  3. #3
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    This works. Thank you very much.
    But could you explain me why I need the double-quotes to make it work.

    Regards.

    Eric.

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Eric,

    I've never used tcsh so it was a guess. I assumed tcsh behaves like other scripting languages like bash wrt variable expansion. Before a command line is executed variables are expanded. Without the double quotes the command line ended up looking like this:

    if (>> == '>>') then

    According to the tcsh manpage >> is a logical, arithmetic, or comparison operator. So I think the if statement ended up as 'if nothing is greater than or equal to '>>' then execute the next command'. Obviously this fails so the next command didn't execute.

    By enclosing $index in double quotes the command ends up as:

    if (">>" == '>>') then

    so the if statement is 'if '>>' is equal to '>>' then execute the next command' so the next command does execute.

    It's good coding practice to always enclose variables containing strings in double quotes to avoid problems like you did.

    Hope that makes sense.

    Vic

  5. #5
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    Thank you for these explanations.

    Regards.

    Eric.

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Eric,

    One thing I forgot to mention. When you're having trouble with a script it helps to enable verify mode to see how the script commands look when they execute. You do this by adding 'vx' to the 1st line like this:

    #!/bin/tcsh -fvx

    Vic

  7. #7
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    Good to know.

    Thanks.

    Eric.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...