LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script to seperate male employees from female employees (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-seperate-male-employees-from-female-employees-944293/)

sarahm 05-10-2012 04:01 PM

shell script to seperate male employees from female employees
 
Write shell script to enter the information of company employees until the user enter 0 for employee’s id .The information is
(Id , name , sex (M, F) , total hour worked in a month)The script will list the information of all Males employees and females’ employees in two separate files named Males and Females and the total number of Males and total number of Females with average total hours worked for each with final result message ex. “The productive of males is better” or “The productive of females is better”.

I just started using linux and all i came up with is this
-record (employee, {id, name, sex, total-hours})
and i'm not even sure of it

Kustom42 05-10-2012 04:07 PM

Sounds like a homework assignment here. So in order to point you in the right direction so you can get this done we will need some more info.

Your script will read data, test it, and then output based upon the results of that test. Is the data going to be read from a flat file or will the user be prompted to enter this information into the script and have it be stored as variables?

If its a flat file what is the structure of the file, is it comma delimited, space, etc..

Not sure what this is: -record (employee, {id, name, sex, total-hours})

sarahm 05-10-2012 04:29 PM

the user is suppose to enter the information and it should look like a table with 4 columns (Id , name, sex, total hours)

Kustom42 05-10-2012 04:37 PM

And the user is entering this information on the command line correct? Have you made any progress besides the one line posted above?

Also are you going to be coding this in a bash script or some other language?

sarahm 05-10-2012 05:09 PM

yes, on the command line and i'm not really sure if i should use the bash script, but most likely,yes

Kustom42 05-10-2012 05:24 PM

I'll help you out here do you mind telling me if this is a homework assignment(I will still help you if it is). If this is homework I'll go into more depth and give you so good links to bookmark for future reference.

I use the analogize Linux to being a Chef quite often. Anyone who owns a betty crocker book can become a decent chef, similary in Linux if you know where your resources are and how to read them you can become a decent Linux scripter/user/admin.

sarahm 05-10-2012 05:48 PM

it's a bonus question ...like if i did it, then it'll be great and if not, it'll be fine
but i really wanna know how to do it..

chrism01 05-10-2012 05:53 PM

You'll want some good bash reference material.

Here's a good tutorial
http://rute.2038bug.com/index.html.gz

Ref/tutorials on each cmd by example
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Notes:
1. do put the cmds into a file aka bash shell script file
2. have a good look at the 'read' cmd
3. when you've got something, if you get stuck, post it here and explain what issues you are having.
4. for debugging, add 'set -xv' near the top of your script eg
Code:

#!/bin/bash
set -xv

Good Luck & welcome to LQ :)

sarahm 05-10-2012 06:04 PM

I'm sure the links and notes will help me, thx a lotttt Chris...

Kustom42 05-11-2012 02:00 PM

Those are some great links to have. Chrism01 also gave you a good starting point. I'll give you a small snippet of one of my own codes here that reads a value and stores it as a variable as well as tests the variable for the correct type of input. Hopefully this will give you an idea of what you're trying to accomplish.

Code:

echo "Please enter the number of employees to enter" #Print output to the user asking them a question

read NUMUSERS #The user provides standard input to the script via a keyboard response. This gets stored a variable.

# Now you have a response from a user you can now modify or test that input.
# The below statement will test the users input to verify it is only digits. The != means does not equal

if [[ "$NUMUSERS" != *[[:digit:]]* ]]
then
echo "Please enter only a number for the number of Employees."
exit 1

# An exit of one is a standard exit code for an error with a script.The exit code 0 means it completed successfully.

fi

echo "You choose to create $NUMUSERS users. You will now be asked for their information"

This is where you can continue to echo statements to the user, store their info as variables and then re-print it at the end. You can use if statements like the syntax above if you want to test input. If you really want to learn take a look at writing a while loop for the NUMUSERS variable that will decrease by one each time the user enters in information for a user.

A good link for if statement reference: http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html
And here's the specific chapter for writing interactive scripts that I would suggest you read before getting started. http://tldp.org/LDP/Bash-Beginners-G...l/chap_08.html

Let us know if you have any specific questions or errors while going through this. Welcome to the wonderful world of Bash.

Seham 05-11-2012 02:15 PM

can any one write the full code please?

Kustom42 05-11-2012 03:36 PM

We aren't going to write it for you. Its the old teach a man to fish analogy. Read over the information provided and you should have no problem writing this for yourself. If you get stuck or need info on something specific let us know and we will help you out.


All times are GMT -5. The time now is 12:57 AM.