I didn't use C language at all, so I wanted to use it for the first time in a while. So, as a review of Linux, I will try to make my own command that just adds.
The command specification is to output the total value of the numerical values after the second argument on the command line.
・ By making a simple one, you can imagine what the command looks like. -Understand the concept of PATH that Linux refers to when executing commands. For the above reasons, detailed explanation of implementation is omitted.
・ Amazon Linux AMI release 2018.03
After that, work will be done under this directory.
Directory creation command
$ mkdir test; cd test
I'm using vi here, but any text editor can be used.
File creation command
$ vi test_sum.c
Copy the following source code.
test_sum.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int sum = 0;
int count = 0;
if (argc < 3) {
printf("ERROR: invalid of arguments number\n");
return 1;
}
/* argv[0]Is the command itself, so it is excluded from the calculation, so count=Start from 1*/
for (count=1; count<argc; count++) {
sum += atoi(argv[count]);
}
printf("%d\n", sum);
return 0;
}
Atoi converts from the most significant digit and stops the conversion when it detects a character other than a number. Example) "a": Returns 0 because there is nothing that can be converted. "1a3": The most significant 1 is converted, but conversion is stopped because a is detected, and 1 is returned.
Compile the source code (test_sum.c) using gcc
Compile command
$ gcc -o test_sum test_sum.c
The operation is simple, but this completes the command test_sum!
Try using your own test_sum. Below, try some patterns.
test_Calculation using sum
$ ./test_sum 123
ERROR: invalid of arguments number
$ ./test_sum 123 23456
23579
$ ./test_sum 123 23456 812
24391
Great success! !!
If this is left as it is, it will be necessary to specify the path ("./") every time, so set it so that it operates without specifying the path like other commands.
An environment variable that summarizes the paths that Linux goes to look for files when it executes an executable file (ELF). Linux searches the paths set in the PATH in order and executes the first executable file found.
Use export to set environment variables such as PATH. Register the path of the current directory (current directory) in the PATH with the following command.
Set the path of the current directory to PATH
$ export PATH=$PATH:$(pwd)
Now you don't have to specify a path in any directory! Note that this setting disappears when you drop the shell! !! </ strong>
Move the directory appropriately and check the operation.
Operation check
$ test_sum 1 2 3
6
$ cd ~
$ test_sum 1 2 3
6
$ cd /tmp
$ test_sum 1 2 3
6
Great success! !! Now you can use the test_sum command like any other Linux command.
Use which to check the path of commands such as ls and find. Of course, you can also check the location of the created test_sum with the which command.
Check the command path
$ which ls
alias ls='ls --color=auto'
/bin/ls
$ which find
/bin/find
$ which test_sum
~/environment/test/test_sum
・ Slightly change the title and contents ・ Addition about PATH -Renamed to avoid confusion with existing commands -Set the exit status (return value is 0 on success, 1 on failure) @fujitanozomu Thank you for pointing out! ・ Add purpose
Recommended Posts