#!/bin/bash
echo "Enter the word that you'd like to add to the dictionary and then press [Enter]."
read WORD
PATH_DICTIONARY="/Users/adil/Downloads/MyDictionary.txt"
COUNT=`grep -c "^$WORD$" $PATH_DICTIONARY`
if [ $COUNT -gt 0 ]
then echo "Word is already contained in the dictionary."; exit;
fi
echo "\$COUNT = $COUNT"
echo "Adding $WORD to the dictionary..."
echo "$WORD" >> $PATH_DICTIONARY
echo "Done"
Thursday 31 December 2015
Bash - basic script for requesting input from the user
As mentioned in this post, my know-how of Bash scripting is pretty nil so I've been skimming through this guide the last few days to get to grips with the basic concepts. I've put together this basic script as a reminder to myself for future reference of how to request and parse input from the user:
Bash - basic script for checking positional params
My know-how of Bash scripting is pretty nil so I've been skimming through this guide the last few days to get to grips with the basic concepts. I've put together this basic script as a reminder to myself for future reference of how to pass in and parse positional params in a script:
#!/bin/bash
if [ $# -lt 2 ];
then echo "Must pass in at least two params."; exit 1;
fi
#set -x
GREETING="Hello"
echo "$GREETING $USER"
#set +x
echo ""
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
echo ""
[ -f $0 ] && (echo "File $0 exists.";) || (echo "File $0 doesn't exist.";)
echo ""
if [ $1 -eq $2 ]; then
echo "The value of \$1 is equal to the value of \$2.";
elif [ $1 -lt $2 ]; then
echo "The value of \$1 is less than the value of \$2.";
else echo "The value of \$1 is greater than the value of \$2.";
fi
echo ""
AREA=$[$1 * $2]
echo "\$AREA = \$1 * \$2 = $AREA"
case $AREA in
([-][0-9]*)
echo "The value of \$AREA is negative.";;
([0-9])
echo "The value of \$AREA is < 10.";;
([1-9][0-9])
echo "The value of \$AREA is < 100.";;
*)
echo "The value of \$AREA is >= 100.";;
esac
echo ""
echo "Goodbye $USER"
If I run this script as follows: bash MyScript.sh
, then I'll see this output:
Must pass in at least two params.
If, however, I run this script as follows: bash MyScript.sh 5 4
, then I'll see this output:
Hello adil
$0 = MyScript.sh
$1 = 5
$2 = 4
File MyScript.sh exists.
The value of $1 is greater than the value of $2.
$AREA = $1 * $2 = 20
The value of $AREA is < 100.
Goodbye adil
That's it. That's a basic script which demonstrates how to perform some conditional checks on positional params passed into a script.
Saturday 12 December 2015
Proguard: rules for building a library
So you have a library defined and you want to run proguard on it when building it to remove unused code and to obfuscate code. Of course you do not want to remove or obfuscate classes/interfaces/enums/methods/fields that are public since these will be called on by the projects that depend on your library.
Here are the rules that you need in your proguard config file:
-keepparameternames
-keep public interface com.mycompany.mylibrary.** {
<methods>;
}
-keep public class com.mycompany.mylibrary.** {
public <init>(...);
public <fields>;
public static <fields>;
public <methods>;
public static <methods>;
}
-keep public enum com.mycompany.mylibrary.** {
public <init>(...);
public <fields>;
public static <fields>;
public <methods>;
public static <methods>;
}
The above is not an exhaustive list of all the rules which you might need but it's a starting point and should suffice for most libraries. If you need to add some more rules you'll need to get a grasp of the proguard rules syntax here: http://proguard.sourceforge.net/manual/usage.html
Wednesday 2 December 2015
Android: start Service of another app
So you want to start a Service which belongs to another app? There's two ways to do this depending on how the Service has been defined.
Let's assume first that the Service is defined in the manifest of the first app with the
Let's assume first that the Service is defined in the manifest of the first app with the
exported
attribute as follows:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thinkincode.someapp" >
<application>
<service
android:name=".SomeService"
android:exported="true" />
</application>
</manifest>
In this case the Service can be started from the second app as follows:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.thinkincode.someapp",
"com.thinkincode.someapp.SomeService"));
startService(intent);
The better way of declaring a Service to be used by other apps is of course to use intent filters. So, going back to the manifest of the first app, this is how we'd define the Service:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thinkincode.someapp" >
<application>
<service
android:name=".SomeService"
android:exported="true">
<intent-filter>
<action android:name="StartSomeService" />
</intent-filter>
</service>
</application>
</manifest>
And this is how we'd now start the Service from the second app:
Intent intent = new Intent();
intent.setAction("StartSomeService");
startService(intent);
That's it! You should now be able to (1) define Services which can be used by other apps and (2) call on Services defined by other apps.
Subscribe to:
Posts (Atom)