Solved the error - Syntax error end of file unexpected (expecting then)

While running your (Bash) Shell Script have you seen the below error? 

Syntax error: end of file unexpected (expecting "then")

You may think that you have written your Bash script perfectly fine but the below error makes you wonder where the error actually is? This happens when the file is edited in Windows and the editor does not have an option to save files in "Unix" file format. The shell is seeing the word then^M with a carriage return on the end.

Here is the fix for it. From the prompt execute the following command:

sed -i 's/\r$//' filename where filename is the name of your bash script file.

This will ensure that the line ending is proper and executing your file with the sh command will execute your Bash script perfectly well. 

Enjoy shell scripting!!!



Install Grafana v9.0.5 in IBM zLinux (s390x)

The following article contains the steps that must be followed to install Grafana v9.0.5 from source code on IBM zLinux (s390x).

Pre-requisites for Grafana install

The following software must be installed in a zLinux (s390x) machine. The working folder in this article is assumed to be: grafana-install.

            a. NodeJS version: v16.15.1. (Ensure you execute: export NODE_OPTIONS=--max-old-space-size=12228)

            b. Yarn: v4.0.0-rc.12 (Execute from grafana-install directory: sudo yarn set version 4.0.0-rc.12). This version of yarn contains an important fix related to running Yarn (berry) in Bigendian systems such as zLinux. (More info...)

            c. Go: go version go1.17.11 linux/s390x. Ensure the environment variables for Go (GOPATH and GOROOT) are set:

        d. make: GNU Make 4.3 

Configuration

        1. Download the latest Grafana Source into 'grafana-install' directory using:

                sudo wget https://github.com/grafana/grafana/archive/refs/tags/v9.0.5.tar.gz

       2. Now extract it using: sudo tar -C . -xzf v9.0.5.tar.gz

      The above step will extract Grafana v9.0.5 to directory named: grafana-9.0.5

2. Edit the file grafana-install/grafana-9.0.5/.yarnrc.yml and do the following changes:

            a. Point the yarnPath to ../.yarn/releases/yarn-4.0.0-rc.12.cjs

            b. From the plugins section remove the following entries:

                    -   path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs

                        spec: "@yarnpkg/plugin-typescript"

                    -  path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

                       spec: "@yarnpkg/plugin-interactive-tools"

3. Open grafana-install/grafana-9.0.5/package.json and 

            - change package manager entry as: "packageManager": yarn@4.0.0-rc.12

            under the scripts section change: 

                  "postinstall""husky install" to 

                  "prepare""husky install" 

            - from the devDependencies section, remove all the @swc entries such as:

                    "@swc/core": "1.2.187", 

                    "@swc/helpers": "0.3.13"

4. Remove all occurrences of @swc/helpers from the following files:

        - packages/grafana-data/package.json

        - packages/grafana-schema/package.json

        - packages/grafana-ui/package.json

5. Remove the ts-node section shown below from: scripts/cli/tsconfig.json

        "ts-node": {

            "transpileOnly": true,

            "transpiler": "ts-node/transpilers/swc-experimental"

          }

6. Remove source map generation from webpack for development environment. For this open the file: scripts/webpack/webpack.dev.js and remove the entry devtool: 'inline-source-map'. Please note that there are still issues around the building Grafana v9.0.5 for production (yarn build), hence this article will focus on the dev build only. 


Install the server and client 

With your current directory as grafana-install verify that the yarn version is yarn-4.0.0-rc.12 with the command: yarn -v. (If not execute: sudo yarn set version 4.0.0-rc.12)

1. Change directory to ./grafana-9.0.5 and execute: make run. This will install the server portion of Grafana with all Go libraries/dependencies. Once this completes it goes into a wait state. Press Cntrl + C and exit.

2. Next step is to install the client dependencies. So execute: sudo yarn install 

3. Once client dependencies are installed execute: sudo yarn start. Once it succeeds press Cntrl + C to exit.

4. Now we are all ready to start Grafana. For this navigate to the ./bin directory and start the executable titled: grafana-server. Once it starts at port 3000, open a new browser window and navigate to http://<server_name>:3000

5. Happy visualizations with Grafana!!!


Installing GO (Golang) v1.17.1 on Z-Linux (s390x)

Please follow the given steps for installing Golang v1.17.1 on zLinux (s390x):

1. From your home directory execute:                 wget https://storage.googleapis.com/golang/go1.17.1.linux-s390x.tar.gz

2. Execute: chmod ugo+r go1.17.1.linux-s390x.tar.gz

3. Extract using: sudo tar -C /usr/local -xzf go1.17.1.linux-s390x.tar.gz

4. Define a symlink /usr/bin/go pointing to -> /usr/local/go/bin/go using the command: sudo ln -s /usr/local/go/bin/go /usr/bin/go (The directory /usr/bin must be specified in your PATH env variable. Else add it to the PATH)

5. Verify that go is installed with: go version

Installing Grafana v8.1.7 from source code on zLinux (s390x)

