LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 11-07-2003, 12:41 PM   #1
dtheorem
Member
 
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Rep: Reputation: 15
Flagging exits in Bash Script


Sorry for the weird title, I didn't know what to call this. I wrote a large bash script where a lot of software gets installed automatically. I've put '&&' at the end of every line which its feasible to do so in order to stop the script once errors occur. Is it possible to output the line number of the script that it exited at? If not, is there anyway to flag the script so I can see where it exited?
 
Old 11-07-2003, 01:14 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Rather than using && try examining the exit code in $? and using if statements.
 
Old 11-07-2003, 01:39 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
try something like this (the ps line has an error so you can see)

Code:
#! /usr/bin/sh
let linecnt=1
function check {
     if [ $? -ne 0 ] ; then
         echo "Error occurred on line $linecnt"
         exit 1
     fi
     linecnt=linecnt+2

}
linecnt=11  # starting point
ps -xz
check;
 
Old 11-07-2003, 05:04 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
If you just want to know which line last executed at debugging time, you could run your script like this:

bash -x ./yourscript.bash

This prints the lines as they execute. Putting:

set -x

as one of the first lines in your script has the same effect.

Last edited by Hko; 11-07-2003 at 05:06 PM.
 
Old 11-07-2003, 05:05 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
To try, run this script:
Code:
#!/bin/sh

set -x

true && \
true && \
false && \
false && \
true && \
true && \
exit 0
 
Old 11-08-2003, 10:15 PM   #6
dtheorem
Member
 
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Original Poster
Rep: Reputation: 15
Thanks for the help, guys! I think I'll use the 'set -x' option instead of the check method simply because my script is literally over 1000 lines.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script ... alaios Linux - General 7 07-23-2005 02:55 AM
bash script help me. carboncopy Linux - Software 11 01-06-2005 01:38 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


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

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