How to execute a program as variable in bash script? - linux

I am writing a program with two inputs prog1 and prog2 which are both files. I save these two variables as program1 and program2 first and then execute these two files in my program using ./"$program1" and ./"program2", but the output is "No such file or directory" when i used ls to check the files are in the same directory as the script. Can anyone tell me how to execute it? thanks!

First, it seems like your brackets are out of place - ./"$program1" is not a valid expression, either loose the brackets or put it like this: "./$program1".
Then to elaborate a bit on David's comment:
There are two ways you can run another script from your script. One is to make them executable like David said.
However, you can also use the 'source' command to read and execute the content of files, even if they are not executable. So the following command would work in any case:
source "./$program1"

Instead of executing it as ./$prog1 execute it as below.
bash $prog1
bash $prog2
where $prog1 and $prog2 are variables which have the path of the bash scripts.

Related

Difference between ways in script execution [duplicate]

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

What's the difference between "./<executable>" and ". ./<executable>"? [duplicate]

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[script]"?

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

How do i make my own created shell work with .sh files

My teacher gave us this assignment to create our own shell. Our shell is supposed be called rshell and is supposed to work like the regular shell.
I created my own shell using C++. If you type a command like ls in my created shell it gives you a list just like how if you typed ls in the regular shell.
The problem I am facing is how do I get the .sh files or script files to work with my created shell. I noticed when I run a .sh file using my shell it does not run the .sh file through my shell. It runs it through the regular shell. How do I make .sh files run through my shell?
Change the hash-bang line of the scripts to point at your shell. For instance,
#!/usr/local/bin/rshell
Or wherever your shell executable is.
As John already said, change the shebang to point to your shell. The kernel will invoke the command in the shebang with the file itself as an argument. To demonstrate, try a file with a shebang of #!/bin/cat.
#!/bin/cat
hello world
It pretty much behaves the same as if you typed /bin/cat /path/to/file.
The shebang does not have PATH lookup capabilities, so #!yourshell would not work as a shebang. However, you can use env to do the PATH lookup as in #!/usr/bin/env yourshell. (This approach is preferred for commands that are at different paths on different systems, like python.)

Making perl scripts executable... can I do away with the preceding 'perl' statement?

This is a pretty simple one... I just want to make a perl script executable without the preceding perl command, and instead let the environment deduce the interpreter from the shebang line. Here is my sample script called test:
#!/usr/bin/perl
print "Hey there\n";
I then use chmod 775 test to make the script executable. If I use the command perl test, I get the output Hey there.
However, if I just type test, I get no output. What's the deal? Why isn't my shebang line making the environment realize this is perl? Can someone please help me?
Don't name your script test. This is a built-in command in most shells, so they don't go looking for an external program.
Also, to run a program in your current directory, you should type ./programname. It's generally a bad idea to have . in your $PATH, which would be necessary to execute it without the directory prefix.
To run something from the current directory you need to prefix "./" to tell it "this directory" ie ./testprogram.
If you type just test it will look in standard install directories like /bin. This is why when you run cp or rm it knows where the executable is.
As mentioned by others, naming scripts test is not allowed with most shells.

Resources