[C language] [Linux] Try to create a simple Linux command * Just add! !!

Introduction

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.

Purpose

・ 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.

environment

・ Amazon Linux AMI release 2018.03

procedure

1. Create a test directory

After that, work will be done under this directory.

Directory creation command


$ mkdir test; cd test

2. Create a source code file

I'm using vi here, but any text editor can be used.

File creation command


$ vi test_sum.c

3. Implementation

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.

4. Compile

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!

5. Simple operation check

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! !!

6. PATH setting

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.

6.1. What is PATH?

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.

6.2. PATH setting

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>

6.3. Operation check

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.

  • Just add up. .. ..

bonus

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

Fixes

・ 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

[C language] [Linux] Try to create a simple Linux command * Just add! !!
Try to create a new command on linux
How to create a shortcut command for LINUX
Try to make a Python module in C language
Try to select a language
[Go language] Try to create a uselessly multi-threaded line counter
To add a C module to MicroPython ...
Command to create Linux Live USB
Rails users try to create a simple blog engine with Django
[C language] How to create, avoid, and make a zombie process
[Linux convenient command] Try to insert vivid
Add a command to mark similar files together
Create a command to encode / decode Splunk base64
Use click to create a sub-sub command --netsted sub-sub command -
Create a command to get the work log
Try to make a command standby tool with python
Try to dynamically create a Checkbutton with Python's Tkinter
How to create a local repository for Linux OS
How to create a simple TCP server / client script
Create a simple API just to input and output JSON files ~ Python / Flask edition ~
Add Linux user, how to use useradd command (password specification)
[Python Kivy] How to create a simple pop up window
Just try to receive a webhook in ngrok and python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
A super introduction to Linux
Add a dictionary to MeCab
Create a (simple) REST server
Create a simple textlint server
linux: create original Terminal command
I tried to create a simple credit score by logistic regression.
Try to create a python environment with Visual Studio Code & WSL
[Go] Create a CLI command to change the extension of the image
Try to make a web service-like guy with 3D markup language
How to output the output result of the Linux man command to a file
Try to create a Qiita article with REST API [Environmental preparation]
Create a command line tool to convert dollars to yen using Python
[Linux convenient command] Try inserting exa
Try to draw a Bezier curve
Steps to create a Django project
A memorandum to change to Manjaro Linux
How to create a Conda package
[Linux convenient command] Try inserting csview
Introduction to Protobuf-c (C language ⇔ Python)
How to create a virtual bridge
[linux] kill command to kill the process
Create a Linux environment on Windows 10
[Linux convenient command] Try inserting bat
How to create a Dockerfile (basic)
5 Ways to Create a Python Chatbot
How to create a config file
Create a C wrapper using Boost.Python
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
[Linux] Command to get a list of commands executed in the past
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
Create a command to delete all temporary files generated in a specific folder
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)
What is a C language library? What is the information that is open to the public?
[C language] How to use the crypt function on Linux [Password hashing]
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko