simple problem - Unix

This is a discussion on simple problem - Unix ; while reading a file line by line i want to get the first string of each line and compare it with the user name, if it matches the username than i want to perform certain tasks like i want to ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: simple problem

  1. simple problem

    while reading a file line by line i want to get the first string of
    each line and compare it with the user name, if it matches the
    username than i want to perform certain tasks like i want to run a
    perl script. I wrote the following commands:


    while read line
    do
    echo $line | { read first rest; echo $first; }
    if [ "$first" = "$usrname" ]; then
    echo "User name is matched"
    exec ./script.pl
    exit 0
    else
    continue
    fi
    done < $FILE


    but this is not working because the scope of $first is inside {} and
    as the curley brackets are closed nothing is in $first
    when i try to assign this value to some other variable like this:

    first_string=$(echo $line | { read first rest; echo $first; } )

    it gives syntax error
    i even tried something like this:

    first_string=$(echo $line | awk '{ print $1 }')

    even this is not working
    anyone of you can help me out please


  2. Re: simple problem

    GUNJAN SHARMA wrote:
    > while reading a file line by line i want to get the first string of
    > each line and compare it with the user name, if it matches the
    > username than i want to perform certain tasks like i want to run a
    > perl script. I wrote the following commands:
    >
    >
    > while read line
    > do
    > echo $line | { read first rest; echo $first; }
    > if [ "$first" = "$usrname" ]; then
    > echo "User name is matched"
    > exec ./script.pl
    > exit 0
    > else
    > continue
    > fi
    > done < $FILE
    >
    >
    > but this is not working because the scope of $first is inside {} and
    > as the curley brackets are closed nothing is in $first
    > when i try to assign this value to some other variable like this:
    >
    > first_string=$(echo $line | { read first rest; echo $first; } )
    >
    > it gives syntax error
    > i even tried something like this:
    >
    > first_string=$(echo $line | awk '{ print $1 }')
    >
    > even this is not working
    > anyone of you can help me out please
    >


    while read first rest
    ....

    also, quote your variables, and no need for continue.

    Ed.

  3. Re: simple problem

    I have written the script like this:

    #!/bin/sh
    #


    # keep all of the arguments
    args=$*


    # This contains the username who invoked the script
    usrname=`whoami`


    # Hardcoding the file to be read
    FILE=./file.txt


    # Checking whether the file exits and is readable
    if [ ! -f $FILE ]; then
    echo "The given file $FILE does not exists"
    exit 1
    elif [ ! -r $FILE ]; then
    echo "The given file $FILE is not readable"
    exit 1
    fi


    # read $FILE
    while read USERNAME REST
    do
    if [ "$USERNAME" = "#" ]; then
    continue
    elif [ "$USERNAME" = " " ]; then
    continue
    else [ "$USERNAME" = "$usrname" ]
    echo "User name is matched"
    script.pl $args
    exit 0
    fi
    done < $FILE


    # exit normally if the file does not contain the username
    echo "Undefined User: $usrname\n"
    exit 0


    but when i use "exit 0" for the first time it should come out of the
    loop and the script but still its going to the next statement
    and the last statement is printed
    "Undefined User: "
    which should not be printed if the username is matched


    Can you please help me???

    Regards,
    Gunjan



+ Reply to Thread