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 [bash] #!/bin/bash # This is a comment echo "This is printed on terminal" [/bash] Now, but in certain scenarios we need to put multiple line comments, like some description of … 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

Shell script for switching wifi channel using hopping

$ vim wifi_channel_hop.sh [bash] #!/bin/bash IFACE=wlan0 IEEE80211bg="1 2 3 4 5 6 7 8 9 10 11" IEEE80211bg_intl="$IEEE80211b 12 13 14" IEEE80211a="36 40 44 48 52 56 60 64 149 153 157 161" IEEE80211bga="$IEEE80211bg $IEEE80211a" IEEE80211bga_intl="$IEEE80211bg_intl $IEEE80211a" while true ; do for CHAN in $IEEE80211bg ; do echo "Switching to channel $CHAN" sudo iwconfig $IFACE … Read more

Shell script to check and mount a partition in Ubuntu

In the following script we check whether the current device is mounted or not by reading information from proc file /proc/mounts, if the device is not mounted, we use the “mount” command with sudo / superuser permission to mount the device to mount directory as mentioned in the beginning of script. $ vim script_to_check_and_mount_parition.bash [bash] … Read more

Shell script to find a file and delete from a directory

Below shell script will find and delete certain file or multiple files from the directory you want. We have used current directory from where script is run, you need to change DIR_TO_SEARCH and FILE_TO_SEARCH_N_DEL from below script. $ vim script_to_find_and_delete_file.sh [bash] #!/bin/bash DIR_TO_SEARCH=$PWD FILE_TO_SEARCH_N_DEL=testing.txt for file in $(find $DIR_TO_SEARCH -name $FILE_TO_SEARCH_N_DEL) do echo "deleting $file" … Read more