LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-27-2011, 03:54 AM   #1
Dudemeister
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
Bash: need help correcting very short codes


Hi everyone!

I'm very new to linux so I don't know much about bash programming.Right now I'm working on 4 tasks I got from my teacher,and I've done 3 but I'm not sure if they're completely correct.Also I'm not 100% sure about how to solve the 4th one,but I'm halfway into it.So if anyone could please help me out and see if I've made any mistakes in them,and if so then correct and tell me why you made that specific change to the code(what the change does and what it would've done otherwise).


First task was:
"Write a script which asks for a catalog and in which it goes through all files and changes your own rights to rwx."

My attempt:
Code:
#!/bin/bash

echo "Type in the name of the catalog:"
read catalogname
cd $catalogname

for filn in *

do
	filname=`basename $filn`
	chmod u+rwx $filname
done

Second task:
"Make a script which takes the name of a file as a parameter. This file should be removed if its size is 0, otherwise it should show name, size, owner and modification date of the file."

My attempt:
Code:
#!/bin/bash

fname="$1"
owner="$4"
size="$6"
moddate="$3" #unsure about this,should it be $3?

if [ $6 -eq 0 ]
then
	rm { $1 }
else
	echo "$fname $owner $size $moddate" 
fi

Third task:
"Type a script which shows a menu according to the one below and which executes a plausible activity for each alternative you type in.

1 Show all the files in the current catalog.
2 Decide whether the file is an ordinary file or a catalog.
3 Create a backup of a file.
4 Exit
"

My attempt:
Code:
#!/bin/bash

echo "1    Show all the files in the current catalog."
echo "2    Decide whether the file is an ordinary file or a catalog."
echo "3    Create a backup of a file."
echo "4    Exit"

read var
while [ $var != "x" ]
do
case "$var" in
    1) ls
	;;
    2)  echo "Filename: "
	read name
	file $name
	;;
    3) 	echo "Filename: "
	read name2
	scp $name2
	;;
    4)
	;;
esac
read var
done

And now this fourth one is the one I'm having bigger trouble with.

Fourth task:
"Create a script which will decide which argument you put in is the biggest. You should be able to put in up to 10 parameters.

Example:
>./max 4 31 7 9 42
The biggest one is 42"

My attempt:
Code:
#!/bin/bash

#I'm not sure how to ask for up to 10 parameters,
#should I write echo and then ask like: 
#echo "write up to 10 parameters and finish with enter"
#and after that what do I write to make it 10?
#I'm thinking a while loop would be useful,but I'm not
#sure,plz help.

let max=0
for arg in $*
do
	if [ $arg>max ]
		then
			max=$arg
	fi

done

echo "$max is the biggest"



Any help is appreciated.



Thanks in advance!! =)
 
Old 09-27-2011, 04:07 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
we aren't here to do your work for you. if YOU think they comply with the requirements, then that's what you should submit.

for #4 though, white space is often important in bash, as a bash script is basically a load of separate programs arranged to look like a conventional language. if you leave gaps out, things that would not be relevant in other scripting / programming languages get interpreted very differently. You also might want to look at "shift", you might not.
 
Old 09-27-2011, 04:19 AM   #3
Dudemeister
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Had I wanted you to do it all for me,I would've told you so instead of asking for advice and help concerning correction.

I'll give you some straight-forward questions if you still don't understand:

1.Does $3 show modification date?

2.Should I use a while loop in #4?

3. does "if [ $6 -eq 0 ]" check if the size of the file is equal to zero?
 
Old 09-27-2011, 11:24 AM   #4
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
Well I am not sure what you have learnt so far, but, if I was your instructor I would fail all except the last for giving me what I have asked for
and fail all based on the lack of parsing you do.

Now not to leave you on a sour note, things you might consider:

1. Some of the questions ask that there be a specific number of arguments (or up to), yet you never test how many are passed in.

2. $1 to $N refer to the arguments passed to a script (in this case) and yet you use them as though they exist all the time, eg. question 2 talks about taking the name of a
file as an argument (so we would assume $1 has a value) and yet you are using parameter values greater than this, ie. $4 and $6, where did these come from?

3. I am not familiar with the terminology but assume that a 'catalog' is a directory. Be sure to read the questions closely as some refer to information about files specifically
but you return an almost global view which may include any type, including 'catalogs'

4. Not sure if you have run any of these but be careful when testing a variable that it has actually been set and / or that the item you test it against will actually be entered at some
point.

5. You seem to assume that your instructor (assuming he will test scripts) will not make any errors, either on purpose or by accident. You cannot account for all but you generally
are accounting for none at the moment.

6. How do you know a file is 'yours'? Again no testing.

There is probably more but hopefully it gives you something to think about.
 
1 members found this post helpful.
  


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
I need help correcting a bash script tzily Programming 3 08-20-2011 08:35 AM
[SOLVED] using the special RE codes \1,...,\9 in bash sed ghantauke Linux - Newbie 8 11-30-2010 10:45 AM
capturing error codes in bash script nikaudio Linux - General 2 04-09-2008 08:56 AM
Bash scripting problem with exit codes Jeiku Programming 2 05-15-2006 01:22 AM
Bash script for correcting HTML tags hq4ever Programming 4 11-08-2004 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:31 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