Find the answer to your Linux question:
Results 1 to 3 of 3
hey all, I have this code : Code: #!/bin/tcsh -f set line=($<) while($#line > 0) echo $line[1] set line=($<) end the usage to run the script would be : Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    7

    cshell reading file line by line


    hey all,

    I have this code :

    Code:
    #!/bin/tcsh -f
    
    set line=($<)
    while($#line > 0)
    echo $line[1]
    set line=($<)
    end
    the usage to run the script would be :

    Code:
    ./file < file2
    Is there a way to specify filename in the script itself instead of getting/reading file name from STDIN ?


    Using :

    Code:
    set file=data
    instead of :

    Code:
    set line=($<)

    thanks.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You can assign it to a variable with the set command. you can also pass it to the script as the first argument, using the $1 built-in variable, e.g.:
    Code:
    #!/bin/tcsh -f
    
    # take as fist arg to script
    #set file=$1
    
    # hard-code the file here
    set file='foo.txt'
    
    foreach line ("`cat $file`")
      echo "$line"
    end
    so if the file is set within the script, you could call it like:

    Code:
    ./script.csh
    and if you want to pass the script as a variable, you'd call it like:

    Code:
    ./script.csh foo.txt
    of course, you don't even need to use set at all, and just explicitly pass the file name to cat, e.g.:
    Code:
    foreach line (`cat foo.txt`)
    Edit: added necessary double-quotes to code
    Last edited by atreyu; 09-01-2012 at 03:37 AM. Reason: added double-quotes

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    7
    atreyu thank you ,
    so you changed the loop from while to for ..

  4. $spacer_open
    $spacer_close

Posting Permissions

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