Friday, May 23, 2008

bash one liners

Kill all processes form a user:
pkill -u [USER_NAME]

Automatic start by linux startup check
chkconfig -l [APP_NAME]

Automatic start by linux startup add application, in runlevel 3,5
chkconfig -add [APP_NAME] -level 3,5


Which linux OS:
cat /etc/issue

To remove the ^M characters at the end of all lines in vi, use:
:1,$s/^V^M//g

Want to know which file contains specific words in a directory tree:
find ./ -type f -name '*' -print0  ¦ xargs -0 grep -li -e "word1" -e "word2" -e "word3"

When you want an user typing a password in a shell script you do not want to see this password on the screen. You can use read -s to do this:
echo "Please type the administrator password:
read -s PASSWORD

Get the file name from an string with a whole path this can be done with the basename function.
For example, the path /oracle/product/10.2.0/bin/emca and you only want emca you can do this
EMCA=`basename /oracle/product/10.2.0/bin/emca`

And if you want the directory this is also simple:
ORACLE_BIN=`dirname /oracle/product/10.2.0/bin/emca`

If you want to query some value from an Oracle database and put this value in a variable in the shell script. Make a function which does the login on sqlplus and the query. This function can be called and put in an variable:
execsql_silent()
{
$ORACLE_HOME/bin/sqlplus -s $ORACLE_CREDENTIALS <<SQL
whenever sqlerror exit
set heading off
set feedback off
$*;
exit
SQL
}

STATUS_DB=$(execsql_silent "SELECT status FROM v\$instance")

No comments:

Post a Comment

comment