Skip to content

Instantly share code, notes, and snippets.

View harishkannarao's full-sized avatar

Harish Kannarao harishkannarao

View GitHub Profile
Quitting
:x Exit, saving changes
:q Exit as long as there have been no changes
:wq Write and Quit. Saves any changes made to the file
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes
Inserting Text

Less Cheatsheet

less {filename}

Navigation
SPACE forward one window
b backward one window
d forward half window
@harishkannarao
harishkannarao / build-alpine-jre-docker.sh
Last active September 11, 2024 12:10
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
Last active June 17, 2025 08:58
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
  • -vv -> Higher Verbose output
  • -vvv -> Even Higher Verbose output