- 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
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