Operating System - Linux
1756379 Members
3195 Online
108847 Solutions
New Discussion юеВ

Re: how to call profile function

 
SOLVED
Go to solution
Tonatiuh
Super Advisor

how to call profile function

Red Hat Enterprise Linux 4

I have a function into my profile:

function lcprod01 {
export ORACLE_SID=lcprod01n1
PS1="[`hostname`*${ORACLE_SID} \${PWD}]$ "
dump
}

From the prompt I call the function just writing the name of the function. OK.

How can I call the function from inside another shell script?

This is I have a shell script wich receives a parameter with the name of the funcion and I need to call the function (accroding to the parameter of the shell script) from inside the shell script.

Example:

$ MyScript.sh ProfileFunctionName
4 REPLIES 4
Ivan Ferreira
Honored Contributor
Solution

Re: how to call profile function

Try adding this to MyScript.sh:

source $HOME/.bash_profile

OR

. $HOME/.bash_profile
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Tonatiuh
Super Advisor

Re: how to call profile function

Ivan,

I am afraid that nothing of both options has worked.

This is the code of my script.

#!/bin/bash
#
# Description: De la base de datos especificada, mata todas las sesiones del usuario especificado
# Parametros de entrada: $1 = nombre/referencia de la instancia
# $2 = username del usuario a matar todas sus sesiones
#
# Set ORA_HOME equivalente a $ORACLE_HOME
# Set SCP_HOME equivalente a la ruta completa donde se encuentran los cripts
#
SCP_HOME=/home/oracle/scripts
ORA_HOME=/opt/oracle/rdbms/9.2.0
ORA_USER=$2

. $HOME/.bash_profile
$1 #<- Here should call the function

sqlplus /nolog $args <connect / as sysdba
spool $SCP_HOME/sessions_to_kill.sql
select instance_name from v\$instance;
select 'alter system kill session '''||sid||','||serial#||''';' from v\$session where username='$2';
spool off
exit
EOF


And this is a sample code for the profile functions:


function lcprod01 {
export ORACLE_SID=lcprod01n1
PS1="[`hostname`*${ORACLE_SID} \${PWD}]$ "
dump
}

Mike Stroyan
Honored Contributor

Re: how to call profile function

The functions defined in .bash_profile won't be visible to shell scripts by default. You can use "export -f lcprod01" in your .bash_profile to make the lcprod01 function visible to shell scripts started from your login shell.

Your function will have another problem. It is setting PS1. But setting PS1 in a shell script will not change the value of PS1 in your login shell. The only way to set a variable in your login shell is to evaluate the assignment in your login shell.

One way to set a variable is to source the shell script. You can to that with ./myscript.sh from your login shell.
That runs myscript.sh in your login shell instead of starting a new shell process to run it.

Another way is to source the output of your script as shell commands. That approach is taken by the resize command.
$(resize)
says to evaluate the variable assignment commands that resize write to its stdout.

Peter Nikitka
Honored Contributor

Re: how to call profile function

Hi,

put the code of your function(s) in a seperate file, e.g. /usr/local/bin/myfuncs .
This single file you can source in your .profile as well as in other shell scripts via
. /usr/local/bin/myfuncs

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"