Bash script in agi-bin fails to execute - asterisk - bash

I am trying to execute a bash script from within my dialplan. The bash scripts are from within a directory projectFiles in /var/lib/asterisk/agi-bin. When I try to execute the script, like so:
exten => 0,n,System(/var/lib/asterisk/agi-bin/projectFiles/main.sh ${RECORDED_FILE}.wav ${SOUND_PATH}/menus/wav2.wav)
I end up with the following error:
WARNING[27515]: app_system.c:125 system_exec_helper: Unable to execute '/var/lib/asterisk/agi-bin/projectFiles/main.sh /tmp/rec62.wav /var/lib/asterisk/sounds/en/projects/menus/wav2.wav'
== Spawn extension (test-project, 0, 5) exited non-zero on 'DAHDI/15-1'
The permissions to the script main.sh are set at asterisk as owner and group. Where might I be going wrong?
Any help is most welcome,
Sriram.

I had the same problem and found another reason, which might be common:
I had created the script on Windows, and copied over in binary mode. The text file contained the hidden ^M at each line end. I removed all of them and everything is fine! The error message is of course extremely misleading!

The script within agi-bin, main.sh did not execute successfully. Hence the error message. One line within main.sh copied $1 of the input arguments to the present working directory, which I had assumed would be the same directory in which the script was placed. $HOME for asterisk (and from where it executes all scripts) is /etc. So, a line like the following:
cp $1 .
would copy $1 to /etc. This caused mayhem further down the script. Changing that line makes everything work OK.

Related

How to change current directory via shell script and execute command there?

I have this script:
#!/bin/bash
cd ~/my-dir && bash && runall
And I added this script to my $PATH and when I run this script it does 2 things first.
It changes the current directory and execute bash so that my shell also changes the current dir and i can work there.
Run runall (another shell script) in this folder.
Actual Result:
Script changes my current shell directory successfully.
However, runall is not executed.
Expected Result:
Script changes current shell directory of mine to the my-dir and then execute runall
How can I solve this?
Note:
I know that i can do this:
cd some-dir/ && runall
But this will not change my current session to that dir. I also want to change my current shell directory.
Reason:
I want to also change my current working directory so that i can run other manual commands there after runall executed.
This is very nearly a duplicate of Change the current directory from a Bash script, and the answer is very similar -- the only difference being the appending of the command you want to run.
Don't use a script at all; instead, in your ~/.bashrc or similar, define a function:
runInMyDir() {
cd ~/my-dir || return
runall
}
...to define a command runInMyDir. (If you want runall to happen in the background, add a & at the end of that line).
If you do want a script, that script needs to be sourced rather than executed out-of-process -- when a program is executed as an executable external to the shell, it has already been split off from the original shell before it starts, so it has no opportunity to change that shell's behavior. Thus, if you created a file named runInMyDir with the commands cd ~/my-dir || return and runall, you would need to run source runInMyDir rather than just runInMyDir to invoke it.

Running a cd command after user input in bash script will not change directory [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am trying to create a script whereby I have list of numerical test folders and want users to be able to cd into one of them after inputting the folder number.
The script correctly concatenates the input but on running the script it does not actually execute the cd command to the required directory?
It echo's to the screen but then just sits there as if awaiting a further prompt?
Can anyone advise what I am missing please? Script 'chgdir' is as below:
#!/bin/bash
#
# Script to move to test##dir (using input from user for dir number)
echo "Enter test directory number as ## and hit Return"
read dirnum
echo "cd /home/John/test$dirnum""dir"
However on running the script outputs the command to the screen but does not 'cd' and just remains in ~/bin?
cd /home/John/test01dir
John#John-PC ~/bin
P.S I am completely new to bash scripting as you can tell so any help really appreciated.
All your script does is to echo the command that you formed. You need to actually execute the cd command as well as just echoing it.
cd /home/John/test ${dirnum}dir
The {} around the variable name allows the shell to distinguish the variable name from the extra characters appended after it.
That will change the directory inside the script. To have it apply afterwards, you will need to source the script (with dot "." or "source") to affect the shell you are running in.
Your script just prints the command. That's all the echo command does. It doesn't execute it, because you didn't tell it to.
You could execute the cd command by replacing the echo command with this:
cd "/home/John/test${dirnum}dir"
But if that's the last line of your script, it won't do anything useful. Doing a cd inside a script doesn't affect anything but the script itself.
If you want to cd from a script and have it take effect in the invoking shell, you can source the script rather than executing it:
. ./thescript
or you can have the script print the command you want to execute and eval its output:
eval "`./thescript`"
(To be clear, if you source the script using the . command, it needs to execute the cd command; if you val its output, the script needs to print the command.)

OpenVPN Source vars not working on debian

I have to create a script to setup an OpenVPN server automatically.
In this script I need to source the vars file in /etc/openvpn/easy-rsa/
But when I'm executing the following script in the /etc/openvpn/easy-rsa/ folder (with chmod 775 on the script and vars file) it says "xxxx.sh: 3: xxxx.sh: source: not found:"
#!/bin/bash
source ./vars
When I write . ./vars, it works, but then if I want to do a ./clean-all it says :
Please source the vars script first (i.e. "source ./vars")
Make sure you have edited it to reflect your configuration.
When I do the ./clean-all in the same script than the . ./vars, it works.
Thanks for your help (and sorry for my bad english :/)
When you source (or .) a file, all the commands inside it are read and executed - this includes variable assignments. However, when a variable assignment takes place, it takes place only for the current shell. When you run a script, a subshell is created - so any variables inside the script are only visible within the subshell, not the parent (calling) shell. This is why it works when you have run source and clean-all within the same script, it should also work if you do both from the command line, ie:
$ . /etc/openvpn/easy-rsa/vars
$ /etc/openvpn/easy-rsa/clean-all

Changing a directory of the parent shell [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 4 years ago.
I 'm wondering of any mechanism that one could use to change the directory of a parent shell from sub-shell. For ex., I 'm running the script "settings.sh" in my $HOME. My $HOME has a directory $HOME/TEST/run. If my "settings.sh" scripts is as below
#!/bin/bash
# some shell related code
# some shell related code
cd $HOME/TEST/run
exit 0
I execute the above script at command prompt at $HOME. After the execution, I expect my command prompt in directory $HOME/TEST/run. I do understand that in sub-shell, it is being cd'd to $HOME/TEST/run, but at the end of the execution, it's back in $HOME.
Is there any elegant way of doing the above, using a single script. One way is to modify the script "settings.sh" to generate another script and then use ". $HOME/generatedScript.sh"
Nope, you can't. That's by design. Parent processes should never be affected by the results of a child without them wanting to be affected (otherwise sub-shells could do all sorts of nasty tricky things to the parent).
What you can do is have the shell save the information into a file or print the directory or ... Such that the parent at least can use it to change directories if the parent wants to.
Wes Hardaker explained the reasoning behind why executing that script does not cause it to change the directory of the parent shell. In order to work around that type of issue, you must "souce" the script instead of execute it. This causes the commands in the script to run in the current shell, rather than a child process.
. ./settings.sh
The first "." is a command which tells the shell to "source" the specified file. Here is the documentation from help .:
.: . filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.

How to run 'cd' in shell script and stay there after script finishes?

I used 'change directory' in my shell script (bash)
#!/bin/bash
alias mycd='cd some_place'
mycd
pwd
pwd prints some_place correctly, but after the script finished my current working directory doesn't change.
Is it possible to change my path by script?
You need to source the file as:
. myfile.sh
or
source myfile.sh
Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.
While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.
Another way to do, is to implement your script as a function in bash.
function cdbm() {
cd whereever_you_want_to_go
echo arguments to the functions were $1, $2, ...
}
This technique is used by autojump:
http://github.com/joelthelion/autojump/wiki
to provide you with learning shell directory bookmarks.
The script is run in a separate subshell. That subshell changes directory, not the shell you run it in. A possible solution is to source the script instead of running it:
# Bash
source yourscript.sh
# or POSIX sh
. yourscript.sh
It can be achieved by sourcing. Sourcing is basically execute the script in the same shell whereas normal execution(sh test.sh or ./test.sh) will create sub shell and execute script there.
test.sh
cd development/
ls
# Do whatever you want.
Execute test.sh by
source test.sh
. is shortest notation for source. So you can also do by
. test.sh
This will execute the script and change the directory of current shell to development/.
whenever you run a script on your login shell, a new subprocess is spawned and the script execution is done in a subshell.Once the script completes, the subshell exits and you are returned to the login shell.Hence whenever you do a cd through a script,the directory is changed to the path specified by cd, but by the time script finishes you come back to your login shell to the working directory from where you started the script.
The way to overcome this is use,
source yourscript.sh
what source does is it executes the script as TCL script, i.e it has the same effect as when you typed each line on the command line of your login shell and it executed from there. So this way when the script finishes after cd , it stays in that directory.
Another practical solution is to end your script by opening another shell session.
For instance:
#!/bin/bash
cd some_place
bash
This is useful, in my case, for scripts located in my ~/bin for instance, called from any other place. It is just a bit painful to type source ~/bin/mygoodoldscript instead of mygoo<TAB><ENTER>.
The downside is that the additional shell takes up a few more resources (not much).
Though there are answers. I think the intention of question is to use script to navigate to specific path.
Here is a simple practical solution works here without cancel out existing terminal environment flag.
provide a bash/tch/sh script to work for path generation
/* .goto.sh */
#!/usr/bin/env bash
echo '~/workspace'
add alias to the script output
alias goto 'cd `.goto.sh`'

Resources