Operating System - HP-UX
1752614 Members
4439 Online
108788 Solutions
New Discussion юеВ

Can anybody help me to understand the script and...

 
SOLVED
Go to solution
Shahbaz_1
Regular Advisor

Can anybody help me to understand the script and...

Hi Friends,
I've a typical task, a shell script, may be written in AIX, need to convert to hp-ux.

following is the script,
----
Change script name to lower case
QUERY='echo $1 |tr "[A-Z]" "[a-z]".sql
#Create the body of SQL(OTXUP20.sql)
build ()
{
SQL='cat $YDEBTSQL/$QUERY'
cat $YDEBTSQL/insertCT.sql > $YDEBTSQL/OTXUP20.sql
if [ "$?" != "0" ]
then
exit 9
fi
echo "SQL" >> $YDEBTSQL/OTXUP20.sql
if ["$?" !="0"]
then
exit 9
fi
cat $YDEBTSQL/exit.sql >> YDEBTSQL/OTXUP20.sql
if ["$?" !="0"]
then
exit 9
fi
}
runit()
------
First I think need to understand, what exactly it is doing, then move to the next part...

Please ...

Thanks in Adv.

Regards
Shah



Let's share the great thing "THE KNOWLEDGE"
5 REPLIES 5
Bill McNAMARA_1
Honored Contributor
Solution

Re: Can anybody help me to understand the script and...

QUERY='echo $1 |tr "[A-Z]" "[a-z]".sql


>>> seems to be a missing quote at the end.
>>> I prefer to use
>>> QUERY=$('echo $1 |tr "[A-Z]" "[a-z]".sql)
>>> although both will work.
>>>
>>> this bit just tr(anslates) $1
>>> $1 being the first arg of the script
>>> to lowercase.
>>>
>>> ie:
>>> scriptname hostname user port
>>> $0 $1 $2 $3
>>>

#Create the body of SQL(OTXUP20.sql)
build ()
{
SQL='cat $YDEBTSQL/$QUERY'

>>> $YDEBTSQL is obviously a directory name
>>> $QUERY is the filename we just lowercased
>>> $SQL is now the content of that file.

cat $YDEBTSQL/insertCT.sql > $YDEBTSQL/OTXUP20.sql

>>> same as cp $YDEBTSQL/insertCT.sql $YDEBTSQL/OTXUP20.sql


if [ "$?" != "0" ]
then
exit 9
fi

>>> command error codes are returned to the
>>> shell and referenced by echo $?
>>> no error = return code 0
>>> here your application/script, will return
>>> an error 9 if it can't do the previous
>>> operation.


echo "SQL" >> $YDEBTSQL/OTXUP20.sql
>>>
>>> this will redirect the text SQL
>>> into a newline in the file OTXUP20.sql
>>> common mistake is to:
>>> echo "SQL" > $YDEBTSQL/OTXUP20.sql
>>> that just creates a file with text SQL
>>> and 'erases' previous content of the file.


if ["$?" !="0"]
then
exit 9
fi
cat $YDEBTSQL/exit.sql >> YDEBTSQL/OTXUP20.sql
if ["$?" !="0"]
then
exit 9
fi
}
runit()

runit()
is referencing a function defined somewhere else in this script or in a referenced script.

Later,
Bill
It works for me (tm)
T G Manikandan
Honored Contributor

Re: Can anybody help me to understand the script and...

First it is changing the filename which is entered as argument, to lowercase.
Then,it is just doing a cat of insertCT.sql,$QUERY and exit.sql and storing it in a single file i.e.getting the contents of the three files and storing it in OTXUP20.sql.
In the meanwhile it is checking for the status to be successful.
The script exits if one of the steps where it is doing a cat and storing to OTXUP20.sql fails.


Thanks

Thanks
Shahbaz_1
Regular Advisor

Re: Can anybody help me to understand the script and...

Hi Bill & Manikandan,
Thanks Lot,
some times, we don't understand simple things, may be because of lack of patience.
You people are very good. great,,

Now I understand the script, but the following line,

echo "SQL" >> $YDEBTSQL/OTXUP20.sql

to me it is just appending the word "SQL" to $YDEBTSQL/OTXUP20.sql file.
or is it the script writer trying to store the content of SQL file to the above file?

...

Regards
Shah




Let's share the great thing "THE KNOWLEDGE"
T G Manikandan
Honored Contributor

Re: Can anybody help me to understand the script and...

shah,

how about
echo "$SQL" >>/...


THanks
Bill McNAMARA_1
Honored Contributor

Re: Can anybody help me to understand the script and...

SQL='cat $YDEBTSQL/$QUERY'

SQL variable now contains the content of the Query file.


cat $YDEBTSQL/insertCT.sql > $YDEBTSQL/OTXUP20.sql

This is the same as a copy

echo "SQL" >> $YDEBTSQL/OTXUP20.sql

This echos the text
SQL
in a new last line of $YDEBTSQL/OTXUP20.sql
It possibly should be

echo "$SQL" >> $YDEBTSQL/OTXUP20.sql

in order that the SQL variable is referenced, but then
cat $YDEBTSQL/$QUERY >> $YDEBTSQL/OTXUP20.sql

does exactly the same thing..

possibly you want to have lots of tests done, and exist codes everywhere..

Later,
Bill
It works for me (tm)