Hi, I am new to bash scripting. I have some questions here, Can anybody help?
I have a text file which has the following format.

UserNam
User full name
road and house nr.
post nr, city
phone, mobil
Department, job


The first thing should be to open file and find :ADD_USERS,
Then read then next line until the user name appears. Here the first user name is JOLA.
1 .Add "JOLA" user to the system with "adduser" command.
2. Then add the rest of information to another text file named "UserData".

Read the next user and continue step 1 and 2, until ":DELETE_USER" appear.

Read the name of user and delete it from system. there will be many users, loop trough then til EOF.

Here is the text file.
___________________________________________
:ADD_USERS

JOLA
John Larsen
Downtown 4, 2.tv
4534, Longby
45434444, 89765445
Electronics, Electrisian

LALA
Lars Larsen
Downtown 7
5345, Loncity
-, 23423423
Electonics, Electrisian

:DELETE_USERS
labe
gama
_____________________________________________________


adv. thanks for help
Kursist.

have you written any script to make a start on this problem? The adminstrators dont like it being used as a home work forum!!

Yes, I have written some codes, but I can't copy it. Becuase I use VMWare and can't copy it to windows. Here I am rewritting a of it.

cat users | while read line
do
if [ "$line" == "ADD_USER" ]
then
echo "Adding Users"
elif [ "$line" == "DELETE_USER" ]
echo "Deleting Users"
fi

# and so on....

Ok. first what o/s are you writing this for. Second what bit you stuck on then is it the extracting the user data to another file or the adding user command. Also probably a case statement would be better than an if statement. Let me know and i will see if i can help!

RedHat 9 installed in vmware, using vi editor.

1. the problem is that how to check that I am arrived at ADD_USERS or DELETE_USERS.

2. How to skip ADD_USER line and read the user info.

3. Reading user info, only username conti should be created. Other data should be exported or copied to another text file.
I know that user are added using "adduser" and other info are added using

echo $line > UserInfo.txt

3. Arriving to DELETE_USER, delete only those user which are next to DELETE_USER. Again here I have problem with skiping DELETE_USER line.

I am very thankful to your quick reply.

cool ill have a think and get back to you

hi, i have done some but its not totally finished have tried to comment it as well as possible. You now need to put the useradd into a loop and maybe tidy up the code a little. Other than that it all works might not be the best way of doing it but its a way nether the less!!

#!/usr/bin/ksh
line_number=0;
EOF=`wc users | awk '{print $1 }'`

#first lets strip empty lines in the file
#and put it into a tempory file

grep -v "^$" users > users.tmp

#now lets read the file in a while loop

cat users | while read line
do
  (( line_number = line_number + 1 ))
  if (( line_number == $EOF))
  then
    echo "read of file finished on line $EOF"
    #clean up
    rm users.tmp >/dev/null 2>&1
    exit
  else
    case $line in
        :ADD_USERS)

        #check for the line :ADD_USERS and print where you have found it
        echo "adding users $line_number"

        ((user_line = $line_number + 1 ))
        #use sed to copy the line after :ADD_USER in a file called user.add
        cat users.tmp | sed -n $user_line'p' > user.add
        ((user_line = user_line + 1 ))
        ((user_info = user_line + 4 ))
        #do a little bit more maths so that we can copy the next four
        #lines into user.info
        cat users.tmp | sed -n $user_line,$user_info'p' > user.info
        ;;

        :DELETE_USERS)
        #check for the line :DELET_USERS and print where you have found it
        echo "deleting user on line no $line_number"

        #use sed to copy the remaining lines of the file into
        #user.del
        ((user_del = $line_number + 1 ))
        cat users.tmp | sed -n $user_del,$EOF'p' > user.del
        ;;


    esac
  fi
done

Thank you so much for all your work. Now I have an idea how to go further with the task.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.