LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2004, 10:21 PM   #1
MeelaPo
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Rep: Reputation: 0
Using stderr output for decision making


Hi,
I had a quick question about a script I am writing (hopefully this is the right forum). What I wanted to do was use the which command to search for a binary file in $PATH . If the file was found I want to continue my script but if it was not found I want to quit.

Example (bin file not found):

# which sqlplus
This command is not found (or something to that effect)

Is there a way I can use that error to determine what I do in this if statement:

if error = true (or something like that...that's the part I don't know how to do)
quit script
else
continue script
fi

If there is a way any idea how I can implement it? Thanks for your time.
 
Old 10-18-2004, 11:00 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
It might look something like this where I don't want to see any error messages or anything to the screen. The only output I want comes from the script.

Code:
#!/bin/bash

which firefox > /dev/null 2>&1

if [ $? = 1 ]; then
echo "This program doesn't exist"
exit 1
else
echo "This program exists"
fi

Last edited by homey; 10-18-2004 at 11:18 PM.
 
Old 10-18-2004, 11:22 PM   #3
MeelaPo
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by homey
It might look something like this where I don't want to see any error messages or anything to the screen. The only output I want comes from the script.

Code:
#!/bin/bash

which firefox > /dev/null 2>&1

if [ $? = 0 ]; then
echo "This program exists"
exit 1
else
echo "This program doesn't exist"
fi
Thanks for the quick reply thought I have a few more questions (I'm pretty new to Linux scripting). First we have this line:

Code:
which firefox > /dev/null 2>&1
This sends both standard error and standard output to the same file (/dev/null) correct? If I wanted to make an error log so that the user can check to see what is wrong I could send it to a file called /temp/error.log right?

Code:
which firefox > /temp/error.log 2>&1
Now the line:

Code:
if [ $? = 0 ]; then
What exactly does the $? = 0 mean? I know that when using something like $# means number of arguments at the command line. Does the $? = 0 have to do with characters in a file or at the command line?

Finally is the following a valid statement (I know it works in other programming languages):

Code:
else 
  continue
Thanks again for all the help

P.S. Anyone know of a good place/book that can help with beginner to intermediate to advanced Linux scripting?
 
Old 10-18-2004, 11:47 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I changed it abit so it will exit if the program doesn't exist.

#!/bin/bash

which firefox > /dev/null 2>&1

if [ $? = 1 ]; then
echo "This program doesn't exist"
exit 1
else
echo "This program exists"
fi


If you want to send the err message to a file, that will work...
which firefox > /temp/error.log 2>&1

The $? =1 is testing stderr to see if it has something.

I don't know about using continue as I haven't use that command.

"P.S. Anyone know of a good place/book that can help with beginner to intermediate to advanced Linux scripting?"
The biggest book you could ever dream of is right in front of you. That's where I get all my answers.
 
Old 10-20-2004, 01:26 AM   #5
MeelaPo
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Okay, here's another question for you guys.

Now the script I want to write is an automated database creation script that a user can run. What would be the best way to run the SQLPlus commands?

My thoughts were this:

-In the script just prompt the user for all the information (i.e SID, log file specs, important directory locations, tablespace info, etc.) required to create a database

-Once I have all this information I would create another file (called /temp/dbCreate for example) and send all required info to the file (SID >> /temp/dbCreate, MAXLOGFILE >> /temp/dbCreate, etc.) one by one till all information is accounted for

-I would then use the following command in my inital script to actually create the database using SQLPlus:

Code:
sqlplus < /temp/dbCreate
Is this the best possible way to implement what I want to do or is there an easier way?

Thanks again

homey (or anyone else): Is there a webpage reference or man page that can explain the [ $? = 1 ] in a little more detail (like how it works and other usage formats)?

Last edited by MeelaPo; 10-20-2004 at 01:29 AM.
 
Old 10-20-2004, 02:48 AM   #6
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
$? just show the return value of the program that just finished running.

try the following program
int main()
{
return 1234;
}

if you run it and type "echo $?" it'll show 1234.

by default most program define 0 as success 1 or any other value as unsuccess
 
Old 10-20-2004, 08:21 PM   #7
MeelaPo
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the information blackzone.

Anyone have thoughts on the sqlplus issue?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Distribution Decision MegaJohn Linux - Newbie 9 09-26-2005 10:26 AM
in-decision Zuggy General 5 02-01-2005 06:46 AM
decision making new2server Linux - Newbie 1 05-01-2004 02:23 PM
captumring output from STDERR rajatgarg Programming 2 01-30-2004 10:35 AM
stderr is not a tty where are you? davidschob Slackware 6 10-31-2003 10:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration