LinuxQuestions.org
Help answer threads with 0 replies.
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-01-2010, 06:58 PM   #1
linuxbeginner1
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Rep: Reputation: 0
Shell Scripting 3 scripts


1. Write a script that enumerates the system’s users and groups from /etc/passwd and /etc/group (or their network database equivalents).For each user, print the user’s UID and the groups of which the user is a member.


2. Write a script that prints out the number of lines of code with the work “magic” on them in the Linux kernel source tree for the kernel running on your system, regardless of whether the word is upper case, lower case, or some combination thereof. Only count instances in files ending with a '.h' extension. Hint: this will require installing the kernel-devel package first, and finding out where it put it's files.

3.Write a script that takes three arguments: a source directory, a source file extension, and a destination file extension, in that order. All three arguments are mandatory; if an incorrect number of arguments are given, an error message should be printed (1 point). If the source directory is not actually a directory, an error message should be printed (1 point). The script should transform the names of all files with the source file extension within the directory so that they have the destination file extension.(2 points) So, running the script as follows:

./script3.sh mysourcedir cc cpp

would transform the name of every file in 'mysourcedir' that ended in .cc to end in .cpp.

Before:

main.cc

utils.cc


After

main.cpp

utils.cpp


The script should operate on the directory recursively; file in subdirectories should have the same operation performed on them.

I am very stuck on these please help
 
Old 10-01-2010, 07:02 PM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
This sounds like a homework assignment to me. If it is, please tell us
what you've done so far on each of these scripts.
 
Old 10-01-2010, 07:08 PM   #3
linuxbeginner1
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
its more of classwork but the teacher is not telling us anything

I am stuck on the first one with this so far...

i dont really know where to start, my intuition is

save the first and fourth coloumn of group then check it against the first column of passwd

and print it out
 
Old 10-01-2010, 07:12 PM   #4
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Okay, would you find every user in /etc/group?
 
Old 10-02-2010, 01:11 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
save the first and fourth coloumn of group then check it against the first column of passwd and print it out
So why not tell us how you would do this and where you are getting stuck?

Remember we are here to help not actually do the work for you.

Quote:
the teacher is not telling us anything
This is probably because you have been already taught all you need or it is in your notes, hence they do need to tell you anymore.
 
Old 10-02-2010, 01:16 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by grail View Post
hence they do need to tell you anymore.
don't need to tell you anymore
 
Old 10-02-2010, 04:16 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
thanks for the assist
 
Old 10-02-2010, 07:24 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
command pipeline to count lines containing "magic" in kernel source .h files

Here are some tips for problem #2. NOTE: There are loads of permutations of how to do this problem; the way I'm thinking of, is a little bit small for actually requiring it be put into a script, but doing so would be simpler than the code itself; you'd just paste the commands into a file, add a shebang at the top, and save it. Instant script. And, I'm not sure if there are any particular requirements or rules as to how you are supposed go about this task, so my method may or may not be the most appropriate way for you to do this (maybe it isn't what your teacher wants, I don't know..).

In an effort to make this educational (i.e. make you read the documentation) I'm not going to just slap down a working example. Hopefully you can come up with a close idea, and we can work on it to fix it if it isn't working.

Anyhow, what I used is the `find` command, to locate all the *.h files in the search path. Look at the `find` manpage, and examine these options: -type, -name, and -exec.
Use the -type option to tell `find` what you're looking for, i.e. files, or directories, etc.
Use the -name option to specify what pattern of filename you are looking for, i.e "*.h" for .h files.
Use the -exec option to execute a command each time a "*.h" file is found.

The command I executed with `find`'s -exec option, is called `grep`. grep is used to search inside files for a certain keyword, in this case the keyword is "magic". To make grep ignore case (and match upper or lowercase), use the -i option.

Finally, I use a pipeline, represented by the (|) pipe symbol to pass each result from find+grep, into the `wc` command. This will send every line containing "magic" through the pipeline, into `wc` which is a word or line counter. It counts how many words/lines are sent to it. Using the -l (lowercase 'L') option with `wc` makes it count whole lines.

NOTE: Each *.h file that `find` finds is represented by this double-brace character(s) when -exec is used: {}
So in our problem here, our `grep` command will be using {} to represent each filename that it searches inside looking for the word "magic".

Here's a template of what my command "looks like". You must put the appropriate options or keywords in place of what I show in [brackets] to make the command work; anything NOT inside [brackets] or <brackets> is exactly as it should be written:
Code:
find [/path/to/kernel/sources] [-type <type> -name <"name">] -exec [grep <option> <keyword>] {} \; [pipe] wc <option>
It would be much easier to just write the command for you, but then you don't learn anything that way, and have no cause to look at the documentation (man pages).

HINT: I got a result of 2145 lines containing the word "magic" in the sources of my 2.6.35.7 kernel. Whatever kernel version you are using, it should be somewhere in this range too, give or take 100 or so. (Please someone else can do their own test and see if I'm close to their result too!)

Good luck! Show us what you end up with and we'll help you fine-tune it. Or, if you go about it a totally different way, that's great! But please still show us what you come up with if you need further assistance with it.
 
1 members found this post helpful.
Old 10-04-2010, 01:32 AM   #9
linuxbeginner1
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
find /usr/src/kernels -type f -name "*.h" -exec grep -i magic {} \; | wc -l

I got 618
 
Old 10-04-2010, 02:17 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
2.6.34 kernel - 2087

I would be guessing that 618 is not enough. Are you sure you have devel version installed as per the question?
 
Old 10-05-2010, 10:52 PM   #11
linuxbeginner1
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
mine is the kernel-devel package 2.6.18
 
  


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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Bash Shell Scripting *Integrate two scripts* Soulful93 Programming 1 04-29-2004 05:07 PM

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

All times are GMT -5. The time now is 06:34 AM.

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