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