source command output lines [duplicate] - bash

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.

Related

Bash create file script with echo command [duplicate]

This question already has answers here:
Multi-line string with extra space (preserved indentation)
(12 answers)
Closed 1 year ago.
I need to generate the following file content with the bash script.
#!/bin/sh
script_dir=$(dirname "$(realpath $0)")
export LD_LIBRARY_PATH=$script_dir/lib
exec $script_dir/{EXEC_FILE_WITH_EXT}
i.e. something like
my_script_generator.sh
#!/bin/sh
echo "<filecontent above>" > new_script.sh
The problem's a make all screening correctly to avoid echo evaluate the content and put it just as is.
For the more huge script, it becomes a problem.
Is there an online service that simplifies that work?
Use cat+here-document and backslash before $ signs you dont't want to be evaluated.
#!/bin/sh
cat > new_script.sh <<EOF
#!/bin/sh
script_dir=\$(dirname "\$(realpath \$0)")
export LD_LIBRARY_PATH=\$script_dir/lib
exec \$script_dir/{EXEC_FILE_WITH_EXT}
EOF
A script like this (not bullet proof) could help automate the task
#!/bin/sh
# make-script.sh
# input : a sequence of commands
# output : sequence of commands which generates a copy of the source script file
echo "cat <<EOF"
echo "#!/bin/sh"
echo
sed "s/\\\$/\\\\\\\$/g"
echo "EOF"

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

what does the below shell script mean? [duplicate]

This question already has answers here:
What do $? $0 $1 $2 mean in shell script? [duplicate]
(2 answers)
Closed 5 years ago.
I have seen one shell script starting with the below code :-
#!/bin/bash
currfoldername=$1
cd $currfoldername
can anyone describe what does $1 mean here?
Thanks for your reply!!
$1 means the first argument given while executing the shell script.
Example -
# my_script.sh
#!/bin/bash
currfoldername=$1
cd $currfoldername
echo "in $currfoldername"
execute -
./my_script.sh my_folder
output -
# value of variable currfoldername is my_folder.
#cd to my_folder
in my_folder # echo statement

Why "echo <<EOF" not working as expected [duplicate]

This question already has answers here:
Bash here document produces no output, any idea why?
(2 answers)
Closed 5 years ago.
I am trying to understand the bash's here document feature. Below code works as expected and returns "abc" to terminal. If I replace the program cat by echo I do not see any output. Why I am not able to pass here document to echo ? Is it becuase it is a bash builtin ?
cat <<EOF
abc
EOF
"abc" is output to the terminal as expected.
No output for below comamnd though-
echo <<EOF
abc
EOF
You want:
cat <<EOF
abc
EOF
Otherwise, what you're doing is just running echo with its stdin connected to a temporary file having abc in it. Since echo doesn't read stdin, it never finds out if there's contents waiting to be read from there or not.

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

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.

Resources