Linux bash know the script path which includes library script [duplicate] - linux

This question already has answers here:
How do I know the script file name in a Bash script?
(25 answers)
Closed 7 years ago.
I have a library script named A and a script B, C which includes A with
. ../../../A
The problem is how A can know which time I run ./B.sh or ./C.sh, example:
if(run ./B.sh)
echo "B (file path) is calling"
else
echo "C (file path) is calling"

You can use $0 to determine the command that was executed:
A.sh:
echo $0
B.sh:
. ./A.sh
When run:
$ sh B.sh
B.sh
$ sh A.sh
A.sh
It will only give the command that was executed, not the arguments:
$ sh B.sh one two three
B.sh

Related

Linux variable value not working when running from script [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
This is MyScript.sh
I want to execute a script as a text inside my script, I have tried to do it like so:
bash -c "
#!/bin/bash
STR=6
echo $STR
"
Prints empty line.
I tried replacing bash -c with sh -c or eval, all options acts the same, why is that and how can it be solved?
Using single quotes to avoid interpretation in the current shell:
$ cat myscript.sh
bash -c 'STR=6; echo $STR'
./myscript.sh
6

why `sh -c "export a=1&&echo $a"` returns nothing? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
A)
first I run export a=1&&echo $a in linux terminal and I get 1.
[root#ip-172-31-16-92 ec2-user]# export a=1&&echo $a
1
B)
But when I run sh -c "export a=2&&echo $a" and I still get 1 instead of 2.
[root#ip-172-31-16-92 ec2-user]# sh -c "export a=2&&echo $a"
1
What happend? If sh -c "export a=2&&echo $a" itself is a child process of the terminal,it should have its own environment, first it should make a copy of his father environment,so a=1,but when it execute export a=2 ,environment a should be set to 2,then echo $a should be 2.But it returns 1,what happend?
Try this one and and you'll see it works.
sh -c 'export a=2&&echo $a'
When enclosed in double-quotes, $a is expanded to its current value by the shell before invoking sh.

Pipe echo of change the current directory to sh does not work [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 4 years ago.
If I do the code:
echo "printf 'working'" | sh
the code prints out working
but when I want to change the current directory this way:
echo "cd ../" | sh
the current directory isn't changed.
Do you know the reason behind that behavior?
do you know how to echo cd command to sh in a working way?
echo "cd /" | sh
actually creates 2 new processes: echo, and sh. The sh process most probably does change the directory, but then just exits. You could test this by
echo "cd ../; touch Jimmix_was_here" | sh
ls -l ../Jimmix_was_here
which should show empty file Jimmix_was_here file, with current timestamp (if you had write permission to the parent directory; otherwise the first command would throw error.)
There's no way to change current directory of a process from within a child; after all if it was possible, it would be a security hole!
Note: this reminds me of a seemingly paradoxical fact: why /bin/cd exists?
Note 2: Try pstree | cat and find both pstree and cat--they are siblings!

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

Difference between executing a script with 'bash cd.sh' and 'source cd.sh'? [duplicate]

This question already has answers here:
What is the difference between using `sh` and `source`?
(5 answers)
Closed 7 years ago.
Explain the difference between executing a script with bash cd.sh and source cd.sh
cd.sh contains:
#!/bin/sh
cd /tmp
bash execute the script in a child shell that cannot modify the environment of the invoking shell while source executes the script in the current shell:
test.sh
#!/bin/sh
export MY_NAME=chucksmash
echo $MY_NAME
Running test.sh:
chuck#precision:~$ bash test.sh
chucksmash
chuck#precision:~$ echo $MY_NAME
chuck#precision:~$ source test.sh
chucksmash
chuck#precision:~$ echo $MY_NAME
chucksmash
chuck#precision:~$
In bash, commands that look like source script.sh (or . script.sh) run the script in the current shell, regardless of the #! line.
Therefore, if you have a script (named script.sh in this example):
#!/bin/bash
VALUE=1
cd /tmp
This would print nothing (because VALUE is null) and not change your directory (because the commands were executed in another instance of bash):
bash script.sh
echo $VALUE
This would print 1 and change your directory to /tmp:
source script.sh
echo $VALUE
If you instead had this script (named script.py in this example):
#!/usr/bin/env python
print 'Hello, world"
This would give a WEIRD bash error (because it tries to interpret it as a bash script):
source shell.py
This would *also *give a WEIRD bash error (because it tries to interpret it as a bash script):
bash shell.py
This would print Hello, world:
./shell.py # assuming the execute bit it set

Resources