【.sh script】

shell - draft notes


foreword

解析

#!/bin/bash
set -x
source /etc/profile

source /home/zhangsan/anaconda3/pkgs/conda-4.4.10-py36_0/etc/profile.d/conda.sh
# ./cheshi.sh

cd '/home/zhangsan/file1'

conda activate tf3.6
python exercise.py

[ source /etc/profile-----The role of the file
After using source /etc/profile to save the configuration under linux, the new environment variable can only be valid in one terminal.

Problem description:
After adding environment variables in /etc/profile, it can only take effect on the current terminal after compiling with source /etc/profile;

After restarting a terminal, the environment variable becomes invalid.

Solution:
restart the system: reboot, the problem is solved;

Because the environment variable set does not really take effect, it just uses the source command to make it run temporarily.


source /home/zhangsan/anaconda3/pkgs/conda-4.4.10-py36_0/etc/profile.d/conda.sh— The origin of the file is to view the current conda.sh and activate it (locate conda.sh to view the specific path of the current conda)


cd '/home/zhangsan/file1'---- switch the rul of file1


conda activate tf3.6 python exercise.py— activate the environment and run


vi ~./condarc

Enter condarc to load the image
Environment variable setting method:
1. /etc/profile: It is the first file used by the operating system to customize the user environment. This file sets the environment information for each user of the system. When the user logs in for the first time , the file is executed.
2. /etc/environment: The second file used by the operating system when logging in. The system sets environment variables before reading your own profile.
3. ~/.bash_profile: The third file used when logging in is the .profile file. Each user can use this file to enter the shell information dedicated to their own use. When the user logs in, the file is only executed once. By default, some environment variables are set and the user's .bashrc file is executed.
/etc/bashrc: Execute this file for every user who runs a bash shell. This file is read when the bash shell is opened.
4. ~/.bashrc: This file contains bash information specific to your bash shell. This file is read when you log in and every time you open a new shell.


Set permanent environment variables
1. In the configuration of environment variables, first delete the three lines in .bash_profile about the definition of .bashrc, and then configure the environment variables in .bashrc
2. Select the java environment to be used: update-alternatives –config java
3. To make the newly modified environment variable take effect: source .bashrc
4. Check the environment variable: env
can be placed in /etc/bash/bashrc, which is system-level

Note: The difference between the source command and shell scripts is: source executes commands in the current bash environment, while scripts starts a subshell to execute commands. In this way, if the command for setting environment variables (or alias, etc.) is written into scripts, it will only affect the subshell and cannot change the current BASH, so when setting environment variables through a file (command line), use the source command.
———————————————————————— ------------------------------------

Know what I can, what I can do is perfect.
Know your boundaries stay humble and don't let one direction limit you.
Those who know what I can't, what I can't, have an open mind.

Guess you like

Origin blog.csdn.net/qq_42700796/article/details/127221498