Find the answer to your Linux question:
Results 1 to 3 of 3
hi all, I'm wanting to use csh (yes, csh) to build an array of variables similar to the argv command below - fyi the (at) is an 'at' symbol but ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2012
    Posts
    6

    Defining variables/arrays in csh from stdn/user input


    hi all,

    I'm wanting to use csh (yes, csh) to build an array of variables similar to the argv command below - fyi the (at) is an 'at' symbol but LF wont let me post 'at's in my first post because it thinks it's a URL:

    #! /bin/csh -f
    foreach i ($argv[-1])
    (at) argnum ++
    echo '$argv['$argnum'], argument:' $i
    end

    which gives me variables for $argv[1], $argv[2], etc

    But i want these to come from user input so that it does:

    Enter list:
    texta
    textb
    textc

    or
    Enter list:
    texta textb textc

    gives a set of variables for later use
    ${variable1}=texta
    ${variable2}=textb
    ${variable3}=textc

    How can i replace argv with user input?
    Thanks

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    If you are using tcsh, you probably can use this.
    Code:
           $<      Substitutes a line from the standard input, with no further
                   interpretation thereafter.  It can be used to read from the
                   keyboard in a shell script.  (+) While csh always quotes $<, as
                   if it were equivalent to `$<:q', tcsh does not.  Furthermore,
                   when tcsh is waiting for a line to be typed the user may type
                   an interrupt to interrupt the sequence into which the line is
                   to be substituted, but csh does not allow this.
    Excerpted from man tcsh, q.v.

    In a demo script, I used:
    Code:
    set line = "$<"
    In the real csh, I don't know. I think I might have a copy on an old Solaris install, but I have not tried it there.

    Best wishes ... cheers, drl

    Standard advice: avoid csh family for scripting, use Bourne shell family. For more on this, see: Csh Programming Considered Harmful

    I am posting this only on LF, not on LQ.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Dec 2012
    Posts
    6

    Defining variables/arrays in csh from stdn/user input- lets try bash

    Thanks drl

    OK, looks like csh is a non-starter - Trying to do the same thing but with bash but as im unfamiliar with bash i need a little help.

    Againi, I need to ask user for a list, take the replies and perform an action on each of them.

    Enter list:
    texta
    textb
    textc

    or
    Enter list:
    texta textb textc

    and then i need to know how to run a script on each of them.

    foreach $input
    anotherscript $text[a-c]
    continue

    Thanks,

  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
  •