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. |
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 |
Quote:
Code:
which firefox > /dev/null 2>&1 Code:
which firefox > /temp/error.log 2>&1 Code:
if [ $? = 0 ]; then Finally is the following a valid statement (I know it works in other programming languages): Code:
else P.S. Anyone know of a good place/book that can help with beginner to intermediate to advanced Linux scripting? |
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. :) |
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 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)? |
$? 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 |
Thanks for the information blackzone.
Anyone have thoughts on the sqlplus issue? |
All times are GMT -5. The time now is 03:59 PM. |