Return: can only `return' from a function or sourced script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return: can only `return' from a function or sourced script
# 1  
Old 04-05-2018
Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command.

Code:
. /opt/app/scripts/cdc_migration.sh

But it fails with the below error when I try it this way

Code:
/opt/app/scripts/cdc_migration.sh

/opt/app/scripts/cdc_migration.sh: line 65: return: can only `return' from a function or sourced script.

Here's the full script

Code:
#!/bin/sh

. ${HOME}/.profile
Usage()
{
cat <<EOF

Usage:
$. /opt/app/scripts/progname -un <USER> -epwd <user_encrypted_pwd> -tu <target_user> -tepwd <target_password> -src <source_listener> -tgt <target_listener> -sdbid <source_db_id> -tdbid <target_db_id> -sschema <src_schema> -tschema <tgt_schema> -test <Y/N>
Example: . /opt/app/scripts/cdc_migration.sh -un USRPDM1_PRRCDC_D -epwd 8E5D00862442061C2383E6661CB93967 -tu USRPDM1_PRRCDC_A -tp 229143B50D056104F445DD63D7214195A3AB5B84D96CE0B2 -src pwxlstnorad -tgt pwxlstnoraa -sdbid CDCPDMD -tdbid CDCPDMA -sschema d8cdcpdmd -tschema d8cdcpdma -test Y

EOF
}

# Get the command line arguments
if [ $# -lt 11 ]
then
   Usage;
   return 1;
fi
user_name=""
enc_pwd=""
tgt_user=""
tgt_epwd=""
src_lstnr=""
tgt_lstnr=""
src_dbid=""
tgt_dbid=""
sschema=""
tschema=""
test=""
while [ $# -gt 0 ]
do
    case "$1" in
        -un)  user_name="$2"; shift;;
        -epwd)  enc_pwd="$2"; shift;;
        -tu)  tgt_user="$2"; shift;;
        -tp)  tgt_epwd="$2"; shift;;
        -src) src_lstnr="$2"; shift;;
        -tgt) tgt_lstnr="$2"; shift;;
        -sdbid) src_dbid="$2"; shift;;
        -tdbid) tgt_dbid="$2"; shift;;
        -sschema) sschema="$2"; shift;;
        -tschema) tschema="$2"; shift;;
        -test) test="$2"; shift;;
        --)     shift; break;;
        -*)
            Usage;
            return 1;;
        *)  break;;     # terminate while loop
    esac
    shift
done

if [ "$user_name" = "" ] ||  [ "$enc_pwd" = "" ] || [ "$tgt_user" = "" ] || [ "$tgt_epwd" = "" ] || [ "$src_lstnr" = "" ] || [ "$tgt_lstnr" = "" ] || [ "$src_dbid" = "" ] || [ "$tgt_dbid" = "" ]  || [ "$sschema" = "" ]  || [ "$tschema" = "" ] || [ "$test" = "" ]; then
   Usage; return 1;
fi

migr_date=`date '+%Y%m%d%H%M%S'`
# creating extraction maps ini file.

xmaps_ini=migr_xmaps_$migr_date.ini

echo "USER $user_name;" >> $xmaps_ini
echo "EPWD $enc_pwd;" >> $xmaps_ini
echo "TARGETUSER $tgt_user;" >> $xmaps_ini
echo "TARGETEPWD $tgt_epwd;" >> $xmaps_ini
echo "SOURCE $src_lstnr;" >> $xmaps_ini
echo "TARGET $tgt_lstnr;" >> $xmaps_ini
echo "DETAIL;" >> $xmaps_ini
echo "XM_COPY;" >> $xmaps_ini

xmap_names=`ls -l $xmaps |awk '{print $9}'|cut -d "." -f2-2`

IFS=$'\n'
    for x in $xmap_names
    do
        echo "SELECT MAP=$x;" >> $xmaps_ini
    done
echo "RENAME SCHEMA=( "${sschema}","${tschema}" );" >> $xmaps_ini
echo "MODIFY NEW_DBID=$tgt_dbid;" >> $xmaps_ini

        if [ "$test" = "Y" ]; then
                echo "VALIDATE;" >> $xmaps_ini
                        else
                echo "REPLACE;" >> $xmaps_ini
        fi
# creating registrations ini file

reg_ini=migr_regs_$migr_date.ini

echo "USER $user_name;" >> $reg_ini
echo "EPWD $enc_pwd;" >> $reg_ini
echo "TARGETUSER $tgt_user;" >> $reg_ini
echo "TARGETEPWD $tgt_epwd;" >> $reg_ini
echo "SOURCE $src_lstnr;" >> $reg_ini
echo "TARGET $tgt_lstnr;" >> $reg_ini
echo "DETAIL;" >> $reg_ini
echo "REG_COPY;" >> $reg_ini
#echo "KEEPREGTAG;" >> $reg_ini

reg_names=`ls -l $xmaps |awk '{print $9}'|cut -d "." -f2-2`

IFS=$'\n'
    for x in $reg_names
    do
        echo "SELECT DBID = $src_dbid REG_NAME=$x;" >> $reg_ini
    done
echo "RENAME SCHEMA=( "${sschema}","${tschema}" );" >> $reg_ini
echo "MODIFY NEW_DBID=$tgt_dbid;" >> $reg_ini

        if [ "$test" = "Y" ]; then
                echo "VALIDATE;" >> $reg_ini
                        else
                echo "REPLACE;" >> $reg_ini
        fi
# migrate extraction maps

dtlurdmo $xmaps_ini >> $scrlog/`echo "$xmaps_ini"|cut -d "." -f1`.log
dtlurdmo $reg_ini >> $scrlog/`echo "$reg_ini"|cut -d "." -f1`.log

mv $xmaps_ini $scripts/ini_files
mv $reg_ini $scripts/ini_files

# 2  
Old 04-05-2018
A return statement is used to exit a function. I think you are looking for the exit statement ?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

3. Shell Programming and Scripting

return in function

I am using ksh. I want to know how can we make any function to return string or double value. I dont want to use the global variables. (5 Replies)
Discussion started by: PRKS
5 Replies

4. Shell Programming and Scripting

return value of a function

I have write a shell function to get the maximum of a vector. However, the returned value from the function is not always the correct one. Here is the script: maxval() { local max j i size arrval size=$1 ; shift max=-999999999 i=0 while do arrval="$1" if then ... (5 Replies)
Discussion started by: fl0r10
5 Replies

5. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

6. Shell Programming and Scripting

need to return value from function

how to return value from function and print from main program??? And also I need to return true or false... Is it possible? (5 Replies)
Discussion started by: darshakraut
5 Replies

7. Shell Programming and Scripting

function return array

Hi all I would like to know if there is a way to return an array for a function. As I know function can return all the contents in an array, I want to return an array type. (6 Replies)
Discussion started by: dophine
6 Replies

8. Shell Programming and Scripting

return value of a function

Hi I have a doubt in the way the variables inside a function are treated . if a function is called from the main script directly, the variables inside them act as global variables. however if the return value of the function is stored to some other variable in the main script as shown,... (3 Replies)
Discussion started by: prez
3 Replies

9. UNIX for Dummies Questions & Answers

Informix function return value needs to be captured in shell script

I need to run a 4ge from within my shell script and capture the string value that is being returned into a unix variable. I have tried the back ticks to no avail. I know it is returning because I did get the returned value to go to my screen at one point.. What I have is: sendlist =... (3 Replies)
Discussion started by: jeniferz
3 Replies
Login or Register to Ask a Question