Skip to main content

11 posts tagged with "bash"

View All Tags

Bash - Echo on the console and in a logfile in the same time

2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Bash - Echo on the console and in a logfile in the same time

In my previous article Bash - Script to add logging features to your script, I've shared a way to write information in a logfile.

By running ls -alh /tmp you will get the list of all files in the /tmp folder and display the list on your console. By running ls -alh /tmp >> application.log you won't see the list in your console since everything will be written in the application.log file.

How can we display the output of a command like ls f.i. both on the console and in a logfile?

Bash - Script to add logging features to your script

7 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Bash - Script to add logging features to your script

Also read Bash - Echo on the console and in a logfile in the same time

When you write Bash scripts and certainly when you foresee running them in a cron, you should implement a logfile. Every action fired by your script should be logging somewhere so you can start the script in a non-interactive mode and in case of need, consult the last logfile.

Below is a script I've developed in the form of a library, which means you can easily include it in your existing code without having to change anything.

You just need to include the file in your script (i.e. add a source log.sh line) then here and there foresee a log::write "Something to log". Easy no?

Introduction to fzf - Fuzzy Finder

5 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Introduction to fzf - Fuzzy Finder

As you probably know, CTRL+R in the console will give you access to your HISTORY i.e. you'll retrieve the list of the commands you previously typed in your console. Just like using UP or DOWN keys but with a very small search engine.

And it makes the job but, honestly, this is really basic, no?

The Fuzzy Finder (aka fzf) command line utility will explode the possibilities linked to searching in the history but, in reality, this is just one of the consequences of installing fzf, which is so much more powerful.

Let's have a look.

Compare environment files in the Linux console

4 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Compare environment files in the Linux console

This is a very common source of problems using .env files: you've two or more different .env file like .env and .env.example.

You're a programmer and coding a new amazing feature. You're adding one or more new environment variables to your local .env file and everything is working fine on your computer.

Boum! Your feature is buggy.

A colleague copy the source code from a versioning system like Github/GitLab or, second scenario, someone will deploy the feature on a server and your feature is broken.

Why? Because the variable(s) you've added have been added in your local .env file, on your computer only.

As you know, you have to create the variables in the .env.example file too but let's be honest, nobody thinks about it.

Batch edit of environment file

4 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Batch edit of environment file

When deploying a project on servers, we need to pay particular attention to the .env file. This file is crucial and will determine whether our application works properly (or crashes).

The normal way of doing things is to run a git clone command to get the latest version of the application from a repository (branch test for a test server, dev for an acceptance server, main for a production server).

Once cloned, the next command will be to create the .env file and it's done using cp .env.example .env.

And that's where the obligation to be meticulous begins.

Search and replace (or add) using sed

3 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Search and replace (or add) using sed

Today, I was facing (once more) with the following need: I need to update a setting in a text file but if the variable is not yet present, I need to add it.

So, in short, I need to make a search and replace or insert new line.

Using sed it's quite easy to automate the search & replace but how to append?

Bash - ASCII art

2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Bash - ASCII art

I write a lot of Bash scripts, and I like to have a similar approach for each one. One of the things I always do is to include a good old-fashioned ASCII Art banner, perhaps for the geek factor, but mostly to make a bigger visual impact.

I use https://patorjk.com/software/taag to create my banners, so let's take a closer look.

Sample ASCII art

Bash - Loading environment variables from a file

2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Bash - Loading environment variables from a file

Imagine you've a .env file like and you wish to process that file in a Bash script.

DOCKER_GIT_USEREMAIL="christophe@me.com"
DOCKER_GIT_FULLNAME="Christophe Avonture"
DOCKER_GIT_USERNAME="Me and myself"

Using a configuration file will enable you to externalize the management of your constants, as well as reuse variables from another application, such as a site developed in Laravel.

Let's take a look at how to do this as correctly as possible.

The jq utility for Linux

3 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

The jq utility for Linux

jq is a powerful utility for Linux allowing manipulating JSON data from the command line and can be integrated into shell scripts.

Using jq you can beautify JSON output but also filter it like f.i. showing only a given node.

The xmlstarlet utility for Linux

3 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

The xmlstarlet utility for Linux

xmlstarlet is a powerful utility for Linux allowing manipulating XML data from the command line and can be integrated into shell scripts.

Using xmlstarlet you can beautify XML output but also filter it like f.i. showing only a given node.