Debug script


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu Debug script
# 1  
Old 04-01-2019
Debug script

How can I debug this script?

I want to know what it is doing or not doing?

Code:
#!/bin/bash
#
# 
if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here
  echo "we are suspending." > /tmp/systemd_suspend_test.txt
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
  echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi

# 2  
Old 04-02-2019
Quote:
Originally Posted by drew77
How can I debug this script?

I want to know what it is doing or not doing?

Code:
#!/bin/bash
#
# 
if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here
  echo "we are suspending." > /tmp/systemd_suspend_test.txt
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
  echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi

Given that we have no access to your system and can't see any diagnostic messages produced while your script is running, that you have not said anything about what you think might not be working in your script, and have not told us what appears in the file named /tmp/systemd_suspend_test.txt after you run your script; I find it difficult to suggest what you might do differently to debug your code.

I guess one might ask why you're throwing away all of the debugging data you have collected whenever your script is invoked with the first operand set to pre?

I guess one might ask why you don't include any timestamps in your debugging output?

I guess one might ask why you don't include a set -vx command at the start of your script and save the diagnostic information that would produce in a log file?
# 3  
Old 04-02-2019
Use -x to show line by line.

Use:

Code:
 #/bin/bash -x

The script use two parameters:
  1. ./script pre
  2. ./script post
# 4  
Old 04-02-2019
Quote:
Originally Posted by Don Cragun
Given that we have no access to your system and can't see any diagnostic messages produced while your script is running, that you have not said anything about what you think might not be working in your script, and have not told us what appears in the file named /tmp/systemd_suspend_test.txt after you run your script; I find it difficult to suggest what you might do differently to debug your code.

I guess one might ask why you're throwing away all of the debugging data you have collected whenever your script is invoked with the first operand set to pre?

I guess one might ask why you don't include any timestamps in your debugging output?

I guess one might ask why you don't include a set -vx command at the start of your script and save the diagnostic information that would produce in a log file?
Nothing appears in ]/tmp/systemd_suspend_test.txt. Because if it did, the script would be working. :-)

As to your 1st guess, I have no idea how to implement what you are asking. i.e. The reason for my post.

Same with your 2nd and 3rd guesses.

--- Post updated at 03:11 AM ---

Quote:
Originally Posted by nelbren
Use:

Code:
 #/bin/bash -x

The script use two parameters:
  • ./script pre
  • ./script post
I added -x.

Nothing new happened.

My code is a modification of what is here.

Running scripts before and after suspend with systemd << Just another Linux geek
# 5  
Old 04-02-2019
What output do you get from the command:
Code:
ls -l /tmp/systemd_suspend_test.txt

?
What output do you get from the command:
Code:
id

?
What output do you get from the command:
Code:
bash -xv /your/script/name pre

where /your/script/name is an absolute pathname of your script? After running this command what output do you get from the command:
Code:
ls -l /tmp/systemd_suspend_test.txt

?
What output do you get from the command:
Code:
bash -xv /your/script/name post

where /your/script/name is an absolute pathname of your script? After running this command what output do you get from the command:
Code:
ls -l /tmp/systemd_suspend_test.txt

?
# 6  
Old 04-02-2019
Code:
andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
ls: cannot access '/tmp/systemd_suspend_test.txt': No such file or directory
andy@7_~$ id
uid=1000(andy) gid=1000(andy) groups=1000(andy),4(adm),20(dialout),21(fax),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)
andy@7_~$ bash -xv /usr/lib/systemd/system-sleep/On_Resume pre
#!/bin/bash #-x
#
# 
if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here
  echo "we are suspending." > /tmp/systemd_suspend_test.txt
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
  echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi
+ '[' pre = pre ']'
+ echo 'we are suspending.'
andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
-rw-rw-r-- 1 andy andy 19 Apr  2 03:45 /tmp/systemd_suspend_test.txt
andy@7_~$ bash -xv /usr/lib/systemd/system-sleep/On_Resume post
#!/bin/bash #-x
#
# 
if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here
  echo "we are suspending." > /tmp/systemd_suspend_test.txt
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
  echo "and we are back from being suspended" >> /tmp/systemd_suspend_test.txt
  #/usr/bin/gxmessage -fg red -font  'sans 30' -timeout 3  ' Computer has now resumed from suspend state.'
