How to pass command line arguments to shell script

getopts allows you to accept command line arguments for the bash script, below is demo script which does following this, You can run the script as, bash scriptname.sh -h => it will display the help bash scriptname.sh -c =>  script displays “Good Morning / Good after noon/ Good Evening / Good NIght” if the time … Read more

Wait for command execution till Internet is available using fping

Do you want to wait for an internet availability before proceeding further such as send an email from command line only when internet is available.. In those cases, we have to go into infinite loop of checking whether we are really connected to network. Ubuntu has a utility called “fping” we comes handy for such … Read more

How to check if Internet is available using bash script

Do you want to wait for an internet availability before proceeding further such as send an email from command line only when internet is available.. In those cases, we have to go into infinite loop of checking whether we are really connected to network. Ubuntu has a utility called “fping” we comes handy for such … Read more

Linux Initscript Example to start and stop process during booting

The below shell script uses start-stop-daemon from “/sbin/start-stop-daemon” to execute the program during booting so that the process is started boottime. In the below example, DAEMON=$PWD/helloworld.bin shows that our program is helloworld.bin and is located in current directory from where the script is executed. The above script can be executed as “bash start_daemon.sh start” to … Read more

How to clear Terminal / Shell history in Ubuntu / Linux ?

When you type something on linux shell, by default Linux save the commands what you type as history to “~/.bash_history” or /home/username/.bash_history file so that you can easily check what you have typed previously using “history” command. $ history | tail 1998 sudo apt-get install mdns 1999 sudo apt-get install mdns-scan 2000 sudo mdns-scan As … Read more

Multiple line comment in bash / shell script

In bash script, if we want to comment a single line, we just need to add # at the start of the line, like below Now, but in certain scenarios we need to put multiple line comments, like some description of what the script is for, OR just need to disable some section of code … Read more

Shell script to check if android application is installed, find an apk path and uninstall if its already installed or install new apk if application is not installed

Following shell script does, check if android application is installed find an apk path uninstall if its already installed install new apk if application is not installed [bash] #!/bin/bash ADBPATH=/usr/bin/adb PACKAGE_NAME=my.app.helloworld LOCAL_DESKTOP_APK_PATH=$PWD/PACKAGE_NAME.apk IS_PACKAGE_EXISTS=`$ADBPATH shell pm list packages $PACKAGE_NAME` echo "pm list package returned: $IS_PACKAGE_EXISTS " if test -n "$IS_PACKAGE_EXISTS" then echo "Application $PACKAGE_NAME exists" CMD=`$ADBPATH … Read more

Shell script to check if Android device is connected over USB

Android devices has a daemon adbd which helps it to get connected over USB using adb command on linux host. If you are doing some kind of automation such as taking logs or installing some applications from ubuntu command line, the very first thing we need to make sure if our device is connected and … Read more

Shell script for using modulo operator, finding if number is dividable and print remainder.

Following shell script helps to understand how to use modulo operator in bash and identify if number is completely dividable by another number and print the remainder.This script accepts two arguments on the commands line, first number is Divident and second number is Divisor. $ vim understanding_modulo_operator.sh [bash] #!/bin/bash NUMBER=$1 DIVIDE_BY=$2 remainder=$((NUMBER%DIVIDE_BY)) echo "Module operation … Read more