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

    Simple User Listing Script

    Hello everyone, I'm just learning bash scripting and I've come across loops so I'm trying to do something useful with loops but I've encountered strange problem, what I'm trying to do is to cat the /etc/passwd file and store it in a variable then loop through this variable and get the user id and username so I can make checks on it like getting users with id =? 500
    GETUSERS=`cat /etc/passwd`;
    for record in $GETUSERS; do
            echo $record;        
    But when happens when I try to echo the $GETUSERS variable it doesn't show the output of the as is but it creates a new lines which make impossible to work on it!!!!
    So any reason why that happens???

  2. #2

    if you want to iterate over each line in the file, then try this alternative method:
    while read line; do
     echo $line
      #userid=$(echo $line|awk -F: '{print $1}')
      #echo User ID: $userid
    done < <(cat /etc/passwd)
    the variable $line will contain a single line of /etc/passwd and you can work on it as in the commented out examples.

  3. #3

    it's not a good idea/practice to read a file or the output of a command substitution with a for loop, because it splits lines into words, aznd then loops on each word.

    the correct way is to use a while loop:
    while IFS=":" read notUseful useful notUseful2 otherUseful rest
       echo "$useful otherUseful"
    done < file
    where file could be
    1:blah:bla:blah blah:etc:etc
    2:blah:bla:blah blah:etc:etc
    cat is rarely useful in script!

    EDIT: an array could be used too:
    while IFS=":" read -a array
       echo "${array[1]} ${array[3]}"
    done < file
    Last edited by watael; 07-28-2013 at 02:44 PM.

  4. $spacer_open
  5. #4
    Quote Originally Posted by watael View Post
    cat is rarely useful in script!
    he's right, you know!

    Useless Use of Cat Award

    cat; one of the most abused of commands that for the life of me, i can't train my brain not to use...

  6. #5
    Well Thank you all for your replies, and actually that's interesting. but the thing is i'm just still learning and by the way I have no idea what does that line do or mean
    while IFS=":" read notUseful useful notUseful2 otherUseful rest
    If you could explain more what does IFS do, that would be great!
    And if you could explain more on "/etc/passwd" file that would be great too.

  7. #6
    /etc/passwd is described in its man page => man passwd

    man bash is useful too to know about IFS:
    Quote Originally Posted by man bash
    IFS The Internal Field Separator that is used for word splitting after expansion and to
    split lines into words with the read builtin command. The default value is
    Here we modify its value for the environment of `read` only.

Posting Permissions

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