I'm trying to call puppet from a bash script and whilst it works, it causes my script to end prematurely.
#!/bin/bash
...
function runPuppetLocally()
{
echo "...running Puppet locally"
exec puppet agent --test
echo "Puppet complete"
}
runPuppetLocally
I presume Puppet is issuing an exit or something similar which causes my script to end. Is there a means by which I can call it without it terminating my script?
Why do you use exec? Read help exec:
Replace the shell with the given command.
Your script is replaced with the puppet. If you do not want it to replace your shell, call it normally, i.e.
puppet agent --test
Related
Im exploring a bit on shell scripts and wrote this script. When the build command fails, it has to provide an error message and exit.
I know that exit 1 would close the current shell its running on. And the exit statement is within a curly braces (meaning that its executed on the same shell). But when this script is run as ./testScript.sh it stops executing test(), testTwo() is not called, and the terminal remains open.
While this is exactly the functionality i'm looking for, my question is why doesn't it close the terminal due to the exit 1; command? Does git bash creates a subshell by default when running a script?
I know its not because its been called within a function. I did try running the exit statement in script without a function, and it still doesnt close the terminal.
Any insights on the actual working of git bash and exit command would be highly useful.
Thanks!
# testScript.sh
function test() {
dotnet build -c Debug sample.csproj || { echo -e "${RED}=== Build Failed"; exit 1; }
}
function testTwo() {
echo "== executing test two function."
}
function all()
{
pushd .
test
testTwo
popd
}
all "$#"
Does git bash creates a subshell by default when running a script?
It's not a "subshell" as in ( ). The shell spawns a separate process that executes the command. The process does not inherit bash variables and functions, only inherits exported variables, i.e. it's not a subshell.
Do not use function name(), just name(). See https://wiki.bash-hackers.org/scripting/obsolete . Check scripts with shellcheck.net .
bash scripting noob here. I've found this article: https://www.shellhacks.com/print-usage-exit-if-arguments-not-provided/ that suggests putting
[ $# -eq 0 ] && { echo "Usage: $0 argument"; exit 1; }
at the top of a script to ensure arguments are passed. Seems sensible.
However, when I do that and test that that line does indeed work (by running the script without supplying any arguments: . myscript.sh) then the script does indeed exit but so does the bash session that I was calling the script from. This is very irritating.
Clearly I'm doing something wrong but I don't know what. Can anyone put me straight?
. myscript.sh is a synonym for source myscript.sh, which runs the script in the current shell (rather than as a separate process). So exit terminates your current shell. (return, on the other hand, wouldn't; it has special behaviour for sourced scripts.)
Use ./myscript.sh to run it "the normal way" instead. If that gives you a permission error, make it executable first, using chmod a+x myscript.sh. To inform the kernel that your script should be run with bash (rather than /bin/sh), add the following as the very first line in the script:
#!/usr/bin/env bash
You can also use bash myscript.sh if you can't make it executable, but this is slightly more error-prone (somebody might do sh myscript.sh instead).
Question seems not clear if you're sourcing script source script_name or . script_name it's interpreted in current bash process, if you're running a function it's the same it's running in same process, otherwise, calling a script, caller bash forks a new bash process and waits until it terminates (so running exit doesn't exit caller process), but when running exit builtin in in current bash it exits current process.
I am writing a bash script that will be run by puppet by an Exec resource (I know. It is not ideal). I would like to know if is possible create debug messages in the script. Normally in Bash I use echo and in a manifest I use notify. But echo seems not work when the script is executed as an Exec resource in Puppet.
Check the documentation here for the logoutput attribute: https://docs.puppet.com/puppet/latest/reference/types/exec.html#exec-attribute-logoutput
You can always see the output from your echo in the script during Puppet agent execution with logoutput set to true.
For example:
exec { '/bin/sh script.sh': logoutput => true }
basically I have written a shell script for a homework assignment that works fine however I am having issues with exiting. Essentially the script reads numbers from the user until it reads a negative number and then does some output. I have the script set to exit and output an error code when it receives anything but a number and that's where the issue is.
The code is as follows:
if test $number -eq $number >dev/null 2>&1
then
"do stuff"
else
echo "There was an error"
exit
The problem is that we have to turn in our programs as text files using script and whenever I try to script my program and test the error cases it exits out of script as well. Is there a better way to do this?
The script is being run with the following command in the terminal
script "insert name of program here"
Thanks
If the program you're testing is invoked as a subprocess, then any exit command will only exit the command itself. The fact that you're seeing contrary behavior means you must be invoking it differently.
When invoking your script from the parent testing program, use:
# this runs "yourscript" as its own, external process.
./yourscript
...to invoke it as a subprocess, not
# this is POSIX-compliant syntax to run the commands in "yourscript" in the current shell.
. yourscript
...or...
# this is bash-extended syntax to run the commands in "yourscript" in the current shell.
source yourscript
...as either of the latter will run all the commands -- including exit -- inside your current shell, modifying its state or, in the case of exit, exec or similar, telling it to cease execution.
I am using Chef on Scalarium to download an agent and run various commands on it. What I'm attempting is to write a shell script in the recipe to perform this.
file "/etc/profile.d/blah.sh" do
content <<-EOH
sudo -sH
<Retrieve file and run some commands>
EOH
end
When I run the recipe in Scalarium, no errors occur, but the commands aren't run either. There's no errors in the commands themselves, as I've run them on my computer.
The recipe is definitely read, as the Chef logs contain Processing file[/etc/profile.d/blah.sh] on blah.localdomain.
I've never used Chef before, do I need to do something else to tell it to execute the shell script?
Perhaps you want something like:
file "/etc/profile.d/blah.sh" do
mode 0500
content <<-EOH
sudo -sH
<Retrieve file and run some commands>
EOH
end
execute "/etc/profile.d/blah.sh"
Or, you can put the file retrieval and running of commands directly into your chef recipe:
remote_file "/path/to/where/the/file/should/be/saved" do
source "https://example.com/path/to/where/the/file/comes/from"
end
execute "first command"
execute "second command"