The below shell scripts are typical example codes which helps us to understand certain frequently used codes.
1. How to get the absolute or full path while calling another shell script from one shell script.
The above code snippet calls another shell script by name ifix_config.sh. which is available in the same directory of current script.
Abosolute path will be used with the readlink -f option there. uninstall isa parameter passed to it.
2. How to check how many parameters are passed in to a shell script..
In the above example code if at least one parameter is not passed it throws a message and exits.
3. How to check if a variable is set or not . How to check if a viariale has empty string ?
In the above example snippet we are setting some value for LOCALE_CODE if in case it is not set for some reason -z option with if condition returns true
so in above example if it( if [ -z $LOCALE_CODE ] ) is true it will set it to en_US
4. How to get the parent directory path of a file or directory ?
When dirname is given a pathname, it will delete any suffix beginning with the last slash ('/') character and return the result.
For example:
$ dirname /usr/home/carpetsmoker/dirname.wiki
/usr/home/carpetsmoker
5. How to get the only filename when absolute path is given ?
Example
6. How to read a properties file in shell script ?
Example :
$PropertiesFile contents are like below ..
-------------------------------------------
#Fri, 01 Mar 2013 02:59:37 -0500
product.was.user=smadmin
product.jdbc.lib.path=/opt/IBM/JazzSM/lib/db2
product.data.location=/var/ibm/InstallationManager
product.install.location=/opt/IBM/InstallationManager/eclipse
-------------------------------------------
The while process is to Scan through the file to get an individual property
This works well if you just want to read a specific property in a Java-style properties file. Here's what it does:
* Remove comments (lines that start with '#') >> sed '/^\#/d' myprops.properties
* Grep for the property >> grep 'someproperty'
* Get the last value using >> tail -n 1
* Strip off the property name and the '=' (using {{cut -d "=" -f2-}) to handle values with '=' in them).
sed '/^\#/d' myprops.properties | grep 'someproperty' | tail -n 1 | cut -d "=" -f2-
It can also be handy to strip off the leading and trailing blanks with this sed expression:
s/^[[:space:]]*//;s/[[:space:]]*$//
Which makes the whole thing look like this:
sed '/^\#/d' myprops.properties | grep 'someproperty' | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
Reference
7. How to check file existance before running it ?
Having quotes for the file path eliminates issues arise out of spaces in the path.
8. How to read input values through shell script ?
echo -n "${wasUserNamePrompt}: " >> it helps to prompt user with particular string before commandline prompt while running shell script, in order to resolve wasUserNamePrompt string we need to run its correspinding properties file before this call .
For example config_messages.sh has text like below , and when it is run before the above command it will resolve this value.
#!/bin/sh
#NLS_MESSAGEFORMAT_NONE
#NLS_ENCODING=UTF-8
wasUserNamePrompt="Enter the Websphere Application Server user name:"
wasPasswordPrompt="Enter the Websphere Application Server password:"
1. How to get the absolute or full path while calling another shell script from one shell script.
#!/bin/sh
source $(readlink -f "ifix_config.sh") uninstall
The above code snippet calls another shell script by name ifix_config.sh. which is available in the same directory of current script.
Abosolute path will be used with the readlink -f option there. uninstall isa parameter passed to it.
2. How to check how many parameters are passed in to a shell script..
if [ $# != "1" ]; then$# represents total number of parameters passed to a shell script when called.
echo "Usage: ifix_config.sh install or ifix_config.sh uninstall"
exit
else
....
....
In the above example code if at least one parameter is not passed it throws a message and exits.
3. How to check if a variable is set or not . How to check if a viariale has empty string ?
LOCALE_CODE=$LANG
if [ -z $LOCALE_CODE ];
then JAZZSM_SYSTEM_LOCALE="en_US"
fi
In the above example snippet we are setting some value for LOCALE_CODE if in case it is not set for some reason -z option with if condition returns true
so in above example if it( if [ -z $LOCALE_CODE ] ) is true it will set it to en_US
4. How to get the parent directory path of a file or directory ?
PresentDir=`dirname $PWD`
When dirname is given a pathname, it will delete any suffix beginning with the last slash ('/') character and return the result.
For example:
$ dirname /usr/home/carpetsmoker/dirname.wiki
/usr/home/carpetsmoker
5. How to get the only filename when absolute path is given ?
Example
$ basename /home/jsmith/base.wiki
base.wiki
$ basename /home/jsmith/base.wiki .wiki
base
IFIX_NO=`basename $PWD`
0001
6. How to read a properties file in shell script ?
# Reading settings.properties to find IM Install location
IM_HOME=`sed '/^\#/d' $PropertiesFile | grep 'product.install.location' | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'`
echo IM Install Location is : $IM_HOME
Example :
$PropertiesFile contents are like below ..
-------------------------------------------
#Fri, 01 Mar 2013 02:59:37 -0500
product.was.user=smadmin
product.jdbc.lib.path=/opt/IBM/JazzSM/lib/db2
product.data.location=/var/ibm/InstallationManager
product.install.location=/opt/IBM/InstallationManager/eclipse
-------------------------------------------
The while process is to Scan through the file to get an individual property
This works well if you just want to read a specific property in a Java-style properties file. Here's what it does:
* Remove comments (lines that start with '#') >> sed '/^\#/d' myprops.properties
* Grep for the property >> grep 'someproperty'
* Get the last value using >> tail -n 1
* Strip off the property name and the '=' (using {{cut -d "=" -f2-}) to handle values with '=' in them).
sed '/^\#/d' myprops.properties | grep 'someproperty' | tail -n 1 | cut -d "=" -f2-
It can also be handy to strip off the leading and trailing blanks with this sed expression:
s/^[[:space:]]*//;s/[[:space:]]*$//
Which makes the whole thing look like this:
sed '/^\#/d' myprops.properties | grep 'someproperty' | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
Reference
7. How to check file existance before running it ?
[ -f "$SCRIPT_LOCATION/myshellscript.sh" ] && . "$SCRIPT_LOCATION/myshellscript.sh"
Having quotes for the file path eliminates issues arise out of spaces in the path.
8. How to read input values through shell script ?
# Ask for WAS credentials
if [ -z "${JAZZSM_WAS_USERNAME}" ]
then
echo -n "${wasUserNamePrompt}: "
read JAZZSM_WAS_USERNAME
fi
echo -n "${wasUserNamePrompt}: " >> it helps to prompt user with particular string before commandline prompt while running shell script, in order to resolve wasUserNamePrompt string we need to run its correspinding properties file before this call .
For example config_messages.sh has text like below , and when it is run before the above command it will resolve this value.
#!/bin/sh
#NLS_MESSAGEFORMAT_NONE
#NLS_ENCODING=UTF-8
wasUserNamePrompt="Enter the Websphere Application Server user name:"
wasPasswordPrompt="Enter the Websphere Application Server password:"
0 comments to "Shell Script Tips -1 "
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
-
Following are some of interesting blogs I found till now ...please comment to add your blog here. Blogs in English : http://nitawriter.word...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs
Post a Comment
Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")