Can't call a script from within a script - shell

I am trying to call a script from within another script. The idea is that the program should take in email that is sent to it directly from unix mail as stdin, and then parse some stuff out and send it to a new script.
I am having trouble reaching the new script. However, this problem only occurs when the script is accepting the email directly. If I cat a file into it, there is no problem and it finds the new script.
IE: if i have a test file called "email.txt" and i do the command:
cat email.txt | ./receiveEmail.sh
then the script calling works fine.
but if receiveEmail.sh receives the email directly, it fails to call the new script. I know this is the point where it fails because I get logs that the script is working all the way up to where it tries to call the new script.
--------receiveEmail.sh----------
#!/bin/bash
###do some stuff to parse the stdin coming in and get variable $subject and $body
issue=`. /home/dlaf/bin/makeissue.sh` ->>>> this is the line that doesn't seem to work when the input is straight from the email rather than from a txt file i send it.
I am confused why. I think it might be because I am missing some part of the path? Maybe the email coming in has no idea what my full path actually is? Im not sure though because when I type in to the command line echo $LD_LIBRARY_PATH I just get a blank line, so I assume its not even set so I don't know how this could be a problem

When saving output to a variable with Bash, I usually do this
read issue < <(~/bin/makeissue.sh)
echo "$issue"
If the output is multiple lines you can do this
read -d'' issue < <(~/bin/makeissue.sh)
echo "$issue"
or this
mapfile issue < <(~/bin/makeissue.sh)
echo "${issue[#]}"

Related

Centreon not printing output from bash script

I have a bash script that do checks and show result correctly in local server
but when using graphic web of Centreon the message is empty
I use echo as output of function called from main
Thanks for all, the problem it is when i run script in Nagios-Centreon
i have this outpout NRPE: Unable to read output in OS AIX
the code is large, i resolve it by changing #!/bin/ksh instead of
#!/bin/bash
Basically the problem is because of special caracters in script shell.
https://support.nagios.com/kb/article/nrpe-nrpe-unable-to-read-output-620.html#:~:text=This%20error%20implies%20that%20NRPE,when%20running%20the%20remote%20plugin%20.

Output of complete script to variable

I have rather complex bash script which is normally run manually and thus needs live output on the console (stdout and stderr).
However, since the outcome and output of this script is rather important I'd like to save its output at the end into a database.
I have already a trap function for this and the database query as such is also not a problem. The problem is: How do I get the output of the entire script till that point into a variable?
The live console output should be preserved. The output after the database query (if any) does not matter.
Is this possible at all? Might it be necessary to wrap the script into another script (file)?
I'm doing similar task like this
exec 5>&1
message=$(check|tee /dev/fd/5)
mutt -s "$subjct" "$mailto" <<< "$message"
Add your script instead of check function and change mailing to db query.

missed $ while using variableName in bash script - how to catch such issues?

I have a bash script which does the following:
#!/bin/bash
moduleName=$1
someInfo=`ls | grep -w moduleName`
echo $someInfo
In the line #3, I was supposed to use $moduleName, but I missed it.
Is there any way to find such issues in Bash scripts?
I used shell Check, but it didn't report this issue.
For me, the script looks fine; it lists the files whose name contain the string moduleName.
The script is syntactically correct.
The error in line #3 is a semantic error; it changes the meaning of the script. Only a person that knows the intention of the script can detect it.
There is no way to automatically detect such errors, unless you write a software that reads your mind and knows that you intended to write $moduleName and you mistakenly wrote moduleName instead.

Read file as input for a command skipping lines

