Find the answer to your Linux question:
Results 1 to 6 of 6
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2013
    Posts
    3

    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
    Code:
    GETUSERS=`cat /etc/passwd`;
    COUNT=0;
    
    for record in $GETUSERS; do
            echo $record;        
    done
    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
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    if you want to iterate over each line in the file, then try this alternative method:
    Code:
    #!/bin/bash
    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
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    220
    hi,

    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:
    Code:
    while IFS=":" read notUseful useful notUseful2 otherUseful rest
    do
       echo "$useful otherUseful"
    done < file
    where file could be
    Code:
    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:
    Code:
    while IFS=":" read -a array
    do
       echo "${array[1]} ${array[3]}"
    done < file
    Last edited by watael; 07-28-2013 at 02:44 PM.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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...

  5. #5
    Just Joined!
    Join Date
    Jun 2013
    Posts
    3
    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
    Code:
    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.

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    220
    /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
    ``<space><tab><newline>''.
    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
  •