1834149 Members
2425 Online
110064 Solutions
New Discussion

A simple script

 
Augusto César
Advisor

A simple script

Hi,

I have written a simple script but don´t work. This is the script:

$ cat f_get_count.sh
funct_get_count(){
tcount='$ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF'

if [ $tcount!=0 ]; then
echo "Number of rows: $tcount"
fi
}
################################

funct_get_count

################################

$ f_get_count.sh
f_get_count.sh[9]: -s: A test command parameter is not valid.

Thanks,

Cesar (Brazil)
7 REPLIES 7
Simon Hargrave
Honored Contributor

Re: A simple script

if [ $tcount!=0 ]; then

You need spaces in your test clause. Change it to: -

if [ $tcount != 0 ]; then
Arunvijai_4
Honored Contributor

Re: A simple script

How about this ?

unct_get_count(){
tcount='$ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF'

if [ $tcount != 0 ]; then
echo "Number of rows: $tcount"
fi
}
################################

funct_get_count

################################

You need to have spaces when doing comparison.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: A simple script

what are you trying to do here?

tcount='$ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF'

It will declare a variable tcount with that contents.

You have to try as,

tcount=$($ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF)

Change script as,

tcount=$($ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF)

if [ $tcount -ne 0 ]; then
echo "Number of rows: $tcount"
fi
}
################################

funct_get_count

################################
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: A simple script

Change to correct one as,

tcount=$((
$ORACLE_HOME/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF
))

if [ $tcount -ne 0 ]; then
echo "Number of rows: $tcount"
fi
}

# START
funct_get_count

-Muthu
Easy to suggest when don't know about the problem!
Augusto César
Advisor

Re: A simple script

Forgive me for not to explain the script. The script connects with Oracle and shows the number of users in database. I made some modifications but still it does not works.

$ cat f_get_count.sh
funct_get_count(){
tcount=$((/u01/app/oracle/product/9.2.0.1.0/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF
))

if [ $tcount -ne 0 ]; then
echo "Number of rows: $tcount"
fi
}
################################

funct_get_count

################################

$ f_get_count.sh
f_get_count.sh[8]: /u01/app/oracle/product/9.2.0.1.0/bin/sqlplus -s << EOF^Jconn
ect / as sysdba^Jset heading off^Jset feedback off^Jselect count(*) from dba_use
rs;^Jexit^JEOF^J: Syntax error

ps: Sorry for my English.
Greg Vaidman
Respected Contributor

Re: A simple script

Augusto,
You do not want to use the syntax
$(( command ))
in your script - this will only evaluate a real numeric expression, it won't execute the command.

You should use either
`command`
OR
$(command)
Augusto César
Advisor

Re: A simple script

Works!!! This is the right script:

funct_get_count(){
tcount=$(/u01/app/oracle/product/9.2.0.1.0/bin/sqlplus -s << EOF
connect / as sysdba
set heading off
set feedback off
select count(*) from dba_users;
exit
EOF
)

if [ $tcount -ne 0 ]; then
echo "Number of rows: $tcount"
fi
}
################################

funct_get_count

################################

$ f_get_count.sh
Number of rows:

46


Thank you all!!

Augusto