Assigning Scripts to Variables for a Wrapper Script
I am trying to assign bash scripts to variables for use in a wrapper script, yet every method I have tried has resulted to a "No such file or directory" error:
#!/bin/bash
textgen=$(< textcreator.sh)
tablegen=$(< tablecreator.sh)
htmlgen=$(< webcreator.sh)
exec $textproces
if [[ $textproces -eq 0 ]]
then
echo "Text Processing has ran successfully"
exec $listgen
if [[ $listgen -eq 0 ]]
then
echo "List Generation has ran successfully"
exec $pagegen
if [[ $pagegen -eq 0 ]]
then
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
else
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
The methods I have tried: cat filename, export, source and standard assigning. The idea is that it is to work without having to give a path name, all of these shells are in the same directory anyway. All help is vastly appreciated!
NOTE: I know its horrifying, but it was the way I thought of doing it with the IF statements
bash variables sh filenames textwrapping
|
show 1 more comment
I am trying to assign bash scripts to variables for use in a wrapper script, yet every method I have tried has resulted to a "No such file or directory" error:
#!/bin/bash
textgen=$(< textcreator.sh)
tablegen=$(< tablecreator.sh)
htmlgen=$(< webcreator.sh)
exec $textproces
if [[ $textproces -eq 0 ]]
then
echo "Text Processing has ran successfully"
exec $listgen
if [[ $listgen -eq 0 ]]
then
echo "List Generation has ran successfully"
exec $pagegen
if [[ $pagegen -eq 0 ]]
then
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
else
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
The methods I have tried: cat filename, export, source and standard assigning. The idea is that it is to work without having to give a path name, all of these shells are in the same directory anyway. All help is vastly appreciated!
NOTE: I know its horrifying, but it was the way I thought of doing it with the IF statements
bash variables sh filenames textwrapping
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.
– Kamil Cuk
Nov 26 '18 at 9:00
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
What is the content of$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why usingsource
or.
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is[[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified insideexec
call? If it contains any whitespace character, the syntax is just invalid. I think you meanif (( $? == 0 ))
or similar
– Kamil Cuk
Nov 26 '18 at 10:15
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script calledtextcreator.sh
with the contenta=1;
and you want to see the variablea
being set in yourrunsystem
script, right? Would that be an MCVE for your case?
– Kamil Cuk
Nov 26 '18 at 10:19
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18
|
show 1 more comment
I am trying to assign bash scripts to variables for use in a wrapper script, yet every method I have tried has resulted to a "No such file or directory" error:
#!/bin/bash
textgen=$(< textcreator.sh)
tablegen=$(< tablecreator.sh)
htmlgen=$(< webcreator.sh)
exec $textproces
if [[ $textproces -eq 0 ]]
then
echo "Text Processing has ran successfully"
exec $listgen
if [[ $listgen -eq 0 ]]
then
echo "List Generation has ran successfully"
exec $pagegen
if [[ $pagegen -eq 0 ]]
then
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
else
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
The methods I have tried: cat filename, export, source and standard assigning. The idea is that it is to work without having to give a path name, all of these shells are in the same directory anyway. All help is vastly appreciated!
NOTE: I know its horrifying, but it was the way I thought of doing it with the IF statements
bash variables sh filenames textwrapping
I am trying to assign bash scripts to variables for use in a wrapper script, yet every method I have tried has resulted to a "No such file or directory" error:
#!/bin/bash
textgen=$(< textcreator.sh)
tablegen=$(< tablecreator.sh)
htmlgen=$(< webcreator.sh)
exec $textproces
if [[ $textproces -eq 0 ]]
then
echo "Text Processing has ran successfully"
exec $listgen
if [[ $listgen -eq 0 ]]
then
echo "List Generation has ran successfully"
exec $pagegen
if [[ $pagegen -eq 0 ]]
then
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
else
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
else
echo "Error: Text Processing was unsuccessful"
exit 1
fi
The methods I have tried: cat filename, export, source and standard assigning. The idea is that it is to work without having to give a path name, all of these shells are in the same directory anyway. All help is vastly appreciated!
NOTE: I know its horrifying, but it was the way I thought of doing it with the IF statements
bash variables sh filenames textwrapping
bash variables sh filenames textwrapping
edited Nov 26 '18 at 10:09
Daniel Jameson
asked Nov 26 '18 at 8:47
Daniel JamesonDaniel Jameson
11
11
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.
– Kamil Cuk
Nov 26 '18 at 9:00
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
What is the content of$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why usingsource
or.
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is[[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified insideexec
call? If it contains any whitespace character, the syntax is just invalid. I think you meanif (( $? == 0 ))
or similar
– Kamil Cuk
Nov 26 '18 at 10:15
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script calledtextcreator.sh
with the contenta=1;
and you want to see the variablea
being set in yourrunsystem
script, right? Would that be an MCVE for your case?
– Kamil Cuk
Nov 26 '18 at 10:19
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18
|
show 1 more comment
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.
– Kamil Cuk
Nov 26 '18 at 9:00
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
What is the content of$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why usingsource
or.
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is[[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified insideexec
call? If it contains any whitespace character, the syntax is just invalid. I think you meanif (( $? == 0 ))
or similar
– Kamil Cuk
Nov 26 '18 at 10:15
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script calledtextcreator.sh
with the contenta=1;
and you want to see the variablea
being set in yourrunsystem
script, right? Would that be an MCVE for your case?
– Kamil Cuk
Nov 26 '18 at 10:19
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.– Kamil Cuk
Nov 26 '18 at 9:00
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.– Kamil Cuk
Nov 26 '18 at 9:00
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
What is the content of
$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why using source
or .
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is [[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified inside exec
call? If it contains any whitespace character, the syntax is just invalid. I think you mean if (( $? == 0 ))
or similar– Kamil Cuk
Nov 26 '18 at 10:15
What is the content of
$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why using source
or .
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is [[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified inside exec
call? If it contains any whitespace character, the syntax is just invalid. I think you mean if (( $? == 0 ))
or similar– Kamil Cuk
Nov 26 '18 at 10:15
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script called
textcreator.sh
with the content a=1;
and you want to see the variable a
being set in your runsystem
script, right? Would that be an MCVE for your case?– Kamil Cuk
Nov 26 '18 at 10:19
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script called
textcreator.sh
with the content a=1;
and you want to see the variable a
being set in your runsystem
script, right? Would that be an MCVE for your case?– Kamil Cuk
Nov 26 '18 at 10:19
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18
|
show 1 more comment
2 Answers
2
active
oldest
votes
It actually depends upon which directory you were in when you executed the root script. So them being in the same directory does not help. You can cd
into the directory containing the sub scripts before executing them from the root script.
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
Ashell
is not the same thing as ascript
. Each process ( or a script in execution ) has acurrent working directory
. Its this directory that the program will look for if you reference any other script.
– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
add a comment |
There are several serious problems with the code in the question. Some are identified by Shellcheck. Always run Shellcheck on new and modified code.
Problems include:
- The variables assigned at the start are not the variables that are used later.
- The variables are set to the contents of the programs, not the names of the files containing the programs. (The only way to use the contents would be to pass them to
eval
. That would not be a good idea.)
exec
is used to try to run the programs.exec
replaces the current program with a new program, determined by its arguments. It's roughly equivalent to running another program and exiting immediately afterwards. Onceexec
has been used, no code after it in the program is executed.- The paths to the programs to be run are not specified. Since the programs are in the current directory (
.
), and.
is usually not in thePATH
(for very good reasons), the shell will issue 'command not found` errors for the attempts to run them. - The code tries to get the exit status of programs by checking the value of the variable containing the program name. The exit status of a program can be obtained via the
$?
built-in variable. However, the simplest way to check if a command failed is to use it directly in anif
.
The Shellcheck-clean code below attempts to fix the problems. It also eliminates the nested if
statements in an attempt to make the code clearer.
#!/bin/bash
textgen=textcreator.sh
listgen=tablecreator.sh
pagegen=webcreator.sh
if ! "./$textgen" ; then
echo "Error: Text Processing was unsuccessful"
exit 1
fi
echo "Text Processing has ran successfully"
if ! "./$listgen" ; then
echo "Error: List Generation was unsuccessful"
exit 1
fi
echo "List Generation has ran successfully"
if ! "./$pagegen" ; then
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
The code could be used in various other ways (e.g. send error output to standard eror (... >&2
), reduce duplication by using a function to output the error message and exit the program).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477437%2fassigning-scripts-to-variables-for-a-wrapper-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It actually depends upon which directory you were in when you executed the root script. So them being in the same directory does not help. You can cd
into the directory containing the sub scripts before executing them from the root script.
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
Ashell
is not the same thing as ascript
. Each process ( or a script in execution ) has acurrent working directory
. Its this directory that the program will look for if you reference any other script.
– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
add a comment |
It actually depends upon which directory you were in when you executed the root script. So them being in the same directory does not help. You can cd
into the directory containing the sub scripts before executing them from the root script.
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
Ashell
is not the same thing as ascript
. Each process ( or a script in execution ) has acurrent working directory
. Its this directory that the program will look for if you reference any other script.
– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
add a comment |
It actually depends upon which directory you were in when you executed the root script. So them being in the same directory does not help. You can cd
into the directory containing the sub scripts before executing them from the root script.
It actually depends upon which directory you were in when you executed the root script. So them being in the same directory does not help. You can cd
into the directory containing the sub scripts before executing them from the root script.
answered Nov 26 '18 at 8:56
GautamGautam
1,12329
1,12329
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
Ashell
is not the same thing as ascript
. Each process ( or a script in execution ) has acurrent working directory
. Its this directory that the program will look for if you reference any other script.
– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
add a comment |
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
Ashell
is not the same thing as ascript
. Each process ( or a script in execution ) has acurrent working directory
. Its this directory that the program will look for if you reference any other script.
– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
I'm confused in what you mean by this. The root shell and sub shells are all in the same directory and the root shell is also ran on the same directory, is this what you mean?
– Daniel Jameson
Nov 26 '18 at 9:01
A
shell
is not the same thing as a script
. Each process ( or a script in execution ) has a current working directory
. Its this directory that the program will look for if you reference any other script.– Gautam
Nov 26 '18 at 9:06
A
shell
is not the same thing as a script
. Each process ( or a script in execution ) has a current working directory
. Its this directory that the program will look for if you reference any other script.– Gautam
Nov 26 '18 at 9:06
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
I'm understanding what you mean now, so I checked each scripts working directory but they are all in the same one. Thank you for the suggestion though, I can see why that would be an issue
– Daniel Jameson
Nov 26 '18 at 9:16
add a comment |
There are several serious problems with the code in the question. Some are identified by Shellcheck. Always run Shellcheck on new and modified code.
Problems include:
- The variables assigned at the start are not the variables that are used later.
- The variables are set to the contents of the programs, not the names of the files containing the programs. (The only way to use the contents would be to pass them to
eval
. That would not be a good idea.)
exec
is used to try to run the programs.exec
replaces the current program with a new program, determined by its arguments. It's roughly equivalent to running another program and exiting immediately afterwards. Onceexec
has been used, no code after it in the program is executed.- The paths to the programs to be run are not specified. Since the programs are in the current directory (
.
), and.
is usually not in thePATH
(for very good reasons), the shell will issue 'command not found` errors for the attempts to run them. - The code tries to get the exit status of programs by checking the value of the variable containing the program name. The exit status of a program can be obtained via the
$?
built-in variable. However, the simplest way to check if a command failed is to use it directly in anif
.
The Shellcheck-clean code below attempts to fix the problems. It also eliminates the nested if
statements in an attempt to make the code clearer.
#!/bin/bash
textgen=textcreator.sh
listgen=tablecreator.sh
pagegen=webcreator.sh
if ! "./$textgen" ; then
echo "Error: Text Processing was unsuccessful"
exit 1
fi
echo "Text Processing has ran successfully"
if ! "./$listgen" ; then
echo "Error: List Generation was unsuccessful"
exit 1
fi
echo "List Generation has ran successfully"
if ! "./$pagegen" ; then
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
The code could be used in various other ways (e.g. send error output to standard eror (... >&2
), reduce duplication by using a function to output the error message and exit the program).
add a comment |
There are several serious problems with the code in the question. Some are identified by Shellcheck. Always run Shellcheck on new and modified code.
Problems include:
- The variables assigned at the start are not the variables that are used later.
- The variables are set to the contents of the programs, not the names of the files containing the programs. (The only way to use the contents would be to pass them to
eval
. That would not be a good idea.)
exec
is used to try to run the programs.exec
replaces the current program with a new program, determined by its arguments. It's roughly equivalent to running another program and exiting immediately afterwards. Onceexec
has been used, no code after it in the program is executed.- The paths to the programs to be run are not specified. Since the programs are in the current directory (
.
), and.
is usually not in thePATH
(for very good reasons), the shell will issue 'command not found` errors for the attempts to run them. - The code tries to get the exit status of programs by checking the value of the variable containing the program name. The exit status of a program can be obtained via the
$?
built-in variable. However, the simplest way to check if a command failed is to use it directly in anif
.
The Shellcheck-clean code below attempts to fix the problems. It also eliminates the nested if
statements in an attempt to make the code clearer.
#!/bin/bash
textgen=textcreator.sh
listgen=tablecreator.sh
pagegen=webcreator.sh
if ! "./$textgen" ; then
echo "Error: Text Processing was unsuccessful"
exit 1
fi
echo "Text Processing has ran successfully"
if ! "./$listgen" ; then
echo "Error: List Generation was unsuccessful"
exit 1
fi
echo "List Generation has ran successfully"
if ! "./$pagegen" ; then
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
The code could be used in various other ways (e.g. send error output to standard eror (... >&2
), reduce duplication by using a function to output the error message and exit the program).
add a comment |
There are several serious problems with the code in the question. Some are identified by Shellcheck. Always run Shellcheck on new and modified code.
Problems include:
- The variables assigned at the start are not the variables that are used later.
- The variables are set to the contents of the programs, not the names of the files containing the programs. (The only way to use the contents would be to pass them to
eval
. That would not be a good idea.)
exec
is used to try to run the programs.exec
replaces the current program with a new program, determined by its arguments. It's roughly equivalent to running another program and exiting immediately afterwards. Onceexec
has been used, no code after it in the program is executed.- The paths to the programs to be run are not specified. Since the programs are in the current directory (
.
), and.
is usually not in thePATH
(for very good reasons), the shell will issue 'command not found` errors for the attempts to run them. - The code tries to get the exit status of programs by checking the value of the variable containing the program name. The exit status of a program can be obtained via the
$?
built-in variable. However, the simplest way to check if a command failed is to use it directly in anif
.
The Shellcheck-clean code below attempts to fix the problems. It also eliminates the nested if
statements in an attempt to make the code clearer.
#!/bin/bash
textgen=textcreator.sh
listgen=tablecreator.sh
pagegen=webcreator.sh
if ! "./$textgen" ; then
echo "Error: Text Processing was unsuccessful"
exit 1
fi
echo "Text Processing has ran successfully"
if ! "./$listgen" ; then
echo "Error: List Generation was unsuccessful"
exit 1
fi
echo "List Generation has ran successfully"
if ! "./$pagegen" ; then
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
The code could be used in various other ways (e.g. send error output to standard eror (... >&2
), reduce duplication by using a function to output the error message and exit the program).
There are several serious problems with the code in the question. Some are identified by Shellcheck. Always run Shellcheck on new and modified code.
Problems include:
- The variables assigned at the start are not the variables that are used later.
- The variables are set to the contents of the programs, not the names of the files containing the programs. (The only way to use the contents would be to pass them to
eval
. That would not be a good idea.)
exec
is used to try to run the programs.exec
replaces the current program with a new program, determined by its arguments. It's roughly equivalent to running another program and exiting immediately afterwards. Onceexec
has been used, no code after it in the program is executed.- The paths to the programs to be run are not specified. Since the programs are in the current directory (
.
), and.
is usually not in thePATH
(for very good reasons), the shell will issue 'command not found` errors for the attempts to run them. - The code tries to get the exit status of programs by checking the value of the variable containing the program name. The exit status of a program can be obtained via the
$?
built-in variable. However, the simplest way to check if a command failed is to use it directly in anif
.
The Shellcheck-clean code below attempts to fix the problems. It also eliminates the nested if
statements in an attempt to make the code clearer.
#!/bin/bash
textgen=textcreator.sh
listgen=tablecreator.sh
pagegen=webcreator.sh
if ! "./$textgen" ; then
echo "Error: Text Processing was unsuccessful"
exit 1
fi
echo "Text Processing has ran successfully"
if ! "./$listgen" ; then
echo "Error: List Generation was unsuccessful"
exit 1
fi
echo "List Generation has ran successfully"
if ! "./$pagegen" ; then
echo "Error: HTML Page Generation was unsuccessful"
exit 1
fi
echo "HTML Page Generation has ran successfully"
echo "The command wrapper has finished successfully"
exit 0
The code could be used in various other ways (e.g. send error output to standard eror (... >&2
), reduce duplication by using a function to output the error message and exit the program).
answered Nov 26 '18 at 20:17
pjhpjh
1,844613
1,844613
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477437%2fassigning-scripts-to-variables-for-a-wrapper-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
wrapper script
. Can you show the workflow? How is the wrapper script called? From where? Using what? How do you run this? Using what command? And why your variables are not quoted and ,more concerning, why do you use exec(!)? Show an example, an MCVE, with all the relevant information, so I can reproduce an issue for example on external sites.– Kamil Cuk
Nov 26 '18 at 9:00
I have made the changes now. The wrapper script is called from either 'bash runsystem' or './runsystem'. It is called from a directory in my user files, all the scripts it uses are in that directory as well. They're not quoted as I assumed you wouldn't quote the filename as I have seen that before. I assumed exec would be suitable as I was not sure how to run a file with a variable name
– Daniel Jameson
Nov 26 '18 at 10:12
What is the content of
$textproces
? And why don't you quote it? Running it with exec is extremely dangerous. What are you trying to achieve? Why usingsource
or.
is not suited for you? You don't quote filenames, you should quote the content of the files when running with exec, unless you quote the files content (I think you don't). And what is[[ $textproces -eq 0 ]]
supposed to do? Textprocess is text, moreover it's not quoted, is it modified insideexec
call? If it contains any whitespace character, the syntax is just invalid. I think you meanif (( $? == 0 ))
or similar– Kamil Cuk
Nov 26 '18 at 10:15
Ok, so let's keep that aside, and concentrate on the goal. You want to have a script called
textcreator.sh
with the contenta=1;
and you want to see the variablea
being set in yourrunsystem
script, right? Would that be an MCVE for your case?– Kamil Cuk
Nov 26 '18 at 10:19
Forgive me for asking a stupid question, but why are you assigning the entire text of the scripts to the variables instead of just the script names?
– Paul Hodges
Nov 26 '18 at 15:18