I'm trying to use the contents of a text file as the input for a command. I know how to read a file just fine. However, when I pass the read line to the command I want to execute, the script starts skipping every other line.
Given a plain text file named queue:
one
two
three
four
This prints out each line as expected:
queue=`pwd`/queue
while read input; do
echo $input
done < $queue
output:
one
two
three
four
However, when I pass $input off to the command, every other line is skipped:
queue=`pwd`/queue
while read input; do
echo $input
transcode-video --dry-run $input
done < $queue
output (transcode-video outputs a bunch of stuff, but I omitted that for brevity. I don't believe it is relevant):
one
three
I managed to get my script working by first reading the whole file into an array and then iterating over the array, but I still don't understand why directly looping over the file doesn't work. I'm assuming the file pointer is getting advanced somehow, but I cannot figure out why. transcode-video is a ruby gem. Is there something I'm not aware of going on behind the scenes when the ruby program is executed? The author of the gem provided a sample script that actually strips lines out of the file using a sed command, and that works fine.
Can someone explain what is going on here?
The launched app tries to process stdin, and reads a line. Try:
transcode-video --dry-run $input </dev/null
Or check the manual for a command-line flag that does the job.

How to call a variable between double qoutes in bash?

I'm trying to execute this command in a script:
axel "$1"
Where "$1" is a URL sent to this command in a script by the firefox plugin FlashGot. However,the URL is long and it keeps cutting it off short. The only way to overcome this is to enclose the URL in single or double quotes...eg. "http://...."
Thanks, in advance.
EDIT:
Ok, so an example of the URL is http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4&lid=290476474&token=Z6TTYtio6FYbhzesbxzPyWA%2F%2Bfa2uT5atbV8L0QF%2FMubHshmLJ1hgkN6B8SMZe74V8Q1feGMNmkmyTJO343qYkQ3aklQVKo4mDE2VVl1nkYk05gu0%2BBfP3WtxTCrn8r0gz0wwDgMfzQd68fBcmOTKtB%2FjR2kqVs9ZY7tZQUuabjGcP84ws%2BuIsuTqkKkHyrWaaLkGhk71GoPng2IMrm0L%2B6MeyHu6bvWn%2FoqNhXNerpFLpRZqXZ8JrX9uKVkDmkeQxUVV5%2F8y8uv2yYpG3P5tx1mfAY6U7ZteDLCfCT4JQWzlZscpl7GmtW4gf64KBExGA98xucIp%2Bt1x%2Bjru2Jt%2F7PVeeKWGv2en0%2Fetf1CQWjVUbDoWy4q9cEnYOc7rkpX
Well, it keeps cutting it off at
http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
and that is all is getting sent to axel.
I added an echo command in the script:
#!/bin/bash
cd /home/caleb/Desktop
echo "$1"
axel "$1"
I can see the debug of a script by sending a URL to it through the terminal:
./axel.sh <URL>
The only error message I see is because of the shortened URL.
Here's the output of the script above:
http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
Initializing download: http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
HTTP/1.1 400 Bad Request
axel "$1" should work and I'm not surprised axel ""$1"" doesn't work because that's equivalent to axel $1.
To debug this we'll need an error message or something, because saying "it doesn't work" doesn't help at all.
You say the script is called from Firefox. I'm not sure if you can easily see the error message, maybe you can't. I have an idea for that. Let's call your script script.sh. Create a wrapper script script-wrapper.sh like this:
#!/bin/bash
log=/tmp/script.log
for arg; do
echo arg="'$arg'" | tee $log
done
/path/to/script.sh >>$log 2>&1
Make this script executable, trigger it from Firefox, and then look at the log, which will include both the output and error output of your original script. If you still can't figure out what is wrong, then edit your question, and paste in the content of /tmp/script.log so we can debug.
UPDATE
Based on your update, it looks like the script does not receive the URL correctly. In particular, it looks like the URL is not quoted properly when you pass it to the script. It's not surprising that the cut-off happens right in front of a & character, as that means something to the shell. You should call your script like this:
./axel.sh "http://....?version=4&lid=..."
But this is not happening, it looks it's getting called without the double-quotes, which will result in the behavior you're observing.
Just using
#!/bin/sh
axel "$1"
will work. If it isn't, you're going to need to give a lot more information...

Resources