Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with function

Status
Not open for further replies.
Feb 23, 2005
12
US
Here's an excerpt of the code. Please somebody explain this line (MIG_INFO=`grep $1 /u02/app/psoft/fs800/$BASE_ENV_LOWER/migrations/todo/$BATCH`)...I just don't see where the $1 variable is defined anywhere in the function. The script is called M_FQA8.

Thanks for your help.

###################################################################
# Function: update_program #
# Description: This function will be used to update the #
# S_MIG_BATCH_PRG table in the source environment, #
# with the status of the migration. The SQL is #
# dynamically created, using the input parameter: #
# $1 = program name, with extenstion #
###################################################################
function update_program
{
# Change 6 - Begin - added these lines
MIG_INFO=`grep $1 /u02/app/psoft/fs800/$BASE_ENV_LOWER/migrations/todo/$BATCH`
FILE_DESCR=`echo $MIG_INFO | awk -F: '{print $1}'`
OBJECT_INFO=`grep "$FILE_DESCR" objects.txt`
OBJECTTYPE=`echo $OBJECT_INFO | awk '{print $1}'`
OBJECTID1=`echo $OBJECT_INFO | awk '{print $2}'`
OBJECTID2=`echo $OBJECT_INFO | awk '{print $3}'`
# Change 6 - End

if [ -f mig.sql ]
then
rm mig.sql
fi

SHORT_NAME=`echo $1 | awk -F. '{print $1}'`
EXT=".`echo $1 | awk -F. '{print $2}'`"

echo "Update PS_S_MIG_BATCH_PRG " >>mig.sql
echo " set S_MIG_COPYDONE='$DONE_FLG', " >>mig.sql
echo " S_MIG_MESSAGE='$MIG_MSG'" >>mig.sql
echo "where PROJECTNAME='$PROJ_NAME'" >>mig.sql
echo " and S_TGT_ENVIRONMENT='$T_ENV'" >>mig.sql

# Change 6 - Begin - added the if/then/else, these are the old lines
#echo " and OBJECTVALUE1='$SHORT_NAME'" >>mig.sql
#echo " and OBJECTVALUE2='$EXT';" >>mig.sql
if [ "$OBJECTID2" = 0 ]
then
SHORT_NAME=`echo $1 | awk '{print $1}'`
echo " and OBJECTVALUE1='$SHORT_NAME';" >>mig.sql
else
echo " and OBJECTVALUE1='$SHORT_NAME'" >>mig.sql
echo " and OBJECTVALUE2='$EXT';" >>mig.sql
fi
# Change 6 - End

echo " " >>mig.sql
echo "commit;" >>mig.sql
echo " " >> mig.sql
echo "exit" >> mig.sql

echo " " >>mig.log
echo "Update SQL:" >>mig.log
cat mig.sql >> mig.log
sqlplus -s $SQLUSER/$SQLPASS@$BASE_ENV @mig.sql >> mig.log

if [ "$DONE_FLG" = "Y" ]
then
update_history $1
fi
}
 
$1 is the first command-line parameter.

If you type "myprogram test", then $1 = "test"

 
Thanks!....is $1 the same outside of the function ?. Reason is, after the above function, there's another function call $1 and $2 and i'm confused as to if it's the same value.

See code below...

###################################################################
# Function: Migrate_File #
# Description: This function will be used to do the actual moving #
# or copying of the files. #
# There are two input parameters to this function: #
# $1 = full path of source file #
# $2 = full path of target file #
###################################################################

function migrate_file
{
# Change 7 - Begin - Simplified this function by using SCP, removed large amount of code.
# copy the file to the target
$SCP $1 $TARGET_IP:$2 >> mig.log 2>&1

# If the copy failed, log it and set flags to fail
if (( $? != 0 ))
then
echo " " | tee -a mig.log
echo "ERROR: Error in transfer of $1 to $TARGET_IP:$2." | tee -a mig.log
MIG_MSG="Source to target copy failed."
DONE_FLG="N"
STATUS=1
else # copy was successful
echo "Successful transfer of $1 to $TARGET_IP:$2." | tee -a mig.log

# change permissions on the file that just got copied
$SSH $TARGET_IP $CHMOD 755 $2 >> mig.log 2>&1

# If CHMOD failed, log it and set flags to fail
if (( $? != 0 ))
then
echo " " | tee -a mig.log
echo "ERROR: Error in changing permissions of $TARGET_IP:$2." | tee -a mig.log
MIG_MSG="CHMOD of target failed."
DONE_FLG="N"
STATUS=1
else # CHMOD was successful
echo " " | tee -a mig.log
echo "Successful CHMOD of $TARGET_IP:$2." | tee -a mig.log
MIG_MSG="Successful."
DONE_FLG="Y"
STATUS=0
fi
fi
# Change 7 - End
}
 
Sorry, I had the wrong train of thought. Yes, the parameters work the same way with functions.

In this case, the function would be called something like:

migrate_file /home/myself/sourcefile /var/tmp/destfile

The parameters might not be defined anywhere. I could see how this script would simply pass the command-line arguments that I mentioned before, so the function might be called like this:

migrate_file $1 $2

where $1 and $2 are the first and second command-line arguments.
 
The question is what is $1 and $2 in Migrate_file function. I can understand if $1 is coming from the function update_program but what is $2 ?
 
They are defined when migrate_file is called. It's not being called by update_program, so it must be called somewhere else.

You havent included any function calls for update_program or migrate_file, only the functions themselves. They must be called somewhere else in the script, and the $1 and $2 would be defined there.
 
Code:
function print_my_parameters
{
    echo $1 $2 $3
}

print_my_parameters one two three
print_my_parameters hello world goodbye
print_my_parameters do you understand
print_my_parameters how parameters work?

Output
Code:
one two three
hello world goodbye
do you understand
how parameters work?
 
@lgarner i saw where the function is later called.

Thanks for all your help.

@ChipperMDW, thanks!. Now i understand how parameter works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top