LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-12-2004, 02:19 PM   #1
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Rep: Reputation: 15
$? in Bash scripts


Hi there,

I am trying to create a script that will create a log file of what the user is doing
so that I can keep track of what is going on (not important to the question).

If the program fails I want the log line removed from the .log file. Accomplishing this is my problem. I think it may have something to do with $?, but I am not sure.

Here's the code:

#!/usr/bin/perl

echo "Enter DB description(followed by RETURN): "
until
[ "$description" ]
do
read description
done

printf "$USER\t$(date)\t$description\n" >>
/usr/local/bioinfo/blast/blastdb/added_DBs.log

/usr/local/bioinfo/blast/blastdb/formatdb_real $*
if [ $? -eq 1 ]
then
set $(wc -l added_DBs.log)
head -$(($1 - 1)) added_DBs.log > added_DBs.log
fi

#End code

If formatdb_real fails, the description is still present in the added_DBs.log file. That is, the if conditional statement is returning a value of 0.

Any ideas of where I may be going wrong?
 
Old 02-12-2004, 03:16 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,555

Rep: Reputation: 182Reputation: 182
"If formatdb_real fails, the description is still present in the added_DBs.log file. That is, the if conditional statement is returning a value of 0.

Any ideas of where I may be going wrong?"

Most programs use a return code of 0 to mean success and use a variety of positive integers as error codes. So 0 is probably success and 1 is the first error type that the programmer ran across.

----------------------
Steve Stites
 
Old 02-12-2004, 04:10 PM   #3
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks for the explanation, but that doesn't really address the problem I'm having.

formatdb_real is failing (ie. return code = 1... or, more accurately, not 0)

The following code:

if [ $? -eq 1 ]
then
set $(wc -l added_DBs.log)
head -$(($1 - 1)) added_DBs.log >> added_DBs.log
fi

is (supposed to be) designed to remove the line that was just added to added_DBs.log should formatdb_real fail, but it isn't executing.

I have tried a condition of

[ $? -ne 0 ]

but have not had any success.

Any ideas on how to get the conditional to execute as appropriate?

Some may suggest adding the log message after formatdb_real executes. My reason for not doing that is formatdb_real can at times take a long time; I want the user to be able to add the log message and walk away while formatdb_real executes.

Thanks again.,
 
Old 02-12-2004, 06:24 PM   #4
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,555

Rep: Reputation: 182Reputation: 182
"Any ideas on how to get the conditional to execute as appropriate?"

Try changing:

if [ $? -eq 1 ]

to:

if test $? eq 0;

and see what happens.

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites
 
Old 02-20-2004, 11:15 AM   #5
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Original Poster
Rep: Reputation: 15
if test $? -ne 0;

that worked.

Thanks very much for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
little help with bash scripts sniff Programming 2 10-26-2005 05:17 PM
bash scripts hoffmanyew Programming 3 08-11-2005 01:27 AM
Need Help With Bash Scripts the_woelf Linux - Software 4 06-30-2004 09:09 AM
Bash Scripts Skute Programming 7 03-12-2004 10:17 AM
Bash scripts? BajaNick Programming 3 07-05-2003 10:13 PM


All times are GMT -5. The time now is 10:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration