diff --git a/Labb4/laborationsrapport.md b/Labb4/laborationsrapport.md index 8d8f710..c4fc2a6 100644 --- a/Labb4/laborationsrapport.md +++ b/Labb4/laborationsrapport.md @@ -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. diff --git a/Labb4/ovning3_script1.sh b/Labb4/ovning3_script1.sh new file mode 100755 index 0000000..c543452 --- /dev/null +++ b/Labb4/ovning3_script1.sh @@ -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 " + 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 diff --git a/Labb4/ovning3_script2.sh b/Labb4/ovning3_script2.sh new file mode 100755 index 0000000..ff3c63d --- /dev/null +++ b/Labb4/ovning3_script2.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Jack-Benny Persson +# LX13 +# Övning 3, script 2, labb4 + + + diff --git a/Labb4/ovning3_ver1.sh b/Labb4/ovning3_ver1.sh deleted file mode 100755 index cae6599..0000000 --- a/Labb4/ovning3_ver1.sh +++ /dev/null @@ -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 " - 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