Alright, so let's say I have a .bash file with this inside of it
lib.bash
#!/bin/bash
function hello_world {
echo "Hello World!"
}
That file won't be called on it's own, instead it'll be called via another bash file, i.e
Startup.bash
#!/bin/bash
bash lib.bash
hello_world
But, if I run Startup.bash I get the error: hello_world: command not found
What am I doing wrong, or is it not possible to do what I'm trying to do on bash.
You can use this command in your startup.bash:
source lib.bash
the source command runs the file in the current shell environment, unlike using bash lib.bash (or . lib.bash) which creates a new, separate environment for that script (and only that script) and is why the function is not carried over.
(source)
why don't you call the function directly inside of the first script?
It would look something like this:
#!/bin/bash
function hello_world {
echo "Hello World!"
}
hello_world
If it is a simple script, shouldn't be a problem at all.
Otherwise try the source command, like minerz029 suggested :)
See if this could be helpful to you as well:
Shell Script Loader
Related
I am trying to make a .sh file that when clicked it runs the script inside. I am trying to recursively find a certain string value inside the contents of the files from a given folder, using $ grep -r "word" /home/folder_name but I don't know how to do so without running the script in terminal.
Any ideas for this?
Linux shell scripts can be written very basically.To make a shell script, start the script with #!/bin/sh and add normal linux commands. That is a simple explanation, but it is sufficient for most simple scenarios.
Example:
#!/bin/sh
echo "Hello, World!`
Let's say I have an executable shell script called foo.sh. Inside it is a simple echo "Hello World". From my understanding, when I run this via ./foo.sh, a subshell is invoked which executes the echo "Hello World" line.
Why, then, do I see the output of the echo command in my main shell/terminal? I would think you'd have to do a "source ./foo.sh" instead of the simple "./foo.sh" to see the output in your current shell.
Can any of you help clarify?
The standard output is inherited. Quoting from Bash Reference Manual:
Command Execution Environment
When a simple command other than a builtin or shell function is to be
executed, it is invoked in a separate execution environment that
consists of the following. Unless otherwise noted, the values are
inherited from the shell.
the shell’s open files, plus any modifications and additions specified by redirections to the command
...
I have shell script1 1.sh. I have another shell script 2.sh where I need to use the functionality of the first script and store it in a variable to use in the second script.
None of these seem to work:
a=$(sh 1.sh)
a=sh 1.sh
a=`sh 1.sh`
The first solution is correct, try for example:
echo 'echo "hello, world"' > echohello.sh
hello=$(sh echohello.sh)
echo $hello
Maybe you have an error in 1.sh or it is in a different directory? Can you call it directly by running ./1.sh?
I have a few command that I would like to run faster. Best solution seems to be an alias (in fact, a function because I need to use avec few arguments). I would like my alias to be run when the system starts. So I've created a bash file to execute it. To create my function, I key this stuff in my shell :
function myFunction() { command 1 ; command 2 ; etc... }
But here is the problem ! My bash file do not execute the whole command because myFunction is reconized as... a function ! But an internal function.
So is there any way to make the file executing the whole function ?
Thanks a lot guys !
You could just define the function in your ~/.bashrc or from there, source the file in which it is defined.
I have a perl script and I want to do the following:
system("source myscript.sh");
myscript.sh exports multiple envs but these are lost once system completes so that if I now do:
system("echo $SOME_VAR");
It fails because $SOME_VAR was exported by myscript.sh and lost when system completed.
This is a simple example but in my actually use case myscript.sh is a complex script and I want to execute 10 to 20 system commands afterwards that depend on env vars from it.
Thank you in advance for your replies.
Assuming that myscript.sh doesn't print anything,
%new_env = ();
open SCRIPT_ENV, 'source myscript.sh; printenv|';
map {/(.*?)=(.*)/ and do {$new_env{$1} = $2}} <SCRIPT_ENV>;
close SCRIPT_ENV;
%ENV = %new_env;
If the script myscript.sh prints something you will have to do something more elaborate.
You'll have to run a wrapper shell script that looks like this:
source myscript.sh
env
and then read the output of the 'env' command and set the environment within your perl program from that.
So you need to run the wrapper script like this:
open CMD, "wrapper.sh|";