Linux environment variables and command line parameters


Learning command line parameters is very helpful for us to learn environment variables
Moreover, learning command line parameters can give us a deeper understanding of the Linux operating system and main function.

1. Command line parameters

Command line parameters are the basis of Linux command options
Many languages ​​support this function,
because its function is to enable the same program to implement Different sub-functions
work great

It is precisely because of the existence of the syntax of command line parameters that there is
ls -a
ls -l
ls -d
ls -n
gcc -E/S/c -o -g -static
etc. Wait for the options for these commands!!!

So let’s get into the exploration of command line parameters and environment variables!

1. Grammar

You may have seen that one version of the main function has multiple formal parameters
Just like this

int main(int argc,char* argv[]);
argc是int类型的变量
argv是一个指针数组,只不过传参时因为数组降维而让argv这个形参成为了二级指针
不过无妨,我们依然可以把它当成一个指针数组来遍历

这个指针数组中存放的是char*类型的指针
这些指针指向的是字符串,因此用%s来打印即可

What are these two things?
Let’s print it out and take a look
Insert image description here
Insert image description here
argv[0] is what we entered./mycmd This string
In fact, I can also input it like this:
Insert image description here
In other words, this argv stores what we input when executing this executable program on the command line. string
argc is the number of valid data in this argv array

In fact, this argv ends with a NULL pointer
Let’s verify it
Because the literal value of NULL is 0, it can be verified like this< /span> Let’s take a look at this use So ​​what is the use? This is the syntax of command line parameters
Insert image description here
Insert image description here
Successful operation


2. Application 1: Simple Calculator

Implement a simple calculator that can perform different functions with options

Here we need to use a function:atoi
to convert the string into an integer
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Successful operation
This is how command options are implemented in Linux

2. Environment variables

1. The concept of environment variables

Insert image description here

2. The role of environment variables

We know:
To execute a program, you must first find the program
before you can create a process, form a PCB, load it into memory, and map the address space. Add it to the run queue
and then run it

But there is a question:
Why does our own program have to ./ (tell the system that this program is in this path)?
Why Doesn’t the system command need to tell the system which path it is in?

Because:
There is a global environment variable PATH in the system
When executing a program, the operating system will first search for the path saved in PATH. If the instruction
is found, it can be executed. If it is not found, we need to provide the path ourselves, otherwise it will be: command not found
In most cases, the path of our executable program The following is not in PATH

echo $环境变量  查看环境变量的内容
echo $PATH
显示出来的路径是以:作为分隔符的

Let’s take a look at the value of this PATH
I created a new directory process3
and wrote a code.c in it
Insert image description here
Insert image description here
Insert image description here
Insert image description here

3. Further understand the role of environment variables

What if I want my executable program to run without adding ./?

Method 1: Copy to usr/bin (not recommended)

This copying process is equivalent to installing our own instructions/software into the system.

Method 2: Add environment variables

PATH=我们的可执行程序的路径:$PATH

Note:
The last one: $PATH must not be forgotten!!!
Otherwise, the PATH will be completely changed

What should I do if it is really changed
?
Just close it and log in again, and it will be fine
Because we changed it in the memory and not in the disk
The environment variables will be re-initialized every time you log in again

Let’s demonstrate the second method
Insert image description here
Just imagine:
What if there is no PATH?
Next, I cleared the PATH directly
Insert image description here
Insert image description here
. At this time, I
could not execute it directly using ls, clear, mkdir, and touch.
But pwd can be executed (why? We will explain below)
But most commands cannot be run by directly entering the command name
Insert image description here
At this time I can only Specify the path name to run
Isn’t that too inconvenient?
So we can know the role of PATH
Visible environment Variables are very important!!

4. Common environment variables

env  作用:查看环境变量

Insert image description here

echo $PWD
因此pwd这个命令实际是通过读取环境变量得到的
因此我们刚才把PATH置空导致很多命令都无效的时候
pwd却可以正常运行

echo $USER
记录用户名
whoami

echo $HOME
记录当前用户的家目录
cd ~ 

echo $SHELL
记录当前用户所用的命令行解释器
默认为
/bin/bash

Insert image description here
Insert image description here
When logging in, the operating system will load the environment variables related to the current user into the memory
The system can recognize you through the environment variables
(Similar to a company’s work badge)
It is precisely because of these environment variables
that we will automatically enter our home directory after logging in to Xshell< /span> and it can also know our current path (pwd) at any time
And the system can also know that our user name is wzs

5. Export environment variables (add environment variables)

export AGE=20
比方说我们就在这里添加一个环境变量AGE,让它的值定义为20
env

