Wednesday, September 5, 2012

Shell script to backup modified files in a directory

Want to backup your files before a day's work? This script does that!

#!/bin/bash
since=${1-1}
find  -iname "*.js" -mtime -$since > temp
if [[ -s temp ]] ; then
  date=`date +%D`
  ctime=`date +%H%M%S`
  mdate=$(echo "$date" | tr '/' '_')
  date=$mdate"_"$ctime
  mkdir /home/nanda/backups/$date
  for i in `cat temp` ; do
  i=`echo ${i:2}`
  cp $i /home/nanda/backups/$date/
  done
else
  echo "No files modified since $since day(s)"
fi ;
rm temp

Save the script as backup.sh and give executable permissions to it by typing chmod +755 backup.sh as root in your terminal.

Usage: ./backup.sh {integer number of days}

The parameter to be passed is an integer which specifies to backup files that have been modified in the last so many days. Default value is one day.

This particular script is coded for javascript .js files as seen in line 2. You can modify that for different extension types.

A backup directory with writeable permissions has to be created beforehand. In this example it is /home/nanda/backups. When the script is run, a new directory is created and is named with the time and date when it was created.

Happy Backups!

Android For Dorks - Part 2

Activity and Layout xml


Last post we looked at AndroidManifest.xml here. We talked about activities and an entry point to it. Here we will look at that activity.

 <activity android:name=".SendActivity"
   android:label="@string/app_name">
   <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
 </activity>
Here, SendActivity is the MAIN activity. A SendActivity.java must be created in the "src" directory of your code. Note that the file name and the activity name should match. A code snippet of it looks like this:

public class SendActivity extends Activity {

    /* variables can be defined here  */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }
}

Here we see that SendActivity is a class that inherits from the Activity. We also see that there is the onCreate function which is the entry function for the SendActivity class. Inside it, the view for application is mentioned: setContentView(R.layout.main);
So the screen will display the contents of the main.xml file which will be found under the layout folder in the res folder.

The main.xml file looks like:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <LinearLayout 
     android:orientation="horizontal"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">
     <EditText
        android:id="@+id/msgTextField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
         android:hint="Enter Message"
        />
     <Button
        android:text="Send"
        android:id="@+id/sendButton"
        android:layout_height="wrap_content" 
     android:layout_width="wrap_content"
        android:onClick="send"
        android:lines="1"
        />
    </LinearLayout> 
 <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/webView"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1.0"
 />
</LinearLayout>

Your app can have a LinearLayout or a RelativeLayout on which suits you. Here the screen will display an EditText field for the user to input data, a Send Button and a WebView below it. A WebView is used to display a webpage within the app. A screenshot on the emulator looks like this.


Here ends the short tutorial for beginners!

Monday, September 3, 2012

Android For Dorks - Part 1

AndroidManifest.xml

Before we start,

manifest(noun) definition:- A document giving the details of a ship and its cargo, passengers, and crew for the use of customs officers.

Here for android, the ship is your application. This file is like a blueprint for your application. Lists out its contents, parameters and requirements. It is found in the root directory of your application project. So what does this file do? Lets look at an example:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="my.tracker.app"
      android:versionCode="1"
      android:versionName="1.0">
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-sdk android:minSdkVersion="8" />
  <application android:icon="@drawable/launcherIcon" android:label="@string/app_name">
  <activity android:name=".SendActivity"
   android:label="@string/app_name">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
   </activity>
  </application>  
</manifest> 

package= refers to your application package. The application may ask for permissions from the users, say to access the internet or the GPS, which you see in the  uses permission tag.
The  minSdkVersion refers to the minimum android version build your application is designed for.

Then we come to the application tag. Your android application can contain "Activities". An activity is something that goes on your screen. Naturally, an application can contain more than one activity. You might say that an activity is a function in a broad sense. Like in C/C++ which contain an entry point to the program through a main function, Your android application too should contain a "Main" activity. The sample code above says that there is an activity of name "SendActivity" and it is mentioned as the "MAIN" by the use of the intent-filter tag with "android.intent.action.MAIN". The "android.intent.category.LAUNCHER" value in the category tag informs that this activity has the launcher icon which is referenced by android:icon="@drawable/icon", where @drawable is the folder in which the launcherIcon image is to be found.

Coming soon, Part 2 in the Android for Dorks series.