Results 1 to 7 of 7

Thread: Variable List

  1. #1
    Join Date
    Oct 2003
    Posts
    7

    Unanswered: Variable List

    Anyone know how I can have a user enter a string and have that string removed from a variable list?

    ie

    list="One Two Three"
    echo "Please enter string?"
    read $string

    ...then get that string and remove it from the list someone..I was thinking using cut but I'm not sure. Please advise. Thanks.

  2. #2
    Join Date
    Oct 2003
    Posts
    7

    Bump

    Any suggestions?

  3. #3
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hello Mike,

    first : "read $string" is wrong better you take "read string"

    second : the result from read command is not insert in "list". It is a seperate variable.

    You can delete it by "$string=""". Where is the problem ?

    Greetings from Germany
    Peter F.

  4. #4
    Join Date
    Oct 2003
    Posts
    7

    string

    Hi,

    I want to have the user enter a string and have that string matched up against another variable in the script and 'removed'. Ie.

    If the user enters 'Run' then it will match it against another variable in the script called Transporation="Walk Run Jog"

    The new value of Transportation would be "Walk Jog"

    Please advise. THanks in advance. Would 'awk' do the trick? not sure since I'm new at this.

  5. #5
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    list="One Two Three"
    echo "Please enter string?"
    read string
    new_string=""
    for next in `echo $list | awk '{ for(i = 1; i <= NF; ++i) print $i}'`
    do
    if [ ! "$next" = "$string" ]; then
    new_string=`echo $new_string $next`
    fi
    done
    echo $new_string
    Thanks and Regards
    Karthik R

  6. #6
    Join Date
    Oct 2003
    Posts
    7

    Character

    Hi..

    I can't seem to get that character at the end of line 5? I tried double quotes instead but not working. I'm using korn shell. Any suggestions?

  7. #7
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: Character

    I can't seem to get that character at the end of line 5? I tried double quotes instead but not working. I'm using korn shell. Any suggestions?
    That character is a backtick (`). On a UK keyboard at least, it is underneath the escape key, above tab. Commands between 2 backticks are substituted for their equivalent output and substituted into the current command. You could also use $(command).

    [q]for next in `echo $list | awk '{ for(i = 1; i <= NF; ++i) print $i}'`[\q]

    The line above is saying to echo the value of list, pipe this value into awk and then loop through each field passed into awk, print it out an dthen substitute the final value into the current command, i.e.

    for next in outputFromEchoAwkCommand
    do
    ...


    A bit pointless really because this is effectively the same as...

    for next in $list
    do
    ...


    A simpler example would be...

    list="a b c d e f g h"
    echo "Enter value: \c"; read value
    for item in $list
    do
    [ $value != $item ] && newList="$newList $item"
    done
    list=$newList; echo $list

    Or even...

    list="a b c d e f g h"
    echo "Enter value: \c"; read value
    list=$(echo "$list" | sed "s/${value} \{1,\}//g"); echo $list

    HTH

Posting Permissions

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