LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash: need help correcting very short codes (https://www.linuxquestions.org/questions/linux-newbie-8/bash-need-help-correcting-very-short-codes-905197/)

Dudemeister 09-27-2011 03:54 AM

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!! =)

acid_kewpie 09-27-2011 04:07 AM

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.

Dudemeister 09-27-2011 04:19 AM

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?

grail 09-27-2011 11:24 AM

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.


All times are GMT -5. The time now is 06:35 PM.