LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2012, 03:57 PM   #1
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Rep: Reputation: 1
basic scripting ?'s


i got a HW assignement for unix/linux class and this teacher has not really gone over scripting yet(he gave us one quick example) then gave us the following assignement. i'm not trying to just get my homework done i am actually trying to understand how to do this since this class has no text to follow.
----------------------------------------------------------------------------------
assignement:
Here is the assignment for Monday.
Please do all work on the system. I have created "hwork" directory in your home for this purpose.
I suggest you get started immediately:

Write a bash shell script called "hw1" that takes 1 or 2 parameters:
s4 <string> [ <user> ]

The script looks for the given string (ignore case) in the user's xxx/cars file. If no user is given, all users are searched.
Example usage:
s4 chevy bgordy
s4 mercedes


Bonus (15 points): Use a loop to search each user
Hint 1: Use a for loop which will define a variable over all directories in /home
for DIR in /home/*
do
. . .

Hint 2: You can check for the file existence using the -f flag in an if statement test:
if [[ -f $DIR/xxx/cars ]]
then
. . .

Note: partial credit will be given for trying the bonus portion!
One more thing, to run your script you may have to use ./hw1
----------------------------------------------------------------------------------
i read this as asking to write a script prompting for a string to search and an optional user to search otherwise search all users. and print the results to stdout.(optionally i'd like to 2> /dev/null for errors i.e directories that i have no permissions to search).
this are the two wrong solutions i came up with. i know both are wrong and somewhat why in some cases. i'll note that after each example .
note: all users already have a /xxx/cars directory in their home dir
----------------------------------------------------------------------------------#try1:

echo "please enter string to search followed optionally by a user: "$1 $2
if (( $2 == 0 ))
then
grep $1 /home/*/xxx/cars
else
find /$2 -exec grep -i make {}\; 2> /dev/null
fi

#notes: ok i no i can't use the variable as i did i.e /$2 or /home/$2/xxx/cars #but how do i do that. other than that the script i know still won't work.
----------------------------------------------------------------------------------
#try2

echo "please enter string to search followed optionally by a user: "
read $1 $2
if (( $2 == 0 ))
then
grep $1 /home/*/xxx/cars
else
find /$2 | grep $1 /home/$2/xxx/cars \; 2 > /dev/null
fi

#all that's diff here is a read to prompt for the input so it doesn't just execute #with no parameters and a simple pipe to grep from find instead of the -exec line #i donn't really understand anyway. i don't really understand the line starting #with find much but it was the in class example for executing the grep command on #the find results. also not sure if my redirect of error is right here either.
----------------------------------------------------------------------------------
sorry if i'm an idiot but i got most my info on my own and still am stumped he really didn't give us any tools to do this he spent most class time on simple commands like cd then the rest on vi commands. the problem is there is no prerequisite for the class but most ppl take it at nthe end of 4th semester and it's my first.I don't have a programming class under my belt and it seems like he assumes some knowledge here about loops and variable assignement. not to mention he gave basically no syntax info on how to do this in bash (loops, store and call on variables etc..).
any help appreciated. i'm not just looking for a working script with no explanation. i'd like to know what i'm doing wrong or why a sample script you provide is correct.
THANX in advanced
 
Old 09-21-2012, 04:31 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,095

Rep: Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241Reputation: 2241
Begin with finding your way in the Advanced Bash Scripting Guide you will find here.

Despite its title, it's easy to understand by newbies.
 
Old 09-21-2012, 08:20 PM   #3
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Original Poster
Rep: Reputation: 1
got most of it and thanx for the link

The attachments are screen-shots of my answer's. The first one hw1 me and a classmate combined ideas on. The second I did the same thing but with a for loop since the assignment asked for a for loop. In my opinion hw1 is a better solution though.
That said I still would like to know how to prompt for input in the script and use it interactively as opposed to including the parameters at the execution of the script.
==================================================================================================== ============================================
See hw2 attachment and if any one can tell me why it just sits there after entering the two inputs your prompted for. It never executes the next line with those defined variables from the input. it looks right to me logically but something is wrong.
Attached Thumbnails
Click image for larger version

Name:	xtra_credit.png
Views:	12
Size:	21.0 KB
ID:	10736   Click image for larger version

Name:	hw1.png
Views:	10
Size:	20.1 KB
ID:	10737   Click image for larger version

Name:	hw2.png
Views:	6
Size:	22.4 KB
ID:	10738  
 
Old 09-21-2012, 10:29 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Quote:
Originally Posted by minty33 View Post
I still would like to know how to prompt for input in the script
Type 'help read' in your shell.


Quote:
Originally Posted by minty33 View Post
See hw2 attachment and
Nope. You can copy code right here in [code]vBB code tags[/code]. Easier to read and efficient to correct.
 
Old 09-23-2012, 09:26 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
unSpawn already mentioned [code][/code] tags. But could you please also edit your posts to remove the unbroken strings of equals signs? They force side-scrolling and make the thread very difficult to read. Thanks.


Rather than the ABSG, linked to by Didier Spaier above, I recommend the BashGuide as a more readable introduction to the shell. I wish I'd had it when I was first starting out.

http://mywiki.wooledge.org/BashGuide

And here are some other useful references:
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://wiki.bash-hackers.org/scripting/newbie_traps
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

Last edited by David the H.; 09-23-2012 at 09:27 AM.
 
Old 09-24-2012, 06:50 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
All of the above and also http://rute.2038bug.com/index.html.gz
 
Old 09-28-2012, 08:29 PM   #7
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Original Poster
Rep: Reputation: 1
sorry about the equals sign and the attachments

the edit option is not there for these posts anymore. don't know why, but i am going to mark this solved since i figured out a couple different ways to do this. thanx for the links they are bookmarked now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Basic Scripting help Dru-Jitsu Linux - Newbie 7 07-14-2010 09:02 AM
Basic Scripting karlochacon Linux - Newbie 6 11-12-2009 07:22 PM
Basic shell scripting help zipper777 Linux - Newbie 6 03-13-2006 01:58 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 10:31 AM
very basic scripting question spatter9999 Programming 3 09-18-2003 02:36 AM


All times are GMT -5. The time now is 07:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration