LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   attaching a shell script (https://www.linuxquestions.org/questions/programming-9/attaching-a-shell-script-267680/)

hk_linux 12-18-2004 06:56 AM

attaching a shell script
 
Is there any debugging tool, which can be used to attach and debug a running shell script?
I am using RedHat Distro.

I have a shell script which is working properly, but at times the behaviour is undesirable. I am unable to find out what is happening to the script?

Any Help is appreciated.

TIA.

/bin/bash 12-18-2004 07:17 AM

Using the -v option will print lines as they are read. There is also --debug.

http://bashdb.sourceforge.net/

Tips on debugging bash scripts can be found HERE

Dommy 12-18-2004 07:38 AM

echo and lots of patients

apeekaboo 12-18-2004 05:50 PM

You could also try bash -x scriptname .

Dommy 12-19-2004 01:04 AM

The problem with any of the verbose modes is that the script will display all the lines in your script even ones that are not part of the execution flow, if your script is at all complex it becomes difficult to make sense of the output.

LasseW 12-19-2004 02:51 AM

The output that results from using the -v and -x flags goes to standard error so you can redirect it to a file and analyze it after the run, ie

bash -vx myscript 2>myscript.log

Dommy 12-19-2004 03:26 AM

My point is that simply using -v and -x gives you info that can be misleading.
I have found echo to be more useful when things get a bit tricky.

chrism01 12-19-2004 07:12 PM

If you have an idea which section is failing, you can:
set -xv
just before it,
then
set +xv
just after to turn it off

hk_linux 12-19-2004 10:00 PM

all,
Thanks for the replies. But my problem is a bit different. All the options suggested, requires the code to be changed or the script to be started with the necessary arguments.
My script is started at the time of PC boot itself and runs forever. At the begining it is ok, but after 2 or 3 days, it starts misbehaving. So i want to attach the script while there is a problem. What i want is something like gdb, which can be used to attach a C executable and debug it.
I have searched the net for this and found no options. I belive no counterpart of gdb for shell script is available.
I am going to try the echos as well as the -x option redirecting to a file. Meanwhile, any suggestions is always welcome.

TIA.

hk_linux 12-19-2004 10:07 PM

i wanna add te comments to the prev.

The pseudo code of the script is like

a=29
while :
do
Read a file. Init a variable from it. Perform actions based on the variable.
read from a auto mounted file system of another PC. Do some actions
sleep $a
done


My assumption is that this loop has to run only every 29 seconds. But wat i see is that, the loop is done continuosly, as if the sleep itself is not there. This phenomenon occurs only after running for 2 or 3 days. And also only in one PC. In another PC, i am able to run this script without problem for many days.

Is there any way the variable $a can be corrupted? Or wat could be the cause for such a thing to happen?

This problem is bugging me for last several days. Any help is very much appreciated.

TIA.

Dommy 12-19-2004 11:35 PM

Your problem is that you are opening the file without closing it again
this results in a new file descriptor being open every 29 secs eventually you
run out of resources

a=29
while
do
Read a file. Init a variable from it. Perform actions based on the variable.
read from a auto mounted file system of another PC. Do some actions
sleep $a
done




hk_linux 12-19-2004 11:55 PM

dommy,
Wat i gave was just a pseudo-code. I am just using cat command and grepping from it. So there is no need for explicit closes. I wonder if there is any open and close possible from shell script.

Also, if we run out of resources, i think, the kernel will remove the process from execution. I am not sure though.

/bin/bash 12-20-2004 03:32 AM

Quote:

I wonder if there is any open and close possible from shell script.
Yes there is.
[n]<> [Filename] #Opens Filename for read/write as file descriptor n.

[n]< [Filename] #Opens Filename for read only access as file descriptor n.

[n]> [Filename] #Opens Filename for write access as file descriptor n.

EXAMPLE: (From Advanced Bash Scripting Guide)
Code:

echo 1234567890 > File    # Write string to "File".
exec 3<> File      # Open "File" and assign fd 3 to it.
read -n 4 <&3      # Read only 4 characters.
echo -n . >&3        # Write a decimal point there.
exec 3>&-            # Close fd 3.

cat File                  # ==> 1234.67890


I would suggest you read the link I posted on bash troubleshooting tips.

You should be able to add a few lines to the script to figure out the problem.

It could be that the script is relying on some global system variable that is getting borked by another process?


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