Assign Output of Shell Command To Variable in Bash

var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)

OR use backticks based syntax as follows to assign output of a Linux command to a variable:

var=`command-name-here`
var=`command-name-here arg1`
var=`/path/to/command`
var=`/path/to/command arg1 arg2`

Examples

## store date command output to $now  ##
now=$(date)
## alternate syntax ##
now=`date`

To display back result (or output stored in a variable called $now) use the echo or printf command:

echo "$now"
printf "%s\n" "$now"

References
https://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/

Tweaks for Firefox

Enable Hardware Acceleration

  1. Go to about:preferences
  2. In General, scroll down to Performance
  3. Uncheck the box for Use recommended performance settings
  4. Check the box for Use hardware acceleration when available

Disable Data Collection and Telemetry

  1. Go to about:preferences
  2. Go to Privacy & Security, and scroll down to Firefox Data Collection and Use
  3. Uncheck all boxes in this section
  4. Restart Firefox

Essential About:Config Tweaks

  • Set browser.download.animateNotifications to False
  • Set security.dialog_enable_delay to 0
  • Set network.prefetch-next to False (Only on slow internet connections)
  • Set browser.newtabpage.activity-stream.feeds.telemetry to false
  • Set browser.newtabpage.activity-stream.telemetry to false
  • Set browser.ping-centre.telemetry to false
  • Set toolkit.telemetry.archive.enabled to false
  • Set toolkit.telemetry.bhrPing.enabled to false
  • Set toolkit.telemetry.enabled to false
  • Set toolkit.telemetry.firstShutdownPing.enabled to false
  • Set toolkit.telemetry.hybridContent.enabled to false
  • Set toolkit.telemetry.newProfilePing.enabled to false
  • Set toolkit.telemetry.reportingpolicy.firstRun to false
  • Set toolkit.telemetry.shutdownPingSender.enabled to false
  • Set toolkit.telemetry.unified to false
  • Set toolkit.telemetry.updatePing.enabled to false

Remove Built-in Firefox Add-ons

  • In about:config, set reader.parse-on-load.enabled to False
  • In about:config, set reader.parse-on-load.force-enabled to False
  • In about:config, set browser.pocket.enabled to False
  • In about:config, set loop.enabled to False

References
https://www.makeuseof.com/tag/speed-up-firefox-immediately-with-these-6-simple-tweaks/

MySQL Error: : ‘Access denied for user ‘root’@’localhost’

sudo mysql
-- for MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

-- for MariaDB
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');

With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root and there isn’t any need to restart mysqld or start it with special privileges.

References
https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost

Dependency Injection in ASP.NET Core

Creating the View Model

Create the Models folder and create the following viewModel.

public class ProductViewModel
{
    public int Id { get; set; }
    public string Name { get; internal set; }
}

Adding the Service

Create the folder Services and add a class with the name ProductService.cs.

using DependencyInjection.Models;
using System.Collections.Generic;
 
namespace DependencyInjection.Service
{
 
    public interface IProductService
    {
        List<ProductViewModel> getAll();
    }
 
    public class ProductService : IProductService
    {
        public List<ProductViewModel> getAll()
        {
            return new List<ProductViewModel>
            {
                new ProductViewModel {Id = 1, Name = "Pen Drive" },
                new ProductViewModel {Id = 2, Name = "Memory Card" },
                new ProductViewModel {Id = 3, Name = "Mobile Phone" },
                new ProductViewModel {Id = 4, Name = "Tablet" },
                new ProductViewModel {Id = 5, Name = "Desktop PC" } ,
            };
        }
     }
}

Using the Service in Controller

using DependencyInjection.Service;
using Microsoft.AspNetCore.Mvc;
 
namespace DependencyInjection.Controllers
{
    public class HomeController : Controller
    {
        private IProductService _productService;
 
        public HomeController(IProductService productService)
        {
            _productService = productService;
        }
 
        public IActionResult Index()
        {
            return View(_productService.getAll());
        }
    }
}

Register the Service

The last step is to register the service in the Dependency Injection container.

Open the startup.cs and goto to ConfigureServices method. This is where all services are configured for dependency injection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddTransient<IProductService, ProductService>();
}

Managing the Service Lifetime

There are three ways, by which you can do that. And it in turn decides how the DI Framework manages the lifecycle of the services.

  1. Transient: creates a new instance of the service, every time you request it.
  2. Scoped: creates a new instance for every scope. (Each request is a Scope). Within the scope, it reuses the existing service.
  3. Singleton: Creates a new Service only once during the application lifetime, and uses it everywhere
services.AddTransient<ITransientService, SomeService>();
services.AddScoped<IScopedService, SomeService>();
services.AddSingleton<ISingletonService, SomeService>();

References
https://www.tektutorialshub.com/asp-net-core/asp-net-core-dependency-injection/
https://www.tektutorialshub.com/asp-net-core/asp-net-core-dependency-injection-lifetime/

Convert MP4 video to MP3 audio with FFmpeg

ffmpeg -i filename.mp4 filename.mp3

fixed bit rate :

ffmpeg -i video.mp4 -b:a 192K -vn music.mp3

variable bit rate :

ffmpeg -i in.mp4 -q:a 0 -map a out.mp3

variable bit rate of 165 with libmp3lame encoder :

ffmpeg -vn -sn -dn -i input.mp4 -codec:a libmp3lame -qscale:a 4 output.mp3
  • -vn disables all video-streams from the input
  • -sn disables all subtitle-streams from the input
  • -dn disables all data-streams from the input
  • -i specifies the input file
  • -codec:a libmp3lame specifies the encoder
  • -qscale:a 4 specifies the quality target for libmp3lame
  • The last argument is the output-file. Note that the file-extension might be used to infer additional information.

References
https://superuser.com/questions/332347/how-can-i-convert-mp4-video-to-mp3-audio-with-ffmpeg
https://superuser.com/questions/1463710/convert-mp4-to-mp3-with-ffmpeg