Skip to content

Instantly share code, notes, and snippets.

View harishkannarao's full-sized avatar

Harish Kannarao harishkannarao

View GitHub Profile
@harishkannarao
harishkannarao / build-alpine-jre-docker.sh
Last active March 26, 2024 14:38
Build an alpine image with jq, curl and jre 11 from scratch without pulling from hub.docker.com
#!/bin/sh
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Prints the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
set -x
# Set default values
ORGANISATION_NAME="${ORGANISATION_NAME:-com.example}"
#!/bin/bash
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
# set -x
# Source the functions from an url
source <(curl -s "https://gist.githubusercontent.com/harishkannarao/58d317f2743c842b5323165a492db81c/raw/75ee691d4d4568c59443a070c6e5d192d717b6bc/reusable_functions.sh")
#!/bin/bash
function add()
{
echo "$1 + $2" | bc -l
}
function subtract()
{
echo "$1 - $2" | bc -l

Bash Script loops and conditional statement example

The following blog post shows example of bash script with for loop, while loop, if elif else conditional statements.

Important aspects covered in the sample script:

  • Conditional while loop
  • While loop to iterate over lines in a file
  • Infinite while loop with break and continue
  • Infinite while loop with sleep
  • Iterate over arguments passed as input using for loop
  • For loop with counter
#!/bin/bash
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
# set -x
echo "Example of numerical comparison with if elif else"
for (( c=-5; c<=5; c=$c+1 ))
#!/bin/bash
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
# set -x
while [[ $# -gt 0 ]]
do
@harishkannarao
harishkannarao / youtube-dl.md
Last active March 11, 2024 10:23
youtube-dl

youtube-dl

youtube-dl is an opensource command line tool to download video or audio from online video streaming services.

Videos downloaded in mkv or webm extensions can be played by VLC Media player in all major devices and operating systems including iPhone, Android devices.

Tool website: https://youtube-dl.org/

This gist shows the example commands to use the tool and doesn't support or encourage piracy or violation of copyrights of the online streaming service or the author of the content

Installing youtube-dl:

@harishkannarao
harishkannarao / practical_rsync_command_and_options.md
Created November 15, 2018 15:18
Practical rsync command and options

Copy new files from source to target and skip files if it already exist in target

rsync --dry-run -avzurh --stats --delete --progress /tmp/source/ /tmp/target/

Explanation of flags:

  • -a -> Archive mode, preserve the file system properties
  • -v -> Verbose output
  • -z -> Use compression mode for data transfer, speeds up for transfer over network
  • -h -> Output in human readable format

Bash Script Example

The following blog post shows a sample bash script covering some inportant aspects in writing a bash script.

Important aspects covered in this script:

  • Validate if input arguments are passed
  • Parse positional arguments
  • Handle parameter values with white space
  • Validating the arguments (using regex)
  • While loop to iterate through varargs
  • Usage of unit functions
@harishkannarao
harishkannarao / calculate.sh
Last active August 11, 2023 08:53
Basic bash script example to calculate addition, subtraction, multiplication and division of two numbers
#!/bin/bash
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
# set -x
# Function to print usage of the script with different options
function print_usage()