- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Testing in Bourne shell vs POSIX shell
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 09:10 PM
тАО09-21-2005 09:10 PM
I have yet another Bourne shell vs POSIX shell question.
In a script I check the database status using the following statements:
# Check database status
STATUS=$(sqlplus -s <
set heading off feedback off
select status from v\$instance;
exit
EOF)
Now my question is:
When I test using the Bourne shell all works fine (see example 1), bur when I test the POSIX (preferred) way, it fails.
#example1
if [ ${STATUS} = "OPEN" ]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
exit
fi
The database started successfully!
#example2
if [[ ${STATUS} = "OPEN" ]]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
exit
fi
Database NOT open, status is: OPEN
An echo \'${STATUS}\' returns ' OPEN'
An echo \'"${STATUS}"\' returns '
OPEN'
What is the best way to use the POSIX testing method (and make working)
Thanks in advance,
Renarios
Solved! Go to Solution.
- Tags:
- command substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 09:17 PM
тАО09-21-2005 09:17 PM
Re: Testing in Bourne shell vs POSIX shell
# Check database status
STATUS=$(sqlplus -s <
set heading off feedback off
select status from v\$instance;
exit
EOF)
echo ${STATUS}
Execute and revert with results.
Good scripting is as,
if [[ "${STATUS}" = "OPEN" ]]
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 09:33 PM
тАО09-21-2005 09:33 PM
Re: Testing in Bourne shell vs POSIX shell
here are the results:
$>$STATUS=$(sqlplus -s <
> set heading off feedback off
> select status from v\$instance;
> exit
> EOF)
$>echo ${STATUS}
OPEN
The problem still exists:
if [[ "${STATUS}" = "OPEN" ]]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
fi
Database NOT open, status is: OPEN
$>echo \'${STATUS}\'
' OPEN'
$>echo \'"${STATUS}"\'
'
OPEN'
If I test using if [[ "${STATUS}" = ' OPEN' ]] it also doesn't work
Any more ideas?
Cheers,
Renarios
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 09:48 PM
тАО09-21-2005 09:48 PM
Re: Testing in Bourne shell vs POSIX shell
echo ${STATUS} | od -dc
Change the check statement like,
if [ $(echo $STATUS | grep 'OPEN') = "OPEN" ]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
fi
# Note: Don't use exit when executing shell statements with script.
BTW: I am Muthukumar (hth - hope this helps)
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:12 PM
тАО09-21-2005 11:12 PM
Re: Testing in Bourne shell vs POSIX shell
The "echo ${STATUS} | od -dc" returns the following:
echo ${STATUS} | od -dc
0000000 20304 17742 02560
O P E N \n
0000005
I didn't write the script myself, I'm only checking it on errors and rewriting it to POSIX. The exits in the script is another issue, but we'll solve that.
The option you gave is a good one.
Is there an option to use for the creation of the STATUS variable?
I know an option like:
# Remove leading CR/LF
status=$(echo ${STATUS} | cut -c 1-)
But that's very quick and dirty. Is there another way for that?
Thanks,
Renarios
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:17 PM
тАО09-21-2005 11:17 PM
Re: Testing in Bourne shell vs POSIX shell
To remove CR
str=$(echo $str| tr -d '\r\032')
To remove LF
str=$(echo $str | tr -d '\r')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:20 PM
тАО09-21-2005 11:20 PM
Re: Testing in Bourne shell vs POSIX shell
Sorry for confusing. try to use:
sed 's/^M$//'
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:21 PM
тАО09-21-2005 11:21 PM
Re: Testing in Bourne shell vs POSIX shell
# echo 'OPEN' | od -dc
0000000 20304 17742 02560
O P E N \n
0000005
Actually It is not good to get "STRING" as return value in shell scripting (my idea)!!. Try to use number for that easily as,
# Check database status
STATUS=$(sqlplus -s <
set heading off feedback off
select status from v\$instance;
exit
EOF) | grep -q 'OPEN'
if [ $? -eq 0 ]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
exit
fi
# END
$? is the variable returns value of the previous command execution. I am requesting you to use it. Use like $? or ${?}
May be problem with sh binary? Don't know.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:36 PM
тАО09-21-2005 11:36 PM
Re: Testing in Bourne shell vs POSIX shell
afaik, the Unix line feed is \n or \012
(man ascii), so the transliteration should in my opinion read
echo "$STATUS" | tr -d \\012
Besides, there isn't such big difference between the HP Posix Shell and the Bash.
You could as well avoid word splitting in Bash in your test blocks by usage of the twin square brackets
e.g.
if [[ $STATUS = OPEN ]]; then ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 12:06 AM
тАО09-22-2005 12:06 AM
Re: Testing in Bourne shell vs POSIX shell
#/usr/old/bin/sh
or
#/usr/bin/sh
And just like with Bourne, POSIX, Korn and BASH, you can trace the execution with set -x or run the script with the interpreter and the -x flag as in:
/usr/old/bin/sh -x myscript
Note that tracing is always sent to stderr so to pipe everything into more or pg, redirect stderr:
/usr/bin/sh -x myscript 2>&1 | more
As far as the OPEN not working, the result of the program is TWO lines, not one. Although octal it's a pain to read, it's clear that your sqlplus program returns a blank line followed by OPEN. So there are several ways to solve this:
STATUS=$(echo "$STATUS | tail -1)
but this suffers from the prblem that the sqlplu program may be unstable (not dependable to always write a blank line), so just use grep:
if echo "$STATUS" | grep -q OPEN
then
echo "Database is OPEN"
else
echo "Database not open, status is \""$STATUS"\""
fi
(note the use of extra " in the not-open status--it will delimit the beginning and end of the actual string)
BTW: A much, much easier way to decode strings is to use hex:
echo "$STATUS" | xd -xc
Note that od and xd are the same program, so you can also use this:
echo "$STATUS" | od -xc
Bill Hassell, sysadmin
- Tags:
- redirect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 12:27 AM
тАО09-22-2005 12:27 AM
Re: Testing in Bourne shell vs POSIX shell
Since this script is executed by user oracle (non-root) I always code using the interpreter #!/usr/bin/sh. Is that correct?
I will use the following in my script (which according my information is correct POSIX ):
STATUS=$(echo $(sqlplus -s <
set heading off feedback off
select status from v\$instance;
exit
EOF) | tr -d \\012)
# Use double brackets (My favorit)
if [[ ${STATUS} = "OPEN" ]]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"${STATUS}
#exit <-- rewrite to return
fi
Cheerio,
Renarios
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 04:44 AM
тАО09-22-2005 04:44 AM
Re: Testing in Bourne shell vs POSIX shell
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 05:35 AM
тАО09-22-2005 05:35 AM
Re: Testing in Bourne shell vs POSIX shell
[[ "$STATUS" = "OPEN\n" ]].
The thing I don't understand is how the
trailing newline is getting in there in the
first place. The $() construct is supposed to
strip it: (from the man page)
The standard output from a command enclosed in
parenthesis preceded by a dollar sign ($(...))
or a pair of grave accents (`...`) can be used
as part or all of a word; trailing newlines are
removed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 08:01 PM
тАО09-22-2005 08:01 PM
Solutionthe problem is not related to teh shell, but to Oracle. You disable the header putting head off but not the new page. Instead of head off use pages 0 and SQL will retrun the status withou any control char:
Your code:
09:58 GPOP09BN hpbbnn1/home/oracle/> STATUS=$(sqlplus -s <
> set head off feedback off
> select status from v\$instance;
> exit
> EOF)
10:00 GPOP09BN hpbbnn1/home/oracle/> echo "$STATUS"
OPEN
10:00 GPOP09BN hpbbnn1/home/oracle/>
my code:
10:01 GPOP09BN hpbbnn1/home/oracle/> STATUS=$(sqlplus -s <
> set pages 0 feedback off
> select status from v\$instance;
> exit
> EOF)
10:01 GPOP09BN hpbbnn1/home/oracle/> echo "$STATUS"
OPEN
10:01 GPOP09BN hpbbnn1/home/oracle/>
This fix your problem about test.
Art
- Tags:
- SQL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 08:05 PM
тАО09-22-2005 08:05 PM
Re: Testing in Bourne shell vs POSIX shell
Check this on following the execution of sql as,
if [[ ${STATUS} = "OPEN" ]]
then
echo "The database started successfully!"
else
echo "Database NOT open, status is:"$STATUS
exit
fi
You have to get "The database started successfully!" on getting ${STATUS} on POSIX shell.
Try it and post result.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 08:08 PM
тАО09-22-2005 08:08 PM
Re: Testing in Bourne shell vs POSIX shell
> then
> echo "The database started successfully!"
> else
> echo "Database NOT open, status is:"$STATUS
> exit
> fi
The database started successfully!
10:09 GPOP09BN hpbbnn1/home/oracle/>
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-22-2005 08:24 PM
тАО09-22-2005 08:24 PM
Re: Testing in Bourne shell vs POSIX shell
Gregory, I already tried your test scenario, but that failed. I 'll dive into the man pages again.
Art, You scored the lot! That was solution on my issue.
Solution the reply from Art!
Thanks to all who assisted me on this issue.
Cheerio,
Renarios