Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    cshell reading file line by line

    hey all,

    I have this code :

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

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

    Using :

    set file=data
    instead of :

    set line=($<)


  2. #2
    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.:
    #!/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"
    so if the file is set within the script, you could call it like:

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

    ./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.:
    foreach line (`cat foo.txt`)
    Edit: added necessary double-quotes to code
    Last edited by atreyu; 09-01-2012 at 02:37 AM. Reason: added double-quotes

  3. #3
    atreyu thank you ,
    so you changed the loop from while to for ..

  4. $spacer_open

Posting Permissions

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