Code

On this page you will find four sections:

  • Kotlin (example code)
  • Using curl (db access)
  • Bash-shell script
  • Updated C-standard

I have some Kotlin code examples. For those of you who have bought my book “The Linux Book”, I have updated the menu based on bash-shell. In the book “Linux server- og utviklingsmiljø” I have also updated “C-standard”. You will see this in the last section of this page.

Kotlin

Many times, we need to access REST/API-solution in Kotlin we can use Java 11 HttpClient for easy access. I have some code below which shows this.

You can use HTTP get request in sync mode or in Async mode. In all my examples below I use sync mode.

In my first example, I access REST/API-soltion with POST and I get messages. In the code, we check how much time the system is using for reading the data.

  • import java.net.URI
  • import java.net.http.HttpClient
  • import java.net.http.HttpRequest
  • import java.net.http.HttpResponse
  • import java.time.Duration
  • import kotlin.system.measureNanoTime
  • import kotlin.system.measureTimeMillis
  • import kotlin.time.measureTime
  • fun main(args: Array<String>) {
  •     httpGetRequestSync()
  • }
  • fun printBorder(border: String, timesToRepeat: Int) {
  •     repeat(timesToRepeat) {
  •         print(border)
  •     }
  •     println()
  • }
  • fun httpGetRequestSync() {
  •     val httpClient: HttpClient = HttpClient.newBuilder()
  •         .connectTimeout(Duration.ofSeconds(10))
  •         .build()
  •     //   val SAML_TOKEN =”XXXX”
  •     val SAML_TOKEN = System.getenv(“SAML_TOKEN”)
  •     // neste linje kan fjernes
  •     val elapsed = measureTimeMillis {
  •         val start_part: Int = 105100
  •         val slutt_part: Int = 105102
  •         // fun sum(start_part: Int, slutt_part: Int): Int {
  •         //  return slutt_part – start_part
  •         // }
  •         val diff = slutt_part – start_part
  •         // println(“Antall parter $diff”)
  •         for (partsnummer in start_part..slutt_part) {
  •             val elapsed_per = measureTimeMillis {
  •                 val border = “-“
  •                 val timesToRepeat = 150
  •                 printBorder(border, timesToRepeat)
  •                 val requestHead = HttpRequest.newBuilder()
  •                     .GET()
  •                     .uri(URI.create(“http://databoks-miljo.no/api/meldinger?partsnummer=${partsnummer}&spraak=nb_NO&hentAktive=true”))
  •                     .header(“Authorization”, “SAML/GZ ” + SAML_TOKEN)
  •                     .header(“Content-Type”, “application/json”)
  •                     .header(“Accept”, “application/json”)
  •                     .header(“Klientid”, “curl”)
  •                     .header(“Korrelasjonsid”, “korrelasjon”)
  •                     .build()
  •                 val httpResponse = httpClient.send(requestHead, HttpResponse.BodyHandlers.ofString())
  •                 println(“httpResponse statusCode = ${httpResponse.statusCode()}”)
  •                 println(httpResponse.body())
  •             }
  •             println(“Måler tiden totalt i antall milliskunder ${elapsed_per}”)
  •         }
  •         val border = “=”
  •         val timesToRepeat = 150
  •         printBorder(border, timesToRepeat)
  •         println(“Antall parter $diff”)
  •     }
  • // 2 neste linjene kan fjernes
  •     val border = “=”
  •     val timesToRepeat = 150
  •     printBorder(border, timesToRepeat)
  •     val sekunder: Double = elapsed / 1000.0
  •     println(“Måler tiden totalt i antall sekunder ${sekunder}”)
  • }

In this section I have som example code in Kotlin

Please check the Kotlin links.

New to Kotlin, check out the links below.

Example with bash and curl instead of kotlin program for acessing a REST-interface.

Using curl (db access)

$ more adduser.sh

  • #!/bin/bash
  • i=101000
  • while [ $i -ne 102000 ]
  • do
  •         i=$(($i+1))
  •         echo “$i”
  •             curl -v -X POST “$DATABOX_HOST/api/meldinger/publiser/$i” -d ‘{ “id_melding”: 2, “domene”: “innkreving”, “kategori”: “kat” }’ –header “Authorization: SAML/GZ $SAML_TOKEN” –header ‘Content-Type: application/json’ –header ‘Accept: app
  • lication/json’ –header ‘Klientid: curl’ –header ‘Korrelasjonsid: korrelasjon’ –header ‘Meldingsid: mid’
  • done

Clean start

  • sudo apt install maven
  • git clone https://github.com/tax/openshift-reference-springboot-server.git
  • openshift-reference-springboot-server/
  • mvn clean install
  • cd targetgit
  • java -jar openshift-reference-springboot-server-2.0-SNAPSHOT.jar

Bash-script

Below you find an updated global bash script (menu) from my book “The Linux Book”.

  • #!/bin/bash
  • clear
  • while test “$answer” != “0”
  • do
  • clear
  • echo “”
  • echo ”                     1 – List files(directory) “
  • echo ”                     2 – Change catalog        “
  • echo ”                     3 – File check           “
  • echo ”                     4 – Copy file            “
  • echo ”                     5 – Remove file           “
  • echo ”                     6 – Change filename       “
  • echo ”                     7 – Output file to screen “
  • echo ”                     8 – Output file to printer”
  • echo ”                     9 – Users logged in to the computer”
  • echo ”                    10 – Who am i”
  • echo ”                    11 – Current directory”
  • echo ”                    12 – Make a directory”
  • echo ”                    13 – Delete empty directory”
  • echo ”                    14 – Quit the menu”
  • echo “”
  • echo -n ”         Choose a function (1/../14) and press <RETURN> key :”
  • read answer
  • cd $home
  • #
  • #        1 – List files(directory)
  • #
  • if test “$answer” = “1”;then
  •         clear
  •         echo “”
  •         echo “Current/working directory is :”
  •         pwd
  •         echo “”
  •         echo “List files and directories”
  •         ls -la | more
  •         echo “”
  •         echo -n “Press <RETURN> to continue :”
  •         read stop
  • fi
  • #
  • #       2 – Change directory
  • #
  • if test “$answer” = “2”;then
  •         clear
  •         echo “”
  •         echo -n “Current/working directory :”
  •         pwd
  •         echo “”
  •         echo “To continue in current/working directory answer with
  • ‘.'(period) and press <RETURN>”
  •         echo “If you want to move one level up, answer with
  • ‘..'(two periods) and press <RETURN>”
  •         echo “”
  •         echo “If you want to move back to your own home-directory you only need to press <RETURN>”
  •         echo “”
  •         echo “If you want to move to a child-directory just write the name of the directory and press <RETURN>”
  •         echo “”
  •         echo -n “Which directory would you like to use :”
  •         read dir
  •         cd $dir
  •         echo “”
  •         echo -n “New active directory is :”
  •         pwd
  •         echo “”
  •         echo -n “Press <RETURN> to continue :”
  •         read stop
  • fi
  • #
  • #       3 – File check
  • #
  • if test “$answer” = “3”;then
  •         clear
  •         echo “”
  •         echo -n “Which file do you wish to check :”
  •         read file
  •         if test -d “$file”;then
  •            echo “”
  •            echo ”   “$file” is a directory”
  •            echo “”
  •            echo  -n “Press <RETURN> to continue :”
  •            read stop
  •         fi
  •         if test -r “$file”;then
  •            echo “”
  •            echo “We can read the file”
  •         fi
  •         if test -w “$file”;then
  •            echo “”
  •            echo “We can write to the file”
  •         fi
  •         if test ! -f “$file”;then
  •            echo “”
  •            echo “”$file” is not a sub-directory or a file in the specified directory”
  •         fi
  •         echo “”
  •         echo -n “Press <RETURN> to continue :”
  •         read stop
  • fi
  • #
  • #       4 – Copy file
  • #
  • if test “$answer” = “4”;then
  •         clear
  •         echo “”
  •         echo “”
  •         echo -n “You are now placed in directory :”
  •         pwd
  •         echo “”
  •         echo -n “Which file do you want to copy :”
  •         read fromfile
  •         echo “”
  •         if test  -s “$fromfile”;then
  •            echo -n “Which directory/file would you like to copy to :”
  •            read tofile
  •            if test ! -s “$tofile”;then
  •               cp “$fromfile” “$tofile”
  •            else
  •               echo “”
  •               echo -n “”$tofile” exist do you want overwrite (y/n) :”
  •               read yes
  •               if test “$yes” = “y”;then
  •                  cp “$fromfile” “$tofile”
  •               fi
  •            fi
  •         else
  •            echo “”
  •            echo -n “”$fromfile” do not exist – press <RETURN> :”
  •            read stop
  •         fi
  • fi
  • #
  • #       5 – Delete file
  • #
  • if test “$answer” = “5”;then
  •         clear
  •         echo “”
  •         echo -n “Which file do you want to delete :”
  •         read file
  •         if test -f “$file”;then
  •            clear
  •            echo “”
  •            echo -n “”$file” exists. Do you really want to delete the file (y/n) :”
  •            read yes
  •            if test “$yes” = “y”;then
  •               rm $file
  •            fi
  •        else echo “”
  •              echo -n “”$file” do not exists – press <RETURN>”
  •              read stop
  •         fi
  • fi
  • #
  • #     6 – Change name on file
  • #
  • if test “$answer” = “6”;then
  •         clear
  •         echo “”
  •         echo -n “You are now in directory :”
  •         pwd
  •         echo “”
  •         echo  -n “Which filename would you like to change :”
  •         read file
  •         if test -f “$file”;then
  •            clear
  •            echo “”
  •            echo -n “”$file” exists. Please give a new name :”
  •            read newname
  •            if test ! -f “$newname”;then
  •               mv “$file” “$newname”
  •            else
  •               echo “”
  •               echo  -n “”$newname” exists – press <RETURN> :”
  •               read stop
  •            fi
  •         else
  •            echo “”
  •            echo -n “”$file” do not exists – press <RETURN> :”
  •            read stop
  •         fi
  • fi
  • #
  • #       7 – Output file to monitor screen
  • #
  • if test “$answer” = “7”;then
  •         clear
  •         echo “”
  •         echo “If the file is more than one page you may continue by pressing the <RETURN> key”
  •         echo “”
  •         echo -n “Which file would you like to see on your monitor screen :”
  •         read file
  •         if test  -s “$file”;then
  •            more “$file”
  •            echo “”
  •            echo -n “Press <RETURN> to continue :”
  •            read stop
  •         else
  •            echo “”
  •            echo “The file do not exists or is empty”
  •         echo “”
  •            echo -n “Press <RETURN> to continue :”
  •            read stop
  •         fi
  • fi
  • #
  • #       8 – Output file to printer
  • #
  • if test “$answer” = “8”;then
  •         clear
  •         echo “”
  •         echo -n “Which file would you like to print :”
  •         read file
  •         echo “”
  •         lpr $file    
  •      echo “”
  • fi
  • #
  • #      9  – Users logged on the system
  • #
  • if test “$answer” = “9”;then
  •      clear
  •      echo “”
  •      echo -n “Please, wait…..”
  •         who > /tmp/who-list
  •         clear
  •         echo “”
  •         echo “Active users logged on the system :”
  •         echo “”
  •      cat /tmp/who-list
  •      echo “”
  •         echo -n “Number of users logged on the system :”
  •      cat /tmp/who-list | wc -l
  •      echo “”
  •         echo -n “Press <RETURN> to continue:”
  •         read stop
  • fi
  • #
  • #      10 – Who am I
  • #
  • if test “$answer” = “10”;then
  •         clear
  •         echo “”
  •         echo -n “I am user :”
  •         whoami
  •         echo “”
  •         echo -n “Press <RETURN> to continue :”
  •         read stop
  • fi
  • #
  • #     11 – Current directory
  • #
  • if test “$answer” = “11”;then
  •         clear
  •         echo “”
  •         echo -n “Current directory is :”
  •         pwd
  •      echo “”
  •         echo -n “Press <RETURN> to continue :”
  •         read stop
  • fi
  • #
  • #    12 – Make a directory
  • #
  • if test “$answer” = “12”;then
  •         clear
  •         echo “”
  •         echo -n “Current directory is :”
  •         pwd
  •         echo ”  “
  •         echo -n “Is this the right directory – would you like to continue (y/n) :”
  •         read yes
  •         if test “$yes” = “y”;then
  •            echo “”
  •            echo -n “What is the name of the new sub-directory :”
  •            read subdir
  •            if test ! -d  “$subdir”;then
  •               mkdir “$subdir”
  •               if test ! -d “$subdir”;then
  •                  echo “”
  •                  echo -n “”$subdir” this directory do not exists :”
  •                  read stop
  •               fi
  •            else
  •               echo “”
  •               echo -n “”$subdir” this directory do already exists – press <RETURN> :”
  •               read stop
  •            fi
  •         fi
  • fi
  • #
  • #      13 – Delete empty directory
  • #
  • if test “$answer” = “13”;then
  •         clear
  •         echo “”
  •         echo -n “Current directory is :”
  •         pwd
  •         echo “”
  •         echo -n “Which empty directory would you like to delete :”
  •         read file
  •         if test  -d “$file”;then
  •            cd $file
  •            echo “”
  •            echo “Checking directory”
  •            ls -la
  •            cd ..
  •            echo “”
  •            echo -n “Any files in that directory? – (y/n) :”
  •            read yes
  •            if test “$yes” = “y”;then
  •               rmdir “$file”
  •               if test ! -d “$file”;then
  •                  echo “”
  •                  echo -n “”$file” is deleted – press <RETURN> :”
  •                  read stop
  •               fi
  •            fi
  •         else
  •            echo “”
  •            echo -n “”$file” is not a directory – press <RETURN> :”
  •            read stop
  •         fi
  • fi
  • #
  • #      14 – Quit the menu    
  • #
  • if test “$answer” = “14”;then
  •      exit;
  • fi
  • done

C

Updated C-standard from my book “Linux server- og utviklingsmiljø”.

  • C client-programs and C-functions should be written using Hungarian Standard. Hungarian Standard is a naming standard for variables, where the first one to three characters represents the datatype, and the name of the variable follows with initial caps for each word (ex. usHoldLimit).
  • Use Hungarian Standard when declaring variables
  • Use the formatted-text boxes in the example below to mark start of program and start of each function  
  • Write extended comments within the functions using ‘/*’ and ‘*/’
  • Use 4 blanks as indent, no TABs
  • ANSI-standard, with functions prototype at top and paramters declared within the pharntesis of each function
  • Variable-names should be declared starting in column 32.
  • Let start- and end-of-loop signs (‘{‘ and ‘}’) be on seperate lines
  • If a seperate client-program, let the main-function be the first function
  • When accessing other programs within C-programs, do NOT use malloc() and free(), instead, use mi_alloc() and mi_free()
  • Type                 Description
  • Int                   iInteger
  • unsigned int   uiInteger
  • Short               siShort
  • unsigned short  usShort
  • Float               fNumber
  • Char                cByte
  • Char                sString
  • int *                 piPointer
  • unsigned int *  uiInteger
  • short *             psiShort
  • unsigned short *  pusShort
  • float *              pfFloat
  • char *              psPointer
  • Boolean           bExist
  • image pointer * pimImage
  • void                 vVoid
  • void *               pvVoid
  • struct              stStruct
  • struct *            pstStruct
  • char *              apsArray[190];
  • [david@nittedal david]$ cat c-mal.c
  • /*
  • +————————————————–+
  • | Program name :                                            |
  • | Author :                                                        |
  • | Last changed :                                     |
  • | Description :                                      |
  • |                                                         |
  • +————————————————–+
  • */
  • /* Include files                                     */
  • /* Define’s                                           */
  • /* Prototypes                                      */
  • /*
  • +————————————————–+
  • | Function :                                          |
  • | Description:                                       |
  • |                                                                    |                       
  • |                                                                    |
  • |                                                                    |
  • +————————————————–+
  • */  
  • Example:
  • /*
  • +—————————————————+
  • | Program name : sPrepPar.c                   |
  • | Author : David Elboth            |
  • | Last changed : 6/8-2004                                 |
  • | _____________________________ ____ |
  • | Description:                                       |
  • |                                                                    |
  • | Receives a text-string as the only                |
  • | parameter and changes all ‘ ‘ to ‘+’.         |
  • |                                                                   |
  • +—————————————————+
  • */
  • /* Include files */
  • #include <mi.h>
  • /* Define’s */
  • #define PLUS_SIGN ‘+’
  • #define BLANK_SIGN ‘ ‘
  • #define END_STRING ‘\0’
  • /* Prototypes */
  • mi_text *_PrepPar (mi_text *psPar);
  • /*
  • +———————————————————–+
  • | Function : _PrepPar |
  • | Description: Converts the input string psPar to a regular |
  • | character-pointer. Scans to the string and substitutes |
  • | blanks to |
  • | ‘+’. If the string contains leading blanks, these are |
  • | removed. |
  • +———————————————————–+
  • */
  • mi_text *_PrepPar (mi_text *psPar)
  • {
  • char *psPlusWord;
  • char *psHead;
  • psPlusWord = mi_text_to_string (psPar);
  • /* Remove initial blanks */
  • for (; *psPlusWord == BLANK_SIGN; psPlusWord++) {}
  • psHead = psPlusWord;
  • while (*psPlusWord != END_STRING)
  • {
  • if (*psPlusWord == BLANK_SIGN)
  • {
  • *psPlusWord = PLUS_SIGN;
  • }
  • psPlusWord++;
  • }
  • /* Move pointer to first byte */
  • psPlusWord = psHead;
  • /* Convert the charcter-pointer to Illustra-text */
  • psPar = mi_string_to_text (psPlusWord);
  • /* Return the converted string to caller-function */
  • return (psPar);
  • }