[Command line parameters] and [Environment variables]

I. Introduction

I believe that in the process of learning C language, you must have seen a main function like this:

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

        Most of them only have the first two parameters, and a few have three parameters. However, you have basically never used them, and you have basically never verified what they are. For everyone who sees this, I suggest that you finish learning the operating system and commands. Let’s explore it after performing the operation. Learning C language does not involve the parameters of the main function, so you can ignore it for now.

2. Command line parameter content and verification

The parameters of the main function are called [command line parameters]

int main(int argc, char *argv[], char *env[])
  1. argc: The number of parameters in the command line, separated by spaces;
  2. argv: pointer array, which stores the address of the first element of each parameter in the command line parameters, terminated by NULL
  3. env: pointer array, which stores the address of the first element of each environment variable, terminated by NULL

Now let’s verify what these parameters are!

1. [argc] and [argv]

#include <stdio.h>
int main(int argc, char *argv[]) 
{
    for(int i = 0; i < argc; ++i)
    {
        printf("argv[%d] : %s\n", i, argv[i]);
    }   
    return 0;
}

        Here you will find that the parameter argc is the number of strings entered in our command line , and the character pointer array argv stores the address of the first element of these strings separated by spaces.

What if we don't add the argc limit?

#include <stdio.h>
int main(int argc, char *argv[]) 
{
    for(int i = 0; argv[i]; ++i)
    {
        printf("argv[%d] : %s\n", i, argv[i]);
    }   
    return 0;
}

        We will find that these characters on the command line are still output, so we will find that this character pointer array is terminated by NULL. Seeing that this is very similar to the instructions in the command line, yes, the instructions in the command line are composed of the main parameter - the command line parameter. This way we can also create our own instructions. Different functions are implemented through different options that follow.

2. env

Represents environment variables, also terminated by NULL

#include <stdio.h>
int main(int argc, char *argv[], char *env[]) 
{
    for(int i = 0; env[i]; ++i)
    {
        printf("env[%d] : %s\n", i, env[i]);
    }   
    return 0;
}

3. Environment variables

        First, let’s understand variables. When we write programming language code, we will define many variables. The essence of defining variables is to open up a space, and then give this space a name and write content. So the environment variables we are going to learn today are also based on the same principle. They also open up a space in the memory and have their own names and contents.

        Environment variables: There are multiple variables, but they are collectively called environment variables. Just like each of us has his own name, but we are collectively called people. There is no relationship between environment variables. They are variables built into the operating system and have special purposes.

3.1 How to view environment variables:

1. View the specified environment variables

echo $name //name -> 环境变量名

2. View all environment variables

env

3.2 Create and delete an environment variable

export 环境变量名=值
unset name

3.3 Obtain environment variables in the process

1. Obtaining command line parameters

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[], char *env[])
{
    for(int i = 0; env[i]; ++i)
    {
        printf("***env[%d] : %s\n", i, env[i]);
    }
    return 0;
}

2. Function call to obtain specified environment variables (commonly used)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("%s\n", getenv("PATH"));
    return 0;
}

3. Obtaining third-party variables

#include <stdio.h>
#include <stdlib.h>
int main()
{
    extern char **environ;
    for(int i = 0; environ[i]; ++i)
    {
        printf("environ[%d] : %s\n", i, environ[i]);
    }
    return 0;
}

3.4 Test environment variable PATH

PATH: Specifies the search path for the command

        Based on the above study of command line parameters, we know that instructions and different options of instructions are composed of command line parameters. Therefore, the essence of an instruction is an executable program. To run an executable program, you need to bring the path where it is located . The instruction does not need to bring the path where it is located. This is because when we enter the instruction, we will automatically search for it in the environment variable PATH. If we find it, we will run it directly. If we cannot find it, it will show that it is not found.

1. Turn executable programs into instructions

Add search path to PATH:

PATH=$PATH:程序所在路径

Place the executable program in any path in PATH (but because the instructions we wrote are not mature enough and may pollute the command pool, it is not recommended)

3.5 Talking about environment variables

        Environment variables are actually global variables, because each of our programs has environment variables that can be printed out. This is because environment variables can be inherited by child processes, and all programs running on the command line are child processes of bash, then Environment variables will be inherited by these processes. After the above understanding of PATH, we know that the command can be run only by searching from the search path in PATH.

        So how does the operating system know the path you are on? It is provided by an environment variable PWD;

        The operating system knows who the user is based on the environment variable USER;

        When we log in, we enter the home directory by default, based on the environment variable HOME.

        Therefore, environment variables provide user-related information to the operating system. But the environment variables we know now are at the memory level, that is, every time we log in, the environment variables will be re-initialized, that is, the modification of the environment variables during the last login will not take effect on the second login.

        Then every time you log in, who gives the bash environment variables?

        It must be a file on the disk (.bash_profile in the home directory). Every time you log in, the contents of this file will be given to bash.

3.6 The meaning of environment variables

        Let’s briefly talk about one of the meanings of environment variables. You can identify the user’s identity based on the USER of getenv to see whether he or she has the authority to run a certain code;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    if(strcmp("wyd", getenv("USER")) == 0)
    {
        printf("I am wyd , you can do ...\n");
        //wyd代码
    }
    if(strcmp("root", getenv("USER")) == 0)
    {
        printf("I am root , you can do ...\n");
        //root代码
    }
    return 0;
}

4. Local variables

        It is not an environment variable, cannot be inherited by child processes, does not have global properties, and is only valid within bash.

        Define a local variable, just name=val;

        To view variables, use set. Set can display both local variables and environment variables;

        Delete variable: unset name

Guess you like

Origin blog.csdn.net/2302_76941579/article/details/135158213