Download apk file using Download Manager on Android

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
//get destination to update file and set Uri
   //TODO: First I wanted to store my update .apk file on internal storage for my app but apparently android does not allow you to open and install
   //aplication with existing package from there. So for me, alternative solution is Download directory in external storage. If there is better
   //solution, please inform us in comment
   String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
   String fileName = "AppName.apk";
   destination += fileName;
   final Uri uri = Uri.parse("file://" + destination);

   //Delete update file if exists
   File file = new File(destination);
   if (file.exists())
   //file.delete() - test this, I think sometimes it doesnt work
       file.delete();

   //get url of app on server
   String url = Main.this.getString(R.string.update_app_url);

   //set downloadmanager
   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
   request.setDescription(Main.this.getString(R.string.notification_description));
   request.setTitle(Main.this.getString(R.string.app_name));

   //set destination
   request.setDestinationUri(uri);

   // get download service and enqueue file
   final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
   final long downloadId = manager.enqueue(request);

   //set BroadcastReceiver to install app when .apk is downloaded
   BroadcastReceiver onComplete = new BroadcastReceiver() {
       public void onReceive(Context ctxt, Intent intent) {
           Intent install = new Intent(Intent.ACTION_VIEW);
           install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           install.setDataAndType(uri,
                   manager.getMimeTypeForDownloadedFile(downloadId));
           startActivity(install);

           unregisterReceiver(this);
           finish();
       }
   };
   //register receiver for when .apk download is compete
   registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

References
https://stackoverflow.com/questions/4967669/android-install-apk-programmatically
https://stackoverflow.com/questions/39147608/android-install-apk-with-intent-view-action-not-working-with-file-provider/40131196#40131196

Execute a command whenever a file changes

You can do this with inotifywait utility from inotify-tools package

while true ; do
  inotifywait -e delete_self "/tmp/fileToMonitor.txt" \
    && cp new_file "/tmp/fileToMonitor.txt"
done
[ "$UID" -eq 0 ] || exec sudo "$0" "$@"

while true ; do
  inotifywait -e modify "/etc/resolv.conf" && ./dns.sh
done

References
https://superuser.com/questions/939600/how-to-get-notified-when-a-specific-file-is-deleted-in-linux
https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes
http://man7.org/linux/man-pages/man1/inotifywait.1.html#EVENTS

Command Substitution in bash

Text between backticks is executed and replaced by the output of the command (minus the trailing newline characters, and beware that shell behaviors vary when there are NUL characters in the output). That is called command substitution because it is substituted with the output of the command.

A=`cat /etc/p2ass2wd2 | head -n1`
echo "$A"
A=$(cat /etc/p2ass2wd2 | head -n1)
echo "$A"

References
https://unix.stackexchange.com/questions/48392/understanding-backtick
https://unix.stackexchange.com/questions/147420/what-is-in-a-command

Get IP Address using bash

Displaying private IP addresses

ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'

address=$(ip addr show wlp3s0 | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')

Displaying the public IP address

If you want to know the public IP address of a Linux server, you can send an HTTP request to one of the following web servers.

  • http://ifconfig.me
  • http://www.icanhazip.com
  • http://ipecho.net/plain
  • http://indent.me
  • http://bot.whatismyipaddress.com
  • https://diagnostic.opendns.com/myip
  • http://checkip.amazonaws.com
curl http://checkip.amazonaws.com
wget -qO- http://checkip.amazonaws.com

References
https://unix.stackexchange.com/questions/119269/how-to-get-ip-address-using-shell-script
https://www.linuxtrainingacademy.com/determine-public-ip-address-command-line-curl/