fi
+ '[' post = pre ']'
+ '[' post = post ']'
+ echo 'and we are back from being suspended'
andy@7_~$ ls -l /tmp/systemd_suspend_test.txt
-rw-rw-r-- 1 andy andy 56 Apr  2 03:46 /tmp/systemd_suspend_test.txt
andy@7_~$

# 7  
Old 04-02-2019
So your script seems to do what you want it to do if it is invoked by you.

How have you installed your script so the system will invoke it before suspending your system and while resuming your system after a suspension?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Debug perl script

Hi, I am trying to get squid up and running using a redirector process, and every time I try to load a web page, squid fails miserably. Can some one with perl and squid knowledge take a look at these codes and tell if something is wrong here. #!/usr/bin/perl # $| = 1; @endings = qw/... (0 Replies)
Discussion started by: jamie_123
0 Replies

2. Shell Programming and Scripting

Debug script already running

Hello, is it possible to debug a script that is already running "on-demand"? Somehow a command like "set -x" but once you've executed the script. And another one to disable debugging? Thank you (0 Replies)
Discussion started by: asanchez
0 Replies

3. Shell Programming and Scripting

Cant debug shell script

Hi I am relatively new in shell scripting Below is the code which i developed but for some reason, it keeps giving me error: /apps/bss/BatchProg/at1/batch/scripts/ksh/TBATLC02.ksh: syntax error at line 41 : `then' unmatched #!/usr/bin/ksh... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

4. Shell Programming and Scripting

Help with debug the script

Hi, I have this script, searches and sets variables, then searches and sets more variables from multiple files. I'd need to debug it a bit. #!/bin/bash egrep $1 `find | grep MAGT` >/tmp/resRA-$$ thread=`sed -n '/{0x/ {s/^.*{0x\(*\).*/\1/p;q}' /tmp/resRA-$$` tag=`sed -n '/Tag=/... (5 Replies)
Discussion started by: Vitoriung
5 Replies

5. Shell Programming and Scripting

Problem with the script, help me debug

Hi, When i run the script ./script.sh sun, this give me no output, it should give me the list of file. If i run the script without the argument it should send me echo inside usage(). What is the problem? please help -Adsi #!/bin/sh ROOT_PATH=/net/icebox/vol/local_images/spins... (2 Replies)
Discussion started by: asirohi
2 Replies

6. Shell Programming and Scripting

If condition error-please debug my script

Hi All, I have a script which is based on calculating current time The script looks like this: whenever i try to run the script it gives me error:Please advice wherther i have made mistake with if condition or with for loop?? The code : set -x #!/usr/bin/ksh set -x... (7 Replies)
Discussion started by: usha rao
7 Replies

7. UNIX for Advanced & Expert Users

How can debug our UNIX shell script?

Hi all, i have two simple questions here... 1. i want to know that how to debug the UNIX shell script? 2. is there any way to handling the exception in UNIX shell script like oracle exception handling? Please provide me those details it would be great help/ Thanks and Regards, MPS... (3 Replies)
Discussion started by: psiva_arul
3 Replies

8. Shell Programming and Scripting

debug this script

echo "input time in hhmmss" read $st h=`echo $st | cut -c1-2` min=`echo $st | cut -c3-4` s=`echo $st | cut -c5-6` echo "input time in hhmmss" read $end h1=`echo $end | cut -c1-2` min1=`echo $end | cut -c3-4` s1=`echo $end | cut -c5-6` x= `expr $h /* 60 + $min` y= `expr $h1 /* 60 +... (8 Replies)
Discussion started by: abhishek27
8 Replies

9. Shell Programming and Scripting

How to debug the awk script

Hi, How can I debug an awk script? I know that set -x can be used to debug a script. But this will not suite for awk scripts. Can anyone help me? Thanks in advance, Chella (2 Replies)
Discussion started by: chella
2 Replies

10. Shell Programming and Scripting

Debug an Awk Script

I would like to extract the following fields from the text file attached. I copied the contents from a pdf file and pasted them into the text file so I can use awk to extract them. The layout is as listed below. name1,name2,name3,name4,Title,designation,nationality,dob, national ... (1 Reply)
Discussion started by: mboro
1 Replies
Login or Register to Ask a Question