Running shell script with more information - bash

My script get stuck in some point and it doesn't show any error. I would like to know why and where it gets stuck. Is there a possibility to run a script and show me more information what is it running?
I have found -x command to run it in debug mode but I have a lot of scripts which are connected and it doesn't work for me.
With -vvv I have also tried, but somehow it doesn't show more than without it.
Does anyone know another command for that?

Read:
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html
... and this:
https://unix.stackexchange.com/questions/155551/how-to-debug-a-bash-script
... and if that's not enough, look at the gdb-like bash debugger:
http://bashdb.sourceforge.net/
and many moore...

Related

Not able to run multiline shell command in Jenkins pipeline

I have a shell script where I am running a multiline command to fetch version from our UI.
Command I am using is:
var=$(curl -k myurl| awk something | sed something)
Then I am using the var for further validations.
The above shell is not promoting the correct response. I checked running this in my terminal and command seems fine but this is not working with Jenkins.
Can someone please help me to understand how to solve this issue.
I have gone through multiple posts but no help.
As you mentioned, it is working on your terminal, then it should also work on your Jenkins shell, can you be more brief by sharing the error or difference in response you are getting ,
But anyways as an alternative to this, you can save your shell command in a script file, and execute it using Jenkins.

Bash file running fine manually but on cronjob stops

I've created a bash file that queries my database and then updates some tables.
When I run it manually everything goes smoothly but when I run it with a cronjob it runs the first query and then stops before it goes into a loop.
After looking into it on the net I found a few things that may be the issue but from my side everything looks in order.
So what I did:
Checked if #!/bin/bash is included in my bash at the start and it is.
Checked that the path is correct in the cronjob. My cronjob below
0-59/5 * * * * cd /path/path2/bashLocation/; ./bash.sh
The loop is in the format of
for ID in ${IDS//,/ }
do
...do something
done
This works fine tested manually. My IDS are in string format that why I split it with //,/.(Works fine)
I log all outputs in a log file but it doesn't show any error.
Has anyone encountered this issue before or has any ideas how to fix the issue?
If the command you are running in cron has percent signs ('%'), they need to be escaped with a backslash. I've been bitten by this. From the manpage: "Percent-signs (%) in the command, unless escaped with backslash () ..."
The $PATH variable may be different when run from cron. Try putting something like this at the beginning of your script: export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Try running bash explicitly, i.e. rather than ./bash.sh in crontab, try /bin/bash bash.sh
I don't know how helpful this may be to some people but I noticed when I printenv shell in my logs it printed that it was bin/sh even if I define it at the top of my script and run it as a bash file.
So what I did was changed all parts of my code that where not supported by shell and my conjob works fine.
So I assume that conjob does not support bash files. (Didn't find anything on the internet about this.)
Why it runs in /bin/sh I don't know.
Hope someone finds this helpful.

What will >_ command do in bash

I saw >_ in bash shell icon in mac and was wondering what it will do.
I tried performing the command but I was not able to figure out what happened. My command prompt silently came again.
It should be doing something, that's why it is in the icon for terminal.
Do anyone know what it does and can it be used for something?
Running >_ will successfully run (nothing), redirected to a file named _. It's not terribly useful, but that's what it does. The most useful empty redirection I've seen is to empty the contents of a file with it:
> /var/log/app.log
which, if app.log is not open by another process, will result in /var/log/app.log being empty.

Problems running bash script from incron

I have a simple incron task setup to run a command whenever a particular .json file is written-to, then closed.
/var/www/html/api/private/resources/myfile.json IN_CLOSE_WRITE,IN NO LOOP /var/www/html/api/private/resources/run_service.sh
I can see that whenever the file to written to, there is a syslog entry for the event, and the command that was triggered - along the lines of <date> - incrond: CMD (/var/www/html/api/private/resources/run_service.sh).
But nothing seems to happen...
initially I thought this would be caused by an issue with the script, but replacing the script command to something simple such as echo "hello world" > /tmp/mylog.log still yields no output or results. I seem to have hit a brick wall with this one!
Update
Changing the incron command to read "/bin/bash /var/www/html/api/private/resources/run_service.sh" now seems to triggering the script correctly, as I can now get output from the script.
A simple mistake on my part, despite all examples online showing that using the script as the command should run it, for me it only works if I explicitly call bash to execute it
"<my directory/file to watch> <trigger condition> /bin/bash /var/www/html/api/private/resources/run_service.sh

Difference in behaviour between Cygwin and Command Prompt when running Ruby scripts

I'm running exercise 14 of Learn Ruby the Hard Way. If I run the script in cmd it works fine, but I've been using Cygwin because it's nicer. When I run it in cygwin using this command:
ruby ex14.rb Devon
I get the following output
test
one
two
Hi Devon, I'm the ex14.rb script.
I'd like to ask you a few questions.
Do you like me Devon?
> Where do you live Devon?
> What kind of computer do you have?
> Alright, so you said test about liking me.
You live in one. Not sure where that is.
And you have a two computer. Nice.
That is to say, the program starts and immediately runs the three STDIN.gets.chomp() commands, and once it gets through those it puts and prints everything at once.
Is there a way to fix this behaviour? I would obviously want to have the lines run in the order they are written. I was unsure what to google for this type of error - combinations of "cygwin", "ruby", "puts output delayed" and "gets out of order" returned nothing relevant. Those search terms seem to vague anyway.
What exactly is going on, and is there a solution?
I think it is all to do with the CR LF differences between dos and unix.
try this...
set -o igncr
before running your script.

Resources