Setup bash shell or command prompt to run Python project [duplicate] - linux

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
Want to execute the following commands from any single script. How to do this?
Whenever I'm starting my putty session, on my unix machine, bash shell is coming, then I need to enter the following commands to setup my workspace.
Below is the exact procedure which I'm doing.
-bash-4.2$ bash
[userName#SystemName ~]$ su
getting admin rights
[root#SystemName userName]#
setting up environment variable for python project
[root#SystemName userName]# source xxxx.env
setting the path for my local workspace
[root#SystemName userName]# setxxxx /home/userName/SourceCode/
Now, I want all these commands to get executed from single script.
Thus, I had putted all these commands into a single shell script and tried to execute but only first instruction got executed. Why?
start.sh
#!/bin/bash
bash
su
source ssdt.env
setssdt /home/userName/projectName/

The first command in your script is bash, which opens a new shell that will run until you exit from it, before it will continue executing the other commands in your script. If you're just trying to make the script execute with bash, that's what the #! line at the top does. The su command will also open a subshell that will have to exit before the other commands will be run with normal privileges.

Related

What is the difference between interactive and non-interactive SSH commands? [duplicate]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a command that runs fine if I ssh to a machine and run it, but fails when I try to run it using a remote ssh command like :
ssh user#IP <command>
Comparing the output of "env" using both methods resutls in different environments. When I manually login to the machine and run env, I get much more environment variables then when I run :
ssh user#IP "env"
Any idea why ?
There are different types of shells. The SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell. Description follows, from man bash:
A login shell is one whose first character of argument
zero is a -, or one started with the --login option.
An interactive shell is one started without non-option
arguments and without the -c option whose standard input
and error are both connected to terminals (as determined
by isatty(3)), or one started with the -i option. PS1 is
set and $- includes i if bash is interactive, allowing a
shell script or a startup file to test this state.
The following paragraphs describe how bash executes its
startup files. If any of the files exist but cannot be
read, bash reports an error. Tildes are expanded in file
names as described below under Tilde Expansion in the
EXPANSION section.
When bash is invoked as an interactive login shell, or as
a non-interactive shell with the --login option, it first
reads and executes commands from the file /etc/profile, if
that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that
order, and reads and executes commands from the first one
that exists and is readable. The --noprofile option may
be used when the shell is started to inhibit this behavĀ­
ior.
When a login shell exits, bash reads and executes commands
from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is
started, bash reads and executes commands from ~/.bashrc,
if that file exists. This may be inhibited by using the
--norc option. The --rcfile file option will force bash
to read and execute commands from file instead of
~/.bashrc.
When bash is started non-interactively, to run a shell
script, for example, it looks for the variable BASH_ENV in
the environment, expands its value if it appears there,
and uses the expanded value as the name of a file to read
and execute. Bash behaves as if the following command
were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search
for the file name.
How about sourcing the profile before running the command?
ssh user#host "source /etc/profile; /path/script.sh"
You might find it best to change that to ~/.bash_profile, ~/.bashrc, or whatever.
(As here (linuxquestions.org))
Shell environment does not load when running remote ssh command. You can edit ssh environment file:
vi ~/.ssh/environment
Its format is:
VAR1=VALUE1
VAR2=VALUE2
Also, check sshd configuration for PermitUserEnvironment=yes option.
I had similar issue, but in the end I found out that ~/.bashrc was all I needed.
However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :
#If not running interactively, don't do anything
[ -z "$PS1" ] && return
I found an easy resolution for this issue was to add
source /etc/profile
to the top of the script.sh file I was trying to run on the target system.
On the systems here, this caused the environmental variables which were needed by script.sh to be configured as if running from a login shell.
In one of the prior responses it was suggested that ~/.bashr_profile etc... be used.
I didn't spend much time on this but, the problem with this is if you ssh to a different user on the target system than the shell on the source system from which you log in it appeared to me that this causes the source system user name to be used for the ~.
Just export the environment variables you want above the check for a non-interactive shell in ~/.bashrc.

Bash script output not showing up during execution? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).

How to know the default shell process for my terminal? [duplicate]

This question already has answers here:
How to get the default shell
(5 answers)
Closed 6 years ago.
I am composing a bash script to serve as a utility tools.
The challenges I am facing now is:
- user using my tool will be running in bash environment
- however, some of them might default using krcsh or tcsh. they might have aliases or configurations set in there.
So, I need to prompt/guide user to resolve this during installation. My first challenge: How am I suppose to know the user's default shell within my install.sh?
Knowing the "default" shell, I can prompt and guide the user to do necessary transfer to bash.
my testing code:
my result:
1/ is fault obviously. It return the current shell which is my install.sh (bash)
2/ I am doubtful. It seems to be the history of what I have run before. It does not show me my default configured shell. My case, my terminal default shell is bash, and I run tsch for testing purpose. So the script parsed wrong information and will though my default shell is tcsh. It will then assist me to port configurations from tcsh to bash during the installation process.
If you want to check the shell you are using, you can use the following methods:
echo $0 in terminal will show you the program running if you want to check the shell you are currently using.
echo $SHELL - with this command you can read the user's default shell in the terminal you are running.
If you want to prompt, easily you can put the echo $SHELL in the part of your script where you need to show the current shell you are using.
Don't forget to put #!/bin/bash if your script is designed to run in a bash shell!

clarification about bash script [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am new to bash scripting. This might be obvious to many. please bear with me.
I have a shell script as follows:
#!/bin/bash
echo `pwd`
cd /home/foo/bar
echo `pwd`
Let's say I am currently in dir : /home/foo1
If I execute the above script it prints:
/home/foo1
/home/foo/bar
But once the script completes execution, I have seen that it still remains in dir /home/foo1
I have also seen some scripts where there are explicit commands to reset the working dir using 'cd -' command.
If bash executes all the lines in the script as commands, why does it again reset the working dir?
When you are running an interactive session of bash, and from it, you execute a script (e.g. ./myscript.sh), then bash creates a new bash process to execute the script. Initially, that process get copy of the same environment as the original process (e.g. the current working directory, or environment variables), but if the script modifies the environment somehow, this changes only affect the new process, not the original one. So when the scripts exits, you go back to the original process which retains the original environment. So it is not possible to modify the current directory of the original shell from a script.
As a side note, the following line
echo `pwd`
does not make much sense. You either have to do echo $PWD or simply pwd.

keep the shell script running even after moving to some other shell using script command without logging out from any terminal [duplicate]

This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 6 years ago.
I have a script with one of its command let say "cmd" this cmd will move the user from the shell to some other server/shell , now when I run this script after executing "cmd" the shell gets changed (normal behaviour) but the commands following the "cmd" will not run and when I logout from the new terminal to come back to original one then only that commands execute on their own .
Is there any way so that the remaining will execute on the new terminal ?
I'm assuming your "cmd" is ssh opening a session on another server. If thats the case you can instruct ssh to run a command once the session is open: ssh user#server.com your_command_here

Resources