Shell script to pass multiple arguments with options
Shell script to pass multiple arguments with options #!/bin/bash if (($# == 0)); then echo "Please pass argumensts -t <task1><task2>... -d <date>.." exit 2 fi while getopts ":t:d:" opt; do case $opt in t) echo "-t was triggered, Parameter: $OPTARG" >&2 TASKS=$OPTARG ;; d) echo "-d was triggered, Parameter: $OPTARG"…
How to Run a Shell Script in Infinite Loop
You can run a shell script in infinite loop by using while loop. #!/bin/bash while true do echo "Press CTRL+C to stop the script execution" # Enter your desired command in this block. done You can also do this using below inline command while true; do echo 'Press CTRL+C to stop…
Cut and paste in Vim editor
Here is the simple step by step guideline to cut and paste in Vim editor. Press esc if vim editor in insert mode Position the cursor where you want to start cutting. Press v to select the characters and move the cursor at end of character you want to cut.…
How to Run a Linux Script During Reboot or Startup
I come accros one use case where I need to run a Linux script every time on system reboot or startup. Create a new script in / Lets consider the below example script. vi / #! /bin/sh ### BEGIN INIT INFO # Provides: myupdate ### END INIT INFO PATH=/sbin:/bin:/usr/sbin:/usr/bin case "$1"…
Beginner Shell Scripting Reference
I am sharing beginner shell script reference. Local Variable in Shell Script We can create local variables in shell script by using the keyword local. #!/bin/bash HELLO=Hello function helloWorld { local HELLO=World echo $HELLO } echo $HELLO helloWorld echo $HELLO Conditional Statement 1) if conditional statement The base for the…
Python Script to Access Team Foundation Server (TFS) Rest API
I was working in a project where I have to access Team Foundation Server (TFS) Rest API in Python script. It was little bit tricky job to access TFS REST API from python 2 . Specially for beginners like me :) . I was facing issue to authenticate TFS REST…
Deleting Lines Containing Specific string from multiple files
Deleting Lines Containing Specific string from multiple files recursively Syntax: grep -Rl "STRING" /directory/path/ | xargs sed -i "/STRING/d" Example: Below example delete lines containing @license tag from src/main/php directory all files recursively grep -Rl "@license" src/main/php | xargs sed -i "/@license/d" Option Description: -R or --dereference-recursive For each directory…
Get the List of All Files and Directory Recursively in Linux
List files and directories inside the specified path Some time we want to check list all files in a directory recursively. But its hard to navigate each and every directory to check all the files. Here are the simple commands which will help you to check all the files and…
Get File MIME Type in Python
Get file MIME type in Python You can get mime type in python by using python-magic library. You can install python-magic using PIP. $ pip install python-magic To find mime type, import magic library and pass file path into () >>> import magic >>> ("path/", mime=True) 'application/pdf' If you face…
Rename all Files in the Directory
Here is the simple shell script to rename all files in the folder with increasing number. In below example I am going to rename all jpg image with increasing number. #!/bin/bash a=1 for i in *.jpg; do new=$(printf "%" "$a") mv -- "$i" "$new" let a=a+1 done Output: