Skip to content

Loop Over Each Line in a File using Bash

Use exec as below, for example:

exec < $PASSWORD_FILE
while read LINE; do
  user=`echo $LINE | cut -d: -f1`
  uid=`echo $LINE | cut -d: -f3`
  pgroup=`echo $LINE | cut -d: -f4`
  homedir=`echo $LINE | cut -d: -f6`
  shell=`echo $LINE | cut -d: -f7`
  gecos=`echo $LINE | cut -d: -f5`
  cmd="/usr/sbin/useradd -u $uid -g $pgroup -d $homedir -s $shell -c $gecos $user"
done

You may need to store stdin within another file descriptor if you need your script to receive input from stdin after the loop runs.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*