LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2011, 08:14 AM   #1
Brenten
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Creating scripts


Hi,

I'm trying to create a little script as part of a homework assignment. It's supposed to check the a passwd.p4 file for uids and gids of zero. There will be an output file which logs when it was run and what the results were, its supposed to email you an alert if a concern is found.

It's supposed to run using crontab, so I know it'll be:

0 6,18 * * * * ./script4

It's the script I can't figure out though. It'll need a cut so I was looking at this for the script:

#!/bin/bash
cat ~c20600/passwd.p4 | cut -f1,f3 >> output4a
cat output4 | grep "0" >> output4b
(and then I go blank)


Here is a sample of the passwd.p4 file:

adm:x:4:4:Admin:/var/adm:/dev/null
lp:x:71:8:Line Printer Admin:/usr/spool/lp:/dev/null
uucp:x:5:5:uucp Admin:/usr/lib/uucp:/dev/null
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/dev/null
smmsp:x:25:25:SendMail Message Submission Program:/:/dev/null
listen:x:37:4:Network Admin:/usr/net/nls:/dev/null
nobody:x:60001:60001:Nobody:/:/dev/null
noaccess:x:60002:60002:No Access User:/:/dev/null
nobody4:x:65534:65534:SunOS 4.x Nobody:/:/dev/null
frisco:x:101:10:francisco:/home/admin/frisco:/bin/bash
mysql:x:3610:122::/home/admin/mysql:/noshell
rex:x:103:10:Rex Roof:/home/admin/rex:/bin/tcsh


Any help would be greatly appreciated!
 
Old 10-03-2011, 09:00 AM   #2
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
Some suggestions:

- cut uses TAB as default delimiter, you may want to specify the delimiter with -d, check the man.
- One powerful alternative to cut is awk, check some examples with the awk print.
- You can use bash command substitution to avoid unnecessary hard disk I/O.
 
Old 10-03-2011, 09:06 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!

Since it is a homework assignment, you will understand we cannot give the exact solution. Anyway, here is some advice:

1. Check carefully the man page of cut: how do you specify the LIST as argument of the -f option? How do you tell cut to separate fields? Hint: look at option -d.

2. You don't really need to use cat and pipe the results to the cut or the grep commands, since they accept the file name as argument.

3. To complete your script you need to write down the current time. Hint: use the date command. Finally you have to check if uid and gid 0 are found and send an e-mail alert.

In general you should run each command separately on the command line without redirection to a file, to check the output in real time. This will spare you a lot of time for testing. For example: your first command produce an error:
Code:
$ cat ~c20600/passwd.p4 | cut -f1,f3
cut: invalid byte, character or field list
Try `cut --help' for more information.
Try again and feel free to ask for any further clarification.
 
Old 10-03-2011, 09:33 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Since it is a homework assignment, please... ask your teacher.

There's a not-so obvious reason for this: not-so obvious, that is, unless you have been a teacher!

Speaking now as a (part-time) teacher: "We can't see what's going on inside your head." If you're having a problem, undoubtedly many other people in the class are having the same problem. Perhaps something was not quite clearly explained, but if so, "how's the teacher supposed to know that?"

Your success in the course depends not only on your diligent application of your own resources (which generally means not asking for help on web forums like this one), and the direct involvement as-necessary of your teacher. Effective instruction in computer related topics is designed to achieve, not only that you're able to complete the assignment this time, but that you've learned how to troubleshoot similar situations for years to come. If you "take the easy way out," yeah, you might pass the course now, but you're going to be in a world of hurt in later years.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Runlevel scripts and creating links Nickjpost Linux - Newbie 5 12-16-2010 10:24 PM
Linux Scripting problems creating 3 scripts Mc_henry Linux - Newbie 1 12-02-2010 11:18 AM
12.1 install not creating inittab and RC scripts. mlsrar Slackware 1 08-01-2008 12:10 PM
Need tips on creating scripts for daily backup (tar) a directory to an external drive Joejr4u Linux - Server 2 06-07-2007 07:46 AM
creating a new path for scripts Freestone Linux - Newbie 1 10-05-2006 07:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration