[LINUX] Try programming with a shell!

Hello everyone!

Have you ever heard of a shell?

If you refer to the site of here,

A shell is a cycle in which when using terminal software (kterm, etc.) on UNIX / Linux, the user inputs the command to be operated at the prompt, sees the result displayed there, and then inputs the next command. Work interactively with. In reality, the user is doing various processing using the functions managed by the core (kernel) of UNIX / Linux, but the kernel itself does not have the ability to interact directly with the user. Therefore, there is an intermediary program between the user and the kernel that has the ability to interact with the user and asks the kernel for operations, and the user operates. This intermediary program is called a shell because it looks like a "shell" that wraps around the kernel to the user (see Figure 1). Simply put, the shell has the ability to interpret commands entered by the user, request the kernel to process them, and display the results and messages on the screen.

That's right!

In other words, when we give instructions to the kernel from the terminal, it seems to be an interpreter who breaks in with the kernel and translates it so that the kernel can be understood!

Now that you have a quick understanding of the shell, let's start shell programming using the terminal!

It seems that there are various types of shells, but in recent Linux and Mac OS, bash is the standard environment for shells, so bash is used!

Programming with the shell

A shell script is a set of commands executed by the shell into a single file.

About the command to use

-Introduction of basic commands for shell scripts for beginners

-Introduction to shell script Summary of how to write

Please think about the function you want to use and check it out!

The extension of the shell script is ".sh"

Sample shell script (a script that executes the date command after executing the ls command, outputs a character string with the echo command, receives the input result with the read command, puts it in a variable, and outputs it)

#!/bin/bash
ls
date
Please enter an echo message.
read message
echo Input message: $message

--File permissions For all Linux files

-File owner ·Group membership ・ Other users For a group of three accounts

・ Readable ・ Writable ・ Executable

It manages three authorities

chmod a + x my-first.sh changes file permissions so that my-first.sh can be run by all users

--Run shell script Must be executed with a relative path with ./ → If not, the error my-first.sh: command not found will occur ↓ If you do not specify a relative path, it will be recognized as a command, so you will be angry with "There is no such command!"

Execution result

$ ./my-first.sh

my-first.sh
Thursday, March 29, 2018 08:13:52 UTC
Please enter your message.
Message entered: hoge

Display the input contents by substituting the input character string for the variable of the first argument with the read command and specifying the variable with $ with the echo command.

--If statement in shell script

#!/bin/bash
read -p "Is Yarigatake the second highest mountain in Japan?[y/n]" yn
if [ $yn = "n" ]; then
echo Correct answer. The second highest mountain in Japan is Mt. Kitadake.
else
echo Incorrect answer. The second highest mountain in Japan is Mt. Kitadake.
fi

The read command can receive input into a variable while displaying characters by adding the option -p "character string you want to display".

The if statement is ʻif [$ yn =" n "]; then, the condition is entered in [] with a space, and the last is a colon and then. Also, it is necessary to write fi` at the end of the if statement.

--While clause in shell script

Loop from while:; do to done until the end message is received

Example: while:; do (echo" Thank you for your access! ") | Nc -l 8000; done

→ Start as a server using the port 8000, and if there is access, return the contents of the standard output and exit

Passing the standard output of the echo command to the nc command using | (pipe)

nc command: Abbreviation for NetCat. It is a command to read and write TCP and UDP, and has various functions such as acquiring information from a Web server, installing a simple Web server, and sending mail.

Port: One of the TCP and UDP communication arrangements. Communicate by setting one of the numbers 0 to 65535. In order to communicate, you must always use some kind of port, and there are many that have a fixed port depending on the protocol and software.

Recommended Posts

Try programming with a shell!
Try creating a FizzBuzz problem with a shell program
Try GUI programming with Hy
Let's try a shell script
Shell programming
Programming for humans with a well-defined __repr__
Try drawing a normal distribution with matplotlib
Try HTML scraping with a Python library
Try using a stochastic programming language (Pyro)
Manage logrotate generations with a shell script.
Try a functional programming pipe in Python
Try drawing a map with python + cartopy 0.18.0
Try TensorFlow RNN with a basic model
I made a competitive programming glossary with Python
Try Tensorflow with a GPU instance on AWS
View today's weather forecast with a shell script
Try to draw a life curve with python
Try sending a message with Twilio's SMS service
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Try drawing a map with Python's folium package
Try scraping with Python.
Asynchronous programming with libev # 2
3. 3. AI programming with Python
Scraping with scrapy shell
Python programming with Atom
Competitive programming with python
Shader programming with pyOpenGL
Try SNN with BindsNET
I tried shell programming
Asynchronous programming with libev
Linear Programming with PuLP
Try regression with TensorFlow
Decorate with a decorator
Programming with Python Flask
Asynchronous programming with libev # 3
Try making a simple website with responder and sqlite3
Try to make a command standby tool with python
Try to dynamically create a Checkbutton with Python's Tkinter
Try embedding Python in a C ++ program with pybind11
[GCP] Try a sample to authenticate users with Firebase
A sample to try Factorization Machines quickly with fastFM
Programming with Python and Tkinter
Try to factorial with recursion
Try running python in a Django environment created with pipenv
Try function optimization with Optuna
Learn librosa with a tutorial 1
Try deep learning with TensorFlow
Try using PythonTex with Texpad.
Try normal Linux programming Part 7
Try edge detection with OpenCV
Try implementing RBM with chainer.
Try Google Mock with C
[Linux] Copy data from Linux to Windows with a shell script
Try using matplotlib with PyCharm
Try an autoencoder with Pytorch
Try Python output with Haxe 3.2
Try matrix operation with NumPy
[Python] Try optimizing FX systole parameters with a genetic algorithm
Try normal Linux programming Part 2
Create a homepage with django