How to send BASH variables to multiple scripts? [duplicate] - bash

This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 8 years ago.
I have many BASH scripts called in sequence, e.g., script1.sh contains:
#!/bin/bash
bash script2.sh
bash script3.sh
bash script4.sh
script2.sh contains:
#!/bin/bash
file_a="1.txt"
cp $file_a /tmp/$file_a.tmp
script3.sh contains:
#!/bin/bash
wc -l /tmp/$file_a.tmp
script4.sh contains:
#!/bin/bash
cat /tmp/2.txt $file_a.tmp > file3.txt
Each file requires access to a small collection of variables. How can I pass the variables from one script onto the next?

You have many options.
The first method would be making the variable as the environment variable and pass to the script before the second script get executed.
The second method would be making the second script to run in the same shell.
The methods are described well here with the examples.

Related

Printing arrays are not working in shell script [duplicate]

This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]

evaluating expression with bash [duplicate]

This question already has answers here:
What's the point of eval/bash -c as opposed to just evaluating a variable?
(3 answers)
The 'eval' command in Bash and its typical uses
(11 answers)
Closed 2 years ago.
There is something i do not understand with strings in bash:
Look at this script:
#!/bin/bash
tmp="ls"
"$tmp"
This script executes ls command and display result in the console.
Now look at this script:
#!/bin/bash
tmp="ls > out.txt"
"$tmp"
This second script does not execute ls and displays this error:
line 3: ls > out.txt: command not found
I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.
Thanks

source command output lines [duplicate]

This question already has answers here:
How to echo shell commands as they are executed
(14 answers)
Closed 4 years ago.
I want to source a file containing several commands in a bash shell. How can I have the currently executed command printed on top of the commands output?
E.g. given this file test.sh
echo "hello"
echo "world"
the output of source test.sh should be:
echo "hello"
hello
echo "world"
world
Type set -x before source test.sh. This tells the shell to show the commands that are being executed before executing them.
Type set +x to undo it afterwards.

Get current directory of file after getting called by another bash script [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 9 years ago.
So I have one bash script which calls another bash script.
The second script is in a different folder.
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
In this second script it has the line src=$(pwd) and since I'm calling that script from another script in a different directory, the $(pwd) returns the current directory of the first script.
Is there any way to get the current directory of the second script using a simple command within that script without having to pass a parameter?
Thanks.
I believe you are looking for ${BASH_SOURCE[0]}, readlinkand dirname (though you can use bash string substitution to avoid dirname)
[jaypal:~/Temp] cat b.sh
#!/bin/bash
./tp/a.sh
[jaypal:~/Temp] pwd
/Volumes/Data/jaypalsingh/Temp
[jaypal:~/Temp] cat tp/a.sh
#!/bin/bash
src=$(pwd)
src2=$( dirname $( readlink -f ${BASH_SOURCE[0]} ) )
echo "$src"
echo "$src2"
[jaypal:~/Temp] ./b.sh
/Volumes/Data/jaypalsingh/Temp
/Volumes/Data/jaypalsingh/Temp/tp/
Please try this to see if it helps
loc=`dirname $BASH_SOURCE`

bash script and command definition [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Closed 3 years ago.
Got a little bash script like so:
#!/bin/bash
TIME_CMD='/usr/bin/time -f "%E execution time"'
${TIME_CMD} ls
Only problem: doesn't work:
/usr/bin/time: cannot run execution: No such file or directory
Command exited with non-zero status 127
"0:00.00
What am I doing wrong?
Try making it...
#!/bin/bash
TIME_CMD='/usr/bin/time -f "%E execution time"'
eval "$TIME_CMD ls"
This will utilize bash to re-parse the command string after it has been constructed, so that the quoted argument will be recognized properly.
Storing commands in variables is generally a bad idea (see BashFAQ #050 for details). The reason it's not working as you expect is that quoting inside variable values is ignored (unless you run it through something like eval, which then tends to lead to other parsing oddities).
In your case, I see three fairly straightforward ways to do it. First, you can use an alias instead of a variable:
alias TIME_CMD='/usr/bin/time -f "%E execution time"'
TIME_CMD ls
Second, you can use a function:
TIME_CMD() { /usr/bin/time -f "%E execution time" "$#"; }
TIME_CMD ls
Third, you can use an array rather than a simple variable:
TIME_CMD=(/usr/bin/time -f "%E execution time")
"${TIME_CMD[#]}" ls
Note that with an array, you need to expand it with the "${array[#]}" idiom to preserve word breaks properly.

Resources