LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using stderr output for decision making (https://www.linuxquestions.org/questions/programming-9/using-stderr-output-for-decision-making-244443/)

MeelaPo 10-18-2004 10:21 PM

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.

homey 10-18-2004 11:00 PM

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


MeelaPo 10-18-2004 11:22 PM

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?

homey 10-18-2004 11:47 PM

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. :)

MeelaPo 10-20-2004 01:26 AM

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

blackzone 10-20-2004 02:48 AM

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

MeelaPo 10-20-2004 08:21 PM

Thanks for the information blackzone.

Anyone have thoughts on the sqlplus issue?


All times are GMT -5. The time now is 03:59 PM.