Sunday, 29 September 2024

How to Find the Directory Where a Shell Script Resides

When working with Unix shell scripts, there are many scenarios where you need to determine the directory in which the script itself is located. For example, you might want to reference other files relative to the script’s location, regardless of where the script is being executed from. This task isn’t as straightforward as it might seem because scripts can be called from different directories or via symbolic links.

In this blog post, we’ll explore several methods to find the directory of a shell script, with examples in Bash and other Unix shells.

Why Do You Need the Script Directory?

Sometimes, scripts depend on other resources like configuration files or other scripts that are located in the same directory as the script itself. If you try to reference those files using relative paths, things can break if you run the script from a different directory. The solution is to dynamically determine the directory where the script resides and base all paths on that.

Read more »

Labels:

Friday, 27 September 2024

What is the most efficient way to deep clone an object in JavaScript?

 

1. Native Deep Cloning with structuredClone

The most efficient and now widely supported way to deep clone an object is by using the native structuredClone() function. This method works in all major browsers and Node.js (version 17 and above).

const original = { a: 1, b: { c: 2 } };
const clone = structuredClone(original);

console.log(clone); // { a: 1, b: { c: 2 } }
Read more »

Labels:

Wednesday, 25 September 2024

Automatically Updating Year in PHP for a Copyright Notice

When managing websites, it’s common to add a copyright notice in the footer. However, hardcoding the year can make your site look outdated if it isn’t updated annually. Luckily, with PHP, you can ensure the year updates dynamically without having to adjust the code each year.

Here’s how you can automatically update the year in your copyright notice using PHP.

Getting the Current Year in PHP

In PHP, the easiest way to get the current year is by using the built-in date() function. The date() function takes formatting parameters, and for the year, you simply pass 'Y' (for the four-digit year format).

Read more »

Labels:

Monday, 23 September 2024

When to Use LinkedList Over ArrayList in Java

 

When to Use LinkedList Over ArrayList in Java

Java developers frequently debate when to use LinkedList over ArrayList, as both implement the List interface but have different performance characteristics. Here’s a breakdown to help decide which one to use in specific scenarios.

Read more »

Labels:

Sunday, 22 September 2024

Understanding Python’s ValueError: attempted relative import beyond top-level package Error

If you’ve worked with Python for a while, especially on larger projects, you might have encountered the dreaded “ValueError: attempted relative import beyond top-level package” error. This issue can be frustrating and confusing, particularly when dealing with relative imports within packages. Let’s dive into why this happens, common scenarios, and solutions you can use to avoid this error.

Read more »

Labels:

Wednesday, 18 September 2024

Secure Hashing and Salt for PHP Passwords: A Modern Approach


When considering password protection in PHP, it’s crucial to employ up-to-date techniques to ensure both security and performance. The original question was raised in 2008, but PHP has evolved since then, providing built-in functions for safe password handling. Here’s a modern take on the subject, utilizing best practices and avoiding deprecated approaches like MD5 or SHA1.

Read more »

Labels:

Tuesday, 17 September 2024

Hidden Features of Perl


Perl is known for its flexibility and numerous unique features, many of which might be considered “hidden” because they are not commonly used or are esoteric. Here are some of the most intriguing hidden features of Perl that are surprisingly useful in real-world applications:

Read more »

Labels:

Sunday, 15 September 2024

How to Run a Local Shell Script on a Remote Machine via SSH

If you want to run a local shell script on a remote machine using SSH, there are several ways to achieve this depending on whether you’re using a Windows or Unix-based system. Here’s a guide for both environments:

For Unix-Based Systems (Linux/macOS)

You can use ssh to execute a local shell script on a remote machine without needing to copy the script over. The command works by sending the script via standard input to be executed on the remote server:

ssh user@MachineB 'bash -s' < local_script.sh
Read more »

Labels:

Saturday, 14 September 2024

How to Specify Multiple Return Types Using Type Hints in Python

How to Specify Multiple Return Types Using Type Hints in Python

In Python, type hints improve code readability and help developers understand what kind of values are expected for function arguments and returns. But sometimes, a function can return multiple types, which raises the question: how can we specify these multiple return types?

In this blog post, I’ll walk you through different ways to handle multiple return types using Python type hints, covering various versions of Python and how to use unions, tuples, and type-checking libraries.

Read more »

Labels:

Friday, 13 September 2024

Which Equals Operator (== vs ===) Should Be Used in JavaScript Comparisons?

When working with JavaScript, you have two main options for checking equality between values: the double equals (==) and the triple equals (===) operators. Both are used to compare values, but they function differently, particularly regarding type conversion.

Understanding == (Equality Operator)

The == operator, also known as the equality operator, compares two values for equality after performing type conversion. This means that if the values being compared are of different types, JavaScript will attempt to convert them to a common type before making the comparison.

Read more »

Labels:

Monday, 9 September 2024

How to Run a Local Shell Script on a Remote Machine Using SSH

Running a local shell script on a remote machine using SSH is a common task in system administration and software development. This process allows you to execute a script on a remote server without manually copying it over. Here’s how you can do it depending on your local machine’s operating system:

For Unix-based Systems (Linux/macOS):

If your local machine is Unix-based, you can use the ssh command with input redirection to execute a local script on a remote machine. Here’s the basic syntax:

Read more »

Labels:

Friday, 6 September 2024

Getting the Current Date and Time in Python: A Practical Guide

Python offers a variety of ways to retrieve the current date and time, breaking it down into individual components such as year, month, day, hour, and minute. This functionality is crucial in many applications, ranging from logging events to timestamping data entries. In this post, we will explore different methods to achieve this, highlighting a few alternatives beyond the commonly used datetime module.

Method 1: Using time and struct_time

The time module provides a straightforward way to access the current time and break it down into its components. Here’s how you can use time.localtime():

Read more »

Labels:

Thursday, 5 September 2024

Removing Duplicates from an Array in Perl

When working with arrays in Perl, you might encounter situations where you need to remove duplicate elements. Perl, with its versatile data structures, offers several ways to accomplish this task. Let’s explore different approaches to remove duplicates from an array, demonstrating the flexibility and power of Perl.

Approach 1: Using a Hash to Filter Unique Elements

One of the simplest and most efficient ways to remove duplicates from an array is by using a hash. Hashes in Perl inherently prevent duplicate keys, which makes them ideal for this task.

Read more »

Labels:

Tuesday, 3 September 2024

How to Call Shell Commands from Ruby: A Comprehensive Guide

In Ruby, interacting with the system shell is straightforward, allowing you to execute commands, capture output, and handle errors efficiently. Whether you’re automating tasks, running scripts, or managing system operations, Ruby offers multiple ways to interact with the shell. In this guide, we’ll explore various methods to call shell commands from within a Ruby program and how to handle their outputs and errors.

1. Using Backticks (`command`)

The simplest way to execute a shell command in Ruby is by using backticks. This method runs the command in a subshell and returns its standard output as a string.

output = `echo 'Hello, World!'`
puts output
Read more »

Labels: