Started övning 4, labb 6

This commit is contained in:
Jack-Benny Persson 2013-12-19 15:29:29 +01:00
parent 71534704f2
commit b446f85659
7 changed files with 171 additions and 0 deletions

21
Labb6/ovning1.sh Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 1, labb 6
# Arguments
# Define some variables
Arg1=$1
Arg2=$2
# Sanity checks
if [ $# -ne 2 ]; then
printf "`basename $0` requires two arguments\n"
exit 2
fi
printf "Argument 1 is ${Arg1}\n"
printf "Argument 2 is ${Arg2}\n"
exit 0

20
Labb6/ovning2.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 2, labb 6
# Check EUID and empty /tmp/testdir/
# Sanity check
if [ $EUID -ne 0 ]; then
echo "`basename $0` must run as root"
exit 2
fi
rm /tmp/testdir/* 2> /dev/null
if [ $? -ne 0 ]; then
echo "Something went wrong when deleting files in /tmp/testdir/"
exit 2
fi
exit 0

35
Labb6/ovning3.sh Executable file
View File

@ -0,0 +1,35 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 3, labb 6
# Draw a line with a function
# Define variables
Nr=$1
Iterate=0
# Sanity checks
if [ $# -ne 1 ]; then
echo "`basename $0` requires one argument"
exit 2
fi
if ! [ $Nr -eq $Nr &> /dev/null ]; then
echo "Use only integers"
exit 2
fi
# Define our functions
draw()
{
while [ $Iterate -lt $Nr ]; do
printf "*"
((Iterate++))
done
printf "\n"
}
draw
exit 0

34
Labb6/ovning4/calc.sh Normal file
View File

@ -0,0 +1,34 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 4, labb 6
# Make a calculator that could do add, substract, divide and times
# Define our variables
Nr1=$1
Nr2=$2
# Define our functions
add()
{
Sum=$((Nr1+Nr2))
}
sub()
{
Sum=$((Nr1-Nr2))
}
div()
{
Sum=$((Nr1/Nr2))
}
tim()
{
Sum=$((Nr*Nr2))
}
# Main

13
Labb6/test_args.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Labb 6, testing some arguments
# Echo all arguments
echo $*
# Echo the total number of arguments
echo $#
exit 0

19
Labb6/test_array.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Just testing arrays
Content=`ls`
Nr=0
for i in $Content; do
Word[$Nr]=$i
((Nr++))
done
echo ${Word[@]}
exit 0

29
Labb6/test_func.sh Executable file
View File

@ -0,0 +1,29 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Testing functions
Nr1=$1
Nr2=$2
# Sanity checks
if [ $# -ne 2 ]; then
echo "`basename $0` requires two arguments (integers)"
exit 2
fi
# Functions
add()
{
Sum=$((Nr1+Nr2))
return $Sum
}
# Main
add
echo $?
echo "Nr1 contained ${#Nr1} chars, Nr2 contained ${#Nr2}"
exit 0