From: Gustav Eek Date: Mon, 21 Jun 2021 11:44:05 +0000 (+0200) Subject: [linux-intro] Complete example files X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;h=08685aea03c3ed2076f6f26644d9487fa0eb4493;p=inbyggd-frihet-wiki.git [linux-intro] Complete example files Add an example script file which computes prime numbers, and complete with a C version for execution efficiency comparison. --- diff --git a/linux-intro/files/files.sh b/linux-intro/files/files.sh index 35cf42b..69c6c95 100644 --- a/linux-intro/files/files.sh +++ b/linux-intro/files/files.sh @@ -11,6 +11,8 @@ folders="\ 05.avsl \ " +# Include example text file to each lesson + text=text.txt for f in $folders; do @@ -21,5 +23,11 @@ for f in $folders; do done done +# Include script examples to lesson 4 + +cp prime.sh prime.c $file/04.stil + +# Compress and cleanup + tar -cvf $file.tar $file rm -fr $file diff --git a/linux-intro/files/linux-intro.tar b/linux-intro/files/linux-intro.tar index d89e455..527562b 100644 Binary files a/linux-intro/files/linux-intro.tar and b/linux-intro/files/linux-intro.tar differ diff --git a/linux-intro/files/prime-again.sh b/linux-intro/files/prime-again.sh new file mode 100644 index 0000000..116c1bb --- /dev/null +++ b/linux-intro/files/prime-again.sh @@ -0,0 +1,16 @@ +#!/bin/env bash + +# Prints prime numbers, dividable evenly only by itself and one + +# Here are alternative methods + +N=99 +i=1; printf "%4d" $i +i=2; printf "%4d" $i +while [[ $i < $N ]]; do + i=$(( $i + 1 )) + if ! seq 2 $(( i - 1 )) | sed "s/^/$i%/" | bc | grep -q "^0$"; then + printf "%4d" $i + fi +done +printf "\n" diff --git a/linux-intro/files/prime.c b/linux-intro/files/prime.c new file mode 100644 index 0000000..639d4a8 --- /dev/null +++ b/linux-intro/files/prime.c @@ -0,0 +1,20 @@ +/* Prints prime numbers, dividable evenly only by itself and one */ + +#include + +#define N 99 + +int main(void) +{ + int i, j; + i = 1; + printf ("%5d", i); + while (i < N) + { + i++; + j = 2; + while (i % j != 0) j++; + if (j == i) printf ("%5d", i); + } + printf ("\n"); +} diff --git a/linux-intro/files/prime.sh b/linux-intro/files/prime.sh new file mode 100644 index 0000000..2d91899 --- /dev/null +++ b/linux-intro/files/prime.sh @@ -0,0 +1,19 @@ +#!/bin/env bash + +# Prints prime numbers, dividable evenly only by itself and one + +N=99 + +i=1 +printf "%5d" $i +while [[ $i < $N ]]; do + i=$(( $i + 1 )) + j=2 + while [[ $(( $i % $j )) != 0 ]]; do + j=$(( $j + 1 )) + done + if [[ $j = $i ]]; then + printf "%5d" $i + fi +done +printf "\n"