Author: Java2blog
16 AugustGet Mapped Drives for All Users in PowerShell
Using net use Command Use the net use command to get mapped drives for all users in PowerShell. net use New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------ OK K: \\DESKTOP-NKK10JM\JavaProject Microsoft Windows Network OK Z: \\DESKTOP-NKK10JM\Share Microsoft Windows Network The command completed successfully. In the above example, we used the net use […]
02 AugustCheck if Two Files are the Same in Bash
In this entire article, we will use the following content of three different text files to practice the provided solutions. This is Line number 1. This is Line Number 2. It is Line number 3. I am Line 4. This is Line number 1. This is Line Number 2. It is Line number 3. I […]
02 AugustGet Last Line of Output in Bash
1. Overview In this article, we will see how to get last line of output in Bash using tail, awk, sed, mapfile, and head commands with various options. 2. Introduction to Problem Statement We will use ls -l to list directories and files in long format and get the last line of output using various […]
02 AugustCheck If Host Is Reachable in Bash
Using ping Command Use the ping command in bash to check if the host is reachable or not. #!/bin/bash host="example.com" ping -c 1 $host > /dev/null if [ $? -eq 0 ]; then echo "Host is reachable." else echo "Host is not reachable." fi Host is reachable. In this bash script, the ping command is […]
29 JulySet Output of Command to Variable in Bash
Using Substitution Syntax Use substitution syntax to capture the output of a single command into a variable in Bash. date_variable=$(date) echo "Today's date: $date_variable" Today's date: Thu Jun 8 04:08:47 UTC 2023 We used the date command to get the current date and time. The date command was enclosed within the substitution syntax represented by […]
28 JulyConvert Array to Comma Separated String in Bash
Using printf Statement Use the printf statement to convert the array to a comma-separated string in Bash. array=("Jan" "Feb" "Mar" "Apr") printf -v joined '%s,' "${array[@]}" echo "${joined%,}" Jan,Feb,Mar,Apr We initialized an array containing string elements. Then, we used the printf statement to convert the specified array to a comma-separated string. How? The printf statement […]
25 JulyReturn Array from Function in Bash
Bash cannot return values, whether a single value or an array, but it can return a status (the same as other programs). However, there are multiple turnarounds that we can use to return an array from a function. Let’s explore them below. Using nameref Feature To return the entire array from a function in Bash: […]
25 JulyMerge Arrays in PowerShell
Using + Operator Use + operator to merge arrays in PowerShell. $array1 = @("Welcome", "to", "Java2Blog!") $array2 = @("Let's", "learn", "together.") $merged_array = $array1 + $array2 echo $merged_array Welcome to Java2Blog! Let's learn together. We used the sub-expression operator represented by @() to create two separate arrays named $array1 and $array2 (you can also create […]
25 JulyFormat Date to yyyymmdd in Bash
Using date Command Use the date command to format the date to yyyymmdd in Bash. formatted_date=$(date +'%Y%m%d') echo $formatted_date 20230531 In the above example, the bash date command is used to get the current date with the format specifier '%Y%m%d' to specify the date format. In Bash, the date command is used to retrieve the […]