Insert image description here
However
After I exit Xshell and log in again, the AGE environment variable I just added will disappear
because what we changed is in the memory There are no changes in the disk.
The environment variables will be re-initialized every time you log in again
Insert image description here

6. Characteristics of environment variables

In fact, our main function has another parameter:

int main(int argc,char* argv[],char* env[]);

env这个数组当中存放的就是
指向环境变量的字符串的指针

我们也可以来查看一下所有的环境变量
env[i]也是以NULL结尾

Notice:

1. The system will pass in the environment variables as parameters of the main function.

Insert image description here
Insert image description here
It is the same as using the env command to view all environment variables.

2. Environment variables can be inherited by child processes (that is to say, environment variables are global)

In fact, all the processes we write are child processes,
because their parent processes (excluding the processes created by the process we wrote) are all bash a>
In other words, bash passes its environment variables to the process we wrote through the main function

Let’s use fork (create a child process) to verify it
Insert image description here
Only let the child process print env
Insert image description here
Successful inheritance
But
this method of obtaining environment variables also obtains too much
It looks so messy
Yes Can't we just get the specified environment variables?
Of course you can

7. Another way to obtain environment variables

getenv函数:获取指定的环境变量

Insert image description here
Let's demonstrate it now
Insert image description here
Insert image description here
Successfully obtain the name of the user currently running this mycmd
Now I switch to zs and see what will be printed Is zs coming?
Insert image description here
The same is true for root

8. Small feature: code for authentication

Now we can write a code based on the environment variable of this user name
The function of this code is
to make others "unable to "Execute my mycmd
an authentication code

I switched back to wzs and started writing code
Insert image description here
Only the user wzs can execute this program
Other users will be forced to exit if they execute this program< /span> Although zs has x permissions as other, zs also "Unable" to execute my program 2.zs cannot be executed:
1.wzs can be executed:
Insert image description here

Insert image description here

3.root cannot be executed:

Do you think this code can only control zs?
You are wrong, root cannot "execute" it even if it wants to be executed
Insert image description here
It can be seen that this environment variable is indeed Very useful

9. Supplement: The third way to obtain environment variables

1.approximately

Insert image description here
Insert image description here
Insert image description here
Successfully displayed
Just be sure to declare it with extern before using it

10. Local variables

Local variables are different from environment variables
Local variables are not inherited by child processes and are only valid variables within bash
Let’s take a look below Let’s talk about the basic operations of local variables

1. Basic operations of local variables

1. Create local variables

In fact, the method is very simple. For example, I want to create a local variable now
myname, and assign the value to wzs

只需要在命令行中输入:
myname=wzs

Insert image description here
It can be seen that the local variable myname is created successfully
is indeed valid inside bash

2. Verify the characteristics of local variables

Didn’t you say that local variables will not be inherited by child processes?
So now the local variable myname is in bash
As long as I create one process, wouldn't it be enough to check if there is myname in its environment variable?
Therefore, let's check it out
Insert image description here
If there is the environment variable myname, then it will Print
myname is...
Otherwise, print myname not found
Insert image description here
It can be seen that local variables will not be inherited by child processes a>

3. Remove local variables

Both local variables and environment variables can be removed like this

unset 本地变量名或者是环境变量名

Now let me remove myname

unset myname

Insert image description here
Removed successfully

11. How do environment variables work?

These environment variables in the process we created all come from their ancestors (the bash process),
Therefore, only the bash process needs to have these environment variables, right? Environment variables can be inherited by all processes, so they can play a role

But:
Whenever we log in to Xshell
, the system will assign us a bash
and we The environment variables are in bash
But where are the environment variables in bash obtained?

It comes from some script files or configuration files on the disk
It will be loaded from the file on the disk into the memory every time it is started
This way you can give it to bash
Some of the files are
~/.bash_profile
~/.bashrc< /span> in the current user’s home directory and bashrc in the /etc directory Wait for files are .bash_profile .bashrc
/etc/bashrc

Let’s take a brief look at it
Note: Do not modify these configuration files randomly, otherwise serious problems will occur
Insert image description here
Home .bash_profile in the directory
Insert image description here
This is the configuration file that imports the PATH environment variable
.bashrc in the home directory
This is Because I have automatically configured vim for wzs users before, I changed some configuration files
Insert image description here

.bashrc in the etc directory
Insert image description here
We only need to know that the environment variables in bash are read from the configuration file or script file

The above is all about Linux environment variables and command line parameters. I hope it can be helpful to everyone.

Guess you like

Origin blog.csdn.net/Wzs040810/article/details/134629067