In this article we see how to install Grafana v8.1.7 from source code on zLinux (s390x). By obtaining from source code we can do customizations to Grafana such as adding more themes, behaviors etc. Even though this article says zLinux the below steps should generally work for all flavors of Linux such as Ubuntu, Redhat,  CentOS etc. Also it is important to note that these instructions work only till Grafana v8.2.7 specifically in zLinux because after Grafana v3 and above some breaking changes have been made to the build process and as of writing this article these breaking changes have not been fixed for zLinux. Other distributions just work fine.

Build the Grafana Server

PRE-REQUISITES

  1. Create a working directory (hereafter called <work_dir>) for Grafana (In this example it is in ~/software/grafana_v8)
  2. Ensure make is installed by executing: make --version. If make is not installed please install it.
  3. GoLang Compiler (v1.17.1 or later): Ensure go is installed by executing: go version (If not installed follow this article to install GoLang v1.17.1 to folder /usr/local/go)
  4. Specify the following 'env' variables:
    export GO111MODULE=on
    export GOPATH=~/software/go-path (create this directory if required)
    export GOROOT=/usr/local/go (assuming go is installed here)
    export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
  5. NodeJS (v16.15.1) and Yarn (v1.22.19):  Ensure NodeJS and Yarn are installed by executing: node --version && yarn --version  (If not installed follow this article to install NodeJS v16.15.1)
  6. Execute the following:
    1. export PATH=$PATH:~/software/<node_install_dir>/bin
    2. export NODE_OPTIONS="--max-old-space-size=8192"

GET THE SOURCE CODE and INSTALL THE SERVER

From the <work_dir> download the source code of Grafana v8.1.7 using: 

wget https://github.com/grafana/grafana/archive/refs/tags/v8.1.7.tar.gz

Extract it to the current directory (hereafter referred to as <grafana_root>) with:

sudo tar -C . -xzf v8.1.7.tar.gz

- Go to the path: <grafana_root> and execute: make run (this will install server)
- Once the server starts at port 3000, stop the server (press Cntrl+C) and proceed to next step

INSTALL AND START CLIENT

From the directory <grafana_root> execute the following (this might take a while to complete say a few minutes):
yarn install --pure-lockfile - this will install client dependencies
yarn start - this will build the client and start it. Once the client has started stop it (press Cntrl+C) and proceed to next step.

START THE SERVER

  • (Optional Step) If you want to change the default Grafana port (3000), edit <grafana_root>/conf/default.ini and change the <http_port> value.
  • Change directory to <grafana_root>/bin directory and execute: ./grafana-server - This will run the server at port <http_port> (default 3000). 
  • DONE!!! Open http://<server>:<port> in a browser to see the Grafana login screen. The default username, pwd is admin, admin respectively.

Thank you and happy metrics visualization!!!

Useful Linux Commands

We are all familiar with basic Linux commands such as pwd, ls, cd etc. Other than that given below are a few more commands that we might find useful in our day to day work.

1. Unzip a compressed file (.zip, .tar.xz, .tar.gz)

In Linux compressed files (zip files) come in different formats such as .zip, .tar.xz, .tar.gz etc. Here are some useful commands to unzip these files.

> Unzip a .zip file

    Command: unzip file.zip -d /path-to-unzip/

    Example: unzip myfile.zip -d /dest/bin

> Unzip a .tar.xz file

    Command: tar -C /dest/ -xf file.tar.xz

    Example: tar -C /usr/local -xf node-v16.15.1-linux-s390x.tar.xz 

> Unzip a .tar.gz file

    Command: tar -C /dest/ -xzf file.tar.gz

    Example: tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz

2. Download a file from a URL

In order to download a file from a specific URL execute the wget command with the url as the parameter as:

    Command: wget <download-url>

    Example: wget https://go.dev/dl/go1.17.11.linux-s390x.tar.gz

3. Create a symbolic link (symlink) to a file

A symbolic link is similar to a Windows shortcut that points to another file or folder. In order to create a symlink refer the below example:

Command: ln -s <path-to-file-or-folder> <location>/<symlink-name> 

Example: ln -s /usr/local/nodejs/bin/node /usr/bin/node 

The above command creates a symlink called node in the directory /usr/bin/ which points to the executable file node in directory /usr/local/nodejs/bin/

4. Search for a file with specific criteria

We can use the find command to search for files with different criteria. 

Criteria: Search for a file named .gitignore in the current directory

Command: find . -name ".gitignore" -type f 

Criteria: Search for all files containing text "RMF" in the current directory

Command: grep -Ril "RMF" .

Here: 

  • R stands for recursive.
  • i stands for ignore case (optional).
  • l stands for "show the file name, not the result itself".
  • . stands for current directory


5. Executing a command from the history

Do you know that you can retrieve and execute previously executed commands in your terminal with the history command?

Command: history
Displays a list of commands we have executed in our terminal


To execute a specific command just type the character ! with the line number
Example: !7 prints the current working directory







Blog Archive