Register
It is currently Fri Sep 19, 2014 9:50 am

Need 3 Scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Thu Mar 27, 2008 8:09 am   

Joined: Thu Mar 27, 2008 8:06 am
Posts: 3
I no longer need the scripts. Thx


Last edited by biggypaul on Thu Mar 27, 2008 6:05 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Mar 27, 2008 9:39 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi,

erm...where are your attempts so we can help you fix the issues?
It's just that it looks allot like homework which you should at least attempt first :)


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 11:11 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
we don't do homework...


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 11:14 am   

Joined: Thu Mar 27, 2008 8:06 am
Posts: 3
These are actually practice problems and I need some guidance. I'm not very good at bash scripting.
Ive been working on the first script. This is what i have so far:

#!/bin/bash
for i in $(seq 25 $1); do
echo "I'm jealous of my professor who is on the beach in California" >> output.txt
done


Last edited by biggypaul on Thu Mar 27, 2008 11:23 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Mar 27, 2008 11:22 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
ok... pay close attention to what you are stating is nec. for the script.
"needs 3 arguments"

in that script you have only provided the ability to take 1 argument... with your use of $1
i'll help you a little here...
$1 is a positional parameter.... there will always be atleast 1 positional parameter... and that is $0
which is the name of the script itself.... the positional parameters are lined up and divided by spaces...
so (for example) you could have a script like:
Code:
#!/bin/bash

echo "parameter 0: $0"
echo "parameter 1: $1"
echo "parameter 2: $2"
echo "parameter 3: $3"

now make the script executable and run it with something like:
<script name> hi "i'm using positional parameters" 3

pay close attention to what i have above. and only substitute the <script name> portion with your actual script to run it


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 11:40 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i just realized i never brought over this tutorial (until now)
it was staying over at usalug... well... now it's here too!
it's a nice tutorial on positional parameters (ok... so i'm biased)
check out some of the other tutorials on this site... i wrote a few that are a lot easier to follow (i think) than a lot of the others out there. i try to just cover what is needed.


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 11:58 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i'll show you how i would do the first one... but i won't touch any of the others (just because of the strict "no homework" policy)...

Code:
#!/bin/bash

# $1 is the first argument we pass... we call it str
str="$1"

# $2 is the number of times we loop... or iterations... we call it iter
iter="$2"

# $3 is the file we output to... we call it outfile
outfile="$3"

# here we set up our loop from 1 to whatever number you passed it
for i in `seq 1 $iter`
do
    # here we echo the string we passed it to repeat
    # we echo it to a tmp file... just because of the rules stated (if no 3rd argument... we print to screen)
    echo "$str" >> tmp
done

# here we check if there was a 3rd argument
if [[ "$outfile" == "" ]]
then
    # if not... we cat the tmp file to the screen (same as printing it to screen)
    cat tmp
else
    # if there IS a 3rd argument... we change the tmp file to the name of the outfile
    mv tmp "$outfile"
fi


hope that helps your knowledge of scripts...
we are really not here to do homework for people... but we ARE here to educate...


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 12:06 pm   

Joined: Thu Mar 27, 2008 8:06 am
Posts: 3
I understand your hw policy and I am glad you have such a rule. These were only for practice and from what I can see I need alot. Thanks for your help though. I really appreciate it.


Top
 Profile  
 PostPosted: Thu Mar 27, 2008 12:19 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
you're welcome... don't hesitate to ask questions...
we can answer questions on a general scale... like "how would i do something 3 times?"
we could go over a for loop... etc...
the above script can cause a few different types of errors... there is NO error checking involved in it at all... but i just wanted to cover some of the basics involved in that script.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP