Check if script was started in current shell [duplicate] - shell

This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 8 years ago.
Is there a way to check within a shell script (ksh) whether or not the script was started in the current shell?
Example
Start script in current shell with . (dot/source) command
$ . ./myscript
$ I run in the current environment!
Start script in own process
$ ./myscript
$ I run in my own process!

This is a simple trick you can use.
#!/bin/ksh
if [ ${.sh.file} != ${0} ]; then
echo I run in the current environment
else
echo I run in my own process
fi

Every Shell Has its Own PID ..
so you can use echo "$$" in ur script ..it will helps us find from where the Script is RAN .
i.e Difference in pid means they are run from different shells .

Related

Bash script doesn't set environment variable correctly [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to set environment variables in a bash script to be read by another bash script, but they are not getting set properly. I am on Ubuntu 20.04.
setting environment variables in a script:
setenv.env
export DB1_IMAGE="postgres:latest"
run it: . setenv.env
test it: echo $DB1_IMAGE
result: postgres:latest
script to test the environment variable value:
test.sh
#!/bin/bash
echo $DB1_IMAGE
if [[ $DB1_IMAGE == "postgres:latest" ]]
then
echo "equals"
else
echo "not equals"
fi
run the test script: . test.sh
result:
postgres:latest
not equals
now set the environment variable with command line:
export DB1_IMAGE="postgres:latest"
now run the test script again: . test.sh
result:
postgres:latest
equals
Summary: When an environment variable is set with a bash script, that value will fail an equals comparison in another bash script. When that same environment variable is set with a command line, it passes the equals test. I can't explain why this is. I feel like I'm missing something obvious. How could the == test fail? Are there unprintable characters being inserted somehow? Please help..
Thanks to #glennjackman, the cause of this was that the bash script file (setenv.env) was DOS-formatted as opposed to UNIX-formatted. This means it had \r\n line breaks, which cause hidden characters to be inserted into the environment variables. The fix is to run dos2unix on the file (sudo apt install dos2unix)

Log in to another server and run commands - Using a script [duplicate]

This question already has answers here:
What is the cleanest way to ssh and run multiple commands in Bash?
(14 answers)
bash script execute commands after ssh
(1 answer)
How to use SSH to run a local shell script on a remote machine?
(23 answers)
When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server?
(3 answers)
Closed 3 years ago.
I want to run a script a.sh that will do the following steps -
Run few commands
Call another shell script b.sh in the same server - server01
b.sh will login to another server - server02 and run the commands that follow.
I am able to do till step 2 correctly. In step 3, I am able to loin to another server but it stops there. It does not run the steps that follow.
Have a look at the two scripts.
a.sh
cd ~/sample/home && python helloworld.py
#!/bin/bash
cd ~/sample/ && ./b.sh
b.sh
ssh username#server02.com
echo "In server02"
Both a.sh and b.sh are in the same server, that is server01.com. Here, I want a.sh to run in server01 and then b.sh to run in server01. Once b.sh runs, it should do ssh to server02 and print "In server02".
I am able to do till ssh to server02. After that it is not printing "In server02" in server02.
Is there a way to do it?
Use a heredoc.
ssh username#server02.com << EOF
echo "In server02"
EOF

Effective Methods of changing Shells in UNIX

I used to work with UNIX a couple years ago, and I am just starting to get back into it again. I was wondering if anyone could help me with a question.
For example, if I am in bash, I say chsh --shell /bin/tcsh after this I am prompted to enter my password. If I try to say echo $SHELL it will not tell me I have changed shells. It still tells me I am in bash, not C shell. So I have to exit and restart. Once I log back it, then it tells I am in C shell.
Is there a more effective method to change shells? One that does not require me having to log in and out?
Thank you in advance.
chsh(1): change your login shell
Once you change your shell with chsh, it should automatically login to that shell every time you open a terminal.
If you want to use a different shell temporary, just run that shell directly: "tcsh", "zsh", etc..
If you want to use a particular shell for a script use shebang "#!".
Example -- The following on the first line of a shell script will ensure the script is run with sh (and you can do this for any shell available on your system):
#!/bin/sh
Always check your current shell by using :
echo $0
That way you will get the exact process ( your current shell ) you are running. If you print $SHELL it will return to you the default shell that will be open when you login to the server which unless that's what you need its not reliable.
ubuntu$ echo $SHELL
/bin/bash
ubuntu$ echo $0
-bash
ubuntu$ sh
\[\e[31m\]\u\[\e[m\]$ echo $SHELL
/bin/bash
\[\e[31m\]\u\[\e[m\]$ echo $0
sh
\[\e[31m\]\u\[\e[m\]$
Regards!

Bash: Set up a new command on a new line without executing [duplicate]

This question already has an answer here:
How to prefill command line input
(1 answer)
Closed 6 years ago.
I'm trying to write a BASH script to output a partially completed command which I can then add parameters to, hit ENTER and then run. I want this to be implemented completely in BASH.
e.g.
~> ./test.sh
~> ls -al <CURSOR POSITION HERE>
The only variable I've found that's close is the PROMPT_COMMAND variable, which when set inside test.sh to 'ls -al', will then immediately execute it once the script has exited.
Is there a way to stop the immediate execution, so I can add, say, *.log?
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

Variable is not getting exported [duplicate]

This question already has an answer here:
bash - export doesn't work
(1 answer)
Closed 7 years ago.
I am running the following simple code in a shell script , but it seems like it cant export the variable :
#!/bin/bash
echo -n "Enter AWS_ACCESS_KEY_ID: "
read aws_access_key
export AWS_ACCESS_KEY_ID=$aws_access_key
After that I take the input from the user ,but when I run echo $AWS_ACCESS_KEY_ID I get a blank value .
Run your script in the current shell by using:
source your-script # this runs your-script in the existing shell
...or, if using a POSIX shell...
. your-script # likewise; that space is intentional!
not
./your-script # this starts a new shell just for `your-script`; its variables
# are lost when it exits!
...if you want variables it sets to be available to the shell that calls it.
To be clear, export puts a variable in the current process's environment -- but environment variables are propagated down to child processes, not up to parent processes.
Now, if your goal is to define an interactive command that's easy to call, you might want to consider an entirely different approach altogether -- putting a function in your .bashrc:
awsSetup() {
echo -n "Enter AWS_ACCESS_KEY_ID: "
read && [[ $REPLY ]] && export AWS_ACCESS_KEY_ID=$REPLY
}
...after which the user with this in their .bashrc can run awsSetup, which will run in the current shell.

Resources