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.
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!
A shell script is a set of commands executed by the shell into a single file.
About the command to use
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
#! / Bin / bashis called" shebang ", which means that this shell script is executed in the bash shell located in / bin / bash.
--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!"
$ ./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
while:; do to
done until the end message is received
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
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.