Klar med första scriptet till Övning 3, labb 4 samt skapat stubb till script 2

This commit is contained in:
Jack-Benny Persson 2013-12-12 20:30:13 +01:00
parent 6164767b87
commit ee25830ebf
4 changed files with 45 additions and 30 deletions

View File

@ -29,6 +29,7 @@ Istället för `let` kan man instället skriva `Summa=$((4+4)); echo $Summa` och
### Övningar ###
* [Övning 1](https://github.com/jackbenny/scripts_grundkurs/blob/master/Labb4/ovning1.sh)
* [Övning 2](https://github.com/jackbenny/scripts_grundkurs/blob/master/Labb4/ovning2.sh)
* [Övning 3, script 1](https://github.com/jackbenny/scripts_grundkurs/blob/master/Labb4/ovning3_script1.sh)
## Reflektion ##
En lösning på problemet med att Bash bara räknar med int är att istället använda `bc` (Bench Calculator) istället för `let` i Bash.

36
Labb4/ovning3_script1.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 3, version1, labb 4
# Replace words
# Define some variables
File="$1"
Old="$2"
New="$3"
# Sanity checks
if [ $# -ne 3 ]; then
if [ $# -eq 1 ]; then # If there is just one arg and
if [ -r $# ]; then # if the file exists,
cat $1 # just print it
fi
else
echo "Usage $0 <filename> <old_word> <new_word>"
exit 2 # If it dosen't, exit and print usage
fi
fi
if [ ! -r $File ]; then # If there is no such file
echo "We can't read that file"
exit 2
fi
# Main script
Data=`cat $1` # Put the content of the file in Data
Alt=${Data/$Old/$New} # Replace the word (kinda lika sed 's/old/new/')
echo "$Alt" # Print the content with the replaced words
exit 0

8
Labb4/ovning3_script2.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 3, script 2, labb4

View File

@ -1,30 +0,0 @@
#!/bin/bash
# Jack-Benny Persson
# LX13
# Övning 3, version1, labb 4
# Replace and count words
# Define some variables
File="$1"
Old="$2"
New="$3"
# Sanity checks
if [ $# -ne 3 ]; then
echo "Usage $0 <filename> <old_word> <new_word>"
exit 2
fi
if [ ! -r $File ]; then
echo "We can't read that file"
exit 2
fi
# Main script
Data=`cat $1`
Alt=${Data/$Old/$New}
echo "$Alt"
exit 0