UNIX basics (second part)

About this article

This is a summary of the basics of UNIX-like personal computers. If you read this article and understand it properly, you will have a good understanding of UNIX. Please click the link below for the first part.

UNIX basics (first part) https://qiita.com/minnsou/items/6b6b74fcd79bd00ace1a

Environmental setting

How to set up the environment

The shells used in UNIX (sh, csh, bash, etc.) can be customized. There are two main ways to customize commands.

  1. Method by configuration file A configuration file is a file that describes the settings of a command. Generally, a command that uses a configuration file first reads the contents of the configuration file when it is started, and changes its operation according to the description. For example, the configuration file is .profile for sh, and .bash_profile or .bashrc for bash. This depends on the shell.
  2. Method by environment variables Environment variables are used by setting values in variables. When the command is invoked, it checks the value of environment variables as needed and changes its behavior depending on the set value. Environment variables are usually set in the configuration file.

Interactive shell and non-interactive shell

There are two types of shell startup: __interactive __ and __non-interactive __. Interactive is a form of repeating tasks such as "when a human makes a request, the computer processes it." Non-interactive is a form in which all the contents of processing are described in a file or something in advance, and the computer reads the contents and processes them. Note that a configuration file such as the csh configuration file .cshrc, which can be read even by a non-interactive shell, usually causes an error when displaying a message or setting a terminal. For csh, you need to use the shell variable $ prompt to separate cases.

Shell variables and environment variables

In the shell configuration file, set shell variables, environment variables, aliases, etc. __ Shell variable __ is a variable unique to the shell. Some of these variables have special meaning, and you can change the behavior of the shell by setting such variables. The __environment variable __ is a variable owned by all processes used in UNIX, not the shell. When another process is created from one process, the newly created process inherits the environment variables set in the original process as they are.

The shell and environment variables are: "Environment variables are passed to the child process, but shell variables are not passed (the environment variables are managed by the kernel, so when the child process is spawned, that is, the command is invoked from the shell. The difference is that the kernel can then pass a copy of the shell's environment variables to the child process, but the shell variables are managed by the shell and are not detected by the kernel __). When shell variables and environment variables are confused, remember __ "Shell variables are used to set the behavior of the shell itself, and environment variables are used to set the behavior of commands that the shell invokes." ..

Shell variables are set with variable = value for sh, bash, and zsh. Do not put spaces at both ends of =. An example of a shell variable is

--Prompt (PS1 is$, PS2 is >, PS4 is # is the basic setting. The default of PS1 on Mac is\ h: \ W \ u \ In $, \ h is the host name, \ W is the current directory name, and \ u is the user name.) --Number of commands to save in history (HISTSIZE) --Characters used in history replacement (histchars, which defaults to! And^, but there is nothing in the variable, so you can change this character by setting it likehistchars =%)

and so on.

Examples of environment variables are PATH, TERM, HOME, USER, SHELL, etc. (bash cannot be lowercase) and are generally uppercase. All can be found in ʻecho $ PATH. If you want to use the shell variable a that you have defined as an environment variable, use ʻexport a. However, environment variables like this also disappear when you log out of the shell, so if you want to keep them permanently, you need to write them in .bashrc etc.

You can see all the environment variables with the printenv and ʻexport commands, and you can see the explanation of the typical environment variables used in the system with man environ. See all shell variables with the declare` command (but you can also see the defined functions).

alias

Ability to give aliases to commands. With this function, you can do the following.

  1. Reduce typing effort (such as ʻalias md = mkdir`)
  2. Create a new command (such as creating your own command)
  3. Anticipate typos (such as ʻalias mroe = more`)
  4. Change the meaning of the command (such as ʻalias ls ='ls -F'`)
  5. Do things that a shell script can't do (setting shell and environment variables for the shell you're currently using doesn't make sense in a shell script, because running that shell script launches another shell)

Terminal settings

Terminal settings are also one of the items set in the shell's configuration file. The most important command to configure the terminal is the stty command. With the stty command, you can set the terminal assignment key and output mode. For example, the command stty erase ^ H will change the key to delete one character to Ctrl-H. The following is a list of frequently used keys that can be changed with stty.

function Default key meaning
erase ^? (Del) Delete one character
kill ^U Delete one line
werase ^W Delete one word
susp ^Z suspend(Job control)
intr ^C Stop execution
eof ^D End of file(Log out for shell)

Regular expression (on UNIX)

A regular expression is one of the ways to represent a set of strings based on a rule. Note that the regular expressions used in this article in the future are the regular expressions used in UNIX and are slightly different from the regular expressions used in automaton theory and linguistic theory.

For example, in the string ^ a. * Ttion $, "there is an at the beginning of the line (^), and there is 0 or more repetitions (*) of any one character (. ). , Tion followed by the end of the line ($) ". Characters with special meanings, such as ^ and *, are called metacharacter.

There is more than one type of regular expression, and the functions realized by the tools are limited, and the expression method is slightly different. Here, I will introduce it in two parts.

  1. Regular expressions of grep, ʻed, vi, sed, less`
  2. Regular expressions of the ʻegrep and ʻawk series

In addition, it should be noted__Shell filename replacement__Used in*(Arbitrary string)Or?(Any one character)Note that the meaning of the symbol is a little different from the metacharacter like.ps | grep *tionIf you enter a command like*By the shell__File name is replaced first__。ps | grep '*.tion'like''Must be enclosed in.

1. Regular expressions of grep, ʻed, vi, less` series

--Eliminate the meaning of the \ m metacharacter m --^Line beginning --$End of line --. Any single character --[c1c2 ... cn] Any one of the characters in a row (for example, [abc] is ʻaorb or c) --[^ c1c2 ... cn]Any one character other than the arranged characters (for example, if[^ abs], any one character other than ʻa, b and s) --0 or more repetitions of the r * regular expression r --\ (r \) Tagged regular expression r (This is a grouping of regular expressions, and if you add*after this, you can express 0 or more repetitions) -- \ N Represents a string that matches the Nth tagged regular expression

For example

--ʻA * c matches " c "" ʻac" "ʻa ac" and so on. --\ ([ab] c ) \ 1 matches" ʻacac" or " bcbc ". When "'\ ([ab] c ) "matches" ʻac", " \ 1 "represents" ʻac", so it does not match "ʻacbc".

2. Regular expressions of the ʻegrep and ʻawk series

It's basically the same as the regular expression above, but now you can use the regular expression format defined in linguistic theory.

--One or more repetitions of the r + regular expression r --There is or does not have one r? Regular expression r -- r1 | r2 Regular expression r1 or r2 (meaning or) --Grouping of (r) regular expressions r (this makes tagged regular expressions unusable in ʻegrep` etc.)

For example

--ʻAb + c matches "ʻabc", "ʻabbc", "ʻabbbc", etc. --(a | b) c matches" ʻac" or " bc` "

Find and replace

Search

Basically, use vi or emacs search or grep to search for words in the __ file, and use the find command to search for __ file name __.

In vi, use / keyword to search forward and? Keyword to search backward in command mode. emacs uses Ctrl-S keyword to search forward and Ctrl-R keyword to search backward.

grep, ʻegrep, and fgrepare commands that receive a certain data, extract the line that matches the specified character string from it, and output it. The basic form isgrep keyword files, and when the file name filesis not specified, data is input from the standard input.grep is the oldest in the so-called" grep family ". ʻEgrep is basically grep, but the regular expressions that can be handled have been expanded. fgrep can search multiple strings at the same time, but regular expressions cannot be used.

The find command searches for the file itself. The standard usage of the find command is as follows.

find directory search criteria

For example, find. -Name'*. Py' means to search all files under the current working directory that end with .py. You can also use find. -name'* .py' -atime + 15 to search for files that haven't been accessed for more than 16 days. -atime represents the last access time.

A slightly more practical example is find. -Name'* ~'-exec rm {} \; -print. This is to delete the file name ending with ~ (rm {}) and print it ( -print). The -exec option executes UNIX commands up to the escaped semicolon (\; ), where{}is a pathname that meets the conditions.

Replacement

Replacing with vi is the same as the replacement function of the ʻex command, which is another face of vi. ʻEd and sed use similar replacement commands. Basically, it has the following format and replaces the string that matches pattern with string.

range s/pattern/string/g

Specify the replacement range for range. For example, % represents the whole line, and start, end represents the lines from start to ʻend. Regular expressions can be used for pattern and string. The last / gmeans to change all the replacement parts in one line. The details here will be dealt with in detail in the introduction of thesedcommand. You can replace emacs withM-% (M-x query-replace`).

filter

Filter is a program that processes the data input from the standard input and outputs the result to the standard input. There are two main types, the first is that you can specify the filter function by giving a program to the filter itself (sed, ʻawk, perl, etc.), and the second is that the function is decided from the beginning. Things (head, tail`, etc.).

Filter example

Create a filter with new functions as an example using the head and tail commands. Use belly n m as a filter to extract lines n to m. The contents of belly are as follows.

.sh


#!/bin/bash
from=$1
to=$2
arg=$(($to - $from + 1))
head -$to | tail -$arg

Now you can retrieve lines 12-14 of foo.txt with the command cat -n foo.txt | ./belly 12 14.

tee command

A filter that outputs input from standard input to both the file specified as an argument and standard output. For example, if you do make | tee bar.txt, you can output the result of the make command to bar.txt and also to the screen.

sort command

A filter that sorts the input from the standard input line by line and outputs it to the standard output. For example, with ls -l | sort -nr + 4, the 4th field (file size) is compared as a numerical value and arranged in descending order. -n is compared as a number, -r is displayed in reverse order as specified, and -f is case insensitive.

ʻUniq` command

A filter that considers duplicate lines if adjacent lines of input from standard input have the same content, removes the duplicate lines, and outputs to standard output. For example, suppose you have files aa, bb, and cc in the OLD directory and files aa1, bb, and cc in the NEW directory. (ls OLD; ls NEW) | sort

aa
aa1
bb
bb
cc
cc

Is displayed, but(ls OLD; ls NEW) | sort | uniqThen

aa
aa1
bb
cc

Is displayed. The bb and cc that I'm wearing disappear.

tr command

A filter that replaces the input from the standard input character by character and outputs it to the standard output. Use in the form tr old-string new-string and replace the characters that appear in old-string with the characters that correspond to new-string. For tr ab XY, replace a with X and b with Y. If it is tr a-z A-Z, all alphabets are replaced with uppercase letters and displayed. For example, ls | tr a-z A-Z will display the file names in all capital letters. If you specify the -d option, it becomes a filter that deletes the specified character, so if you use cat file | tr -d'\ 015', you can delete the CR of the control character (windows is the end of the line). Is CR LF and UNIX is only LF, so the above command will fix the line breaks).

ʻExpand` command

A filter that converts input tabs from standard input to a corresponding number of blanks and outputs them to standard output. ʻUnexpand` is a filter that replaces consecutive whitespace with tabs.

fold command

A filter that outputs the input from the standard input to the standard output after forcibly breaking the line so that the length of each line becomes a fixed number of characters. For example, use the command ʻexpand file | fold -65to make the line width 65 characters. Thefold` command does not interpret tabs, so preprocess with expand as in this example.

pr command

A filter that converts the input from the standard input to the output format for line printers and outputs it to the standard output. Specifically, it is possible to add the file name, time, and page number as the header of each page number, and to make the text multi-column.

sed command

sed (stream editor) is an editor designed to be used as a filter. The basic form is sed command. command represents" what function is applied to the line that matches which address ". The format is [address1 [, address2]] function. If one address is specified, the function is applied to only that line, if two addresses are specified, the function is applied to all lines between the two addresses, and if no address is specified, the function function is applied to all lines.

There are several ways to specify the address. You can use numbers to represent the number of lines, and you can also use regular expressions. For example, for 10p, apply the p function on the 10th line. For / ^ a /, / ^ b / p, apply the p function from the line starting with a to the line starting with b.

The following for each function.

--The p function is a function used to output the contents of the __ line being processed. By default, sed outputs the result processed by the command, but when the -n option is specified, the automatic line output is not performed, so only the line specified by the p function is displayed. become. For example, cat -n / usr / share / dict / words | sed -n '100, 105p' will display each line from line 100 to line 105. --The d function is a function used to delete __ unnecessary lines from the input. For example, the command sed'/ ^ BEGIN /, / ^ END / d' deletes from the line starting with BEGIN at the beginning of the line to the line starting with END at the beginning of the line. The q function is a function that ends the execution of sed when it matches the address, and is used like sed -e '100, 105p'-e '105 q'. -e is an option to use when you want to process multiple functions. In this example, the execution of sed ends at the 105th line. --The s function is a function that performs string replacement __ using __regular expressions. Expressed in the form s / regular-expression / replacement / flags. Replaces the first string that matches the regular expression specified in the search pattern regular-expression for the line being processed with the string specified in the replacement pattern replacement. In flags, specify flags to change the behavior of the s function. For example, g replaces all strings that match the regular expression in the line (by default, only the first matching string is replaced). If p, the matched line is output. --The y function is a __ function that replaces __ input units, like the tr command. For example, if you enter y / aiueo / AIUEO /, all letters of aiueo will be capitalized.

Other results such as branching and intermediate storage can be obtained, but details are omitted.

ʻAwk` (oak) command

awk is a language for processing input lines according to patterns. I am good at processing files in table-like format, where each line of input is separated by delimiters such as spaces and tabs. A little closer to c. To run awk, you can either specify program as an argument to ʻawk program, or write the program to file program-file as ʻawk -f program-file and write it to -f There is a method to specify it as an option. The contents of program are

pattern1 { action1 }
pattern2 { action2 }
...

The pattern pattern__ for __input and the action ʻaction__ to be executed when the pattern is matched are described as a pair. When pattern is omitted, the action ʻaction is performed on all input lines. When {action} is omitted, the line matching pattern is output.

Regular expression patterns are specified by enclosing them in /. ʻAwk'/ ^ From: /' will output all lines starting with From :. When you specify an awk program on the command line, don't forget to enclose it in'_ so that the program given to __awk is one argument. ʻAwk automatically splits the input line into fields by delimiter. For example, ls -l is the mode (such as -rw-r--r--) in $ 1, the number of hard links in $ 2, $ 3 is the owner name, and $ 4. Enter the group name, $ 5, such as the size of the file (in the order in which ls -l is displayed).

For example, ls -l | awk'$ 5> 1000 {print" size of file ", $ 9," is ", $ 5;}'

size of file filename1 is 34694
size of file filename2 is 1011
size of file filename3 is 56203

Only files with a file size larger than 1000 are displayed. In addition to print, variable assignments and operations and control statements such as ʻif and for can be included in ʻaction, but details are omitted.

Compression and archiving

compression

In the field of computer science, a mechanism for reducing the amount (data amount) of information expressed in some form without losing its contents has been considered for a long time. Such a mechanism is called data compression, or simply compression. Restoring a compressed file is called decompression.

The compress command doesn't have that high compression, but it's not slow and comes standard with most UNIX systems. When compressed, it has a .Z extension. Use the ʻuncompresscommand for decompression. Thegzipcommand has a .gz extension. This is a type of GNU software. Use thegunzip command to decompress a file compressed with the gzipcommand. Note that you may be able to decompress files compressed with thecompress command and files compressed with the gzipcommand by using the commandzcat. There is also a compression command called bzip2. Use the bunzip2` command for decompression.

archive

Combining multiple files into one file is called __archive __. Commands for archiving include tar, shar, cpio, zip.

tar is an abbreviation for tape archive, which was originally a command for creating an archive file on magnetic tape. However, the media that can be used is not limited to magnetic tape. Use this command in the form tar key filename .... The key contains only one of the five command characters c, t, x, r, and ʻu`, and options if needed.

-- c creates a tar file --t examines the contents of the tar file --x retrieves the file from the tar file --r and ʻu` add files to the end of the tar file

Frequently used options are as follows.

--The v option means verbose, and if this is specified, the file names to be processed in the archive will be output in sequence. --The f option takes one argument and specifies the name of the tar file to create

However, the option in tar is specified by connecting it with the command character and other options without adding the character " - ". For example, with tar cvf srcfiles.tar ./src, create a tar file called scrfiles.tar based on ./src.

There is also GNU tar, which extends the original tar command. Sometimes it has the command name gtar, and sometimes it has the command name tar. There are many other commands such as shar, cpio, and zip, but they are omitted here.

Times of Day

Know the current time

Use the date command to see the time. Set to Greenwich Mean Time with the -u option.

View calendar

Use the cal command to see the calendar. For example, you can see the calendar for November 2019 at cal 11 2019. Note that if there is only one argument, it will be interpreted as a year.

Use an alarm clock

The leave command can be used as an alarm clock. If you set leave 1920, the alarm will be set at 19:20 and a message will appear with a beep. There is also a way to specify leave + 30 (a message is sent after 30 minutes), and it is also possible to set an alarm with a relative time from the present. Use the kill command when you don't need it.

Take an interval

The sleep command is literally a command to rest without doing anything. A command that rests and exits without doing anything for the number of seconds given in the argument. For example, if you execute the command sleep 300; echo" Tea is just ready. "&, After 5 minutes, the ʻecho` command will be executed and a message will be displayed.

Execute the command at the specified time

There is a ʻat command as one of the methods to execute the command at the time you decide. This will execute the specified shell script at the specified time. ʻAt time In the format [filename], enter the time to start in time and the name of the shell script to be executed in filename. If filename is not specified, the shell script is read from the standard input and executed. For example, the command ʻat 6am 6 script will execute the script at 6 am on June 6th. However, this command uses sh by default. Also, the standard output and standard error output of this script come by __mail __, so if you don't like it, redirect (> result.out`, etc.).

The status of the shell script whose execution is specified by the ʻat command can be known by the ʻatq command. Use the ʻatrm command to cancel the execution of the shell script specified by the ʻat command.

Work regularly

UNIX has a mechanism to start commands on a regular basis. With this mechanism, it is possible to automatically delete unnecessary files every day. This mechanism is realized by a daemon called cron. The file that sets the commands that are executed regularly is called the crontab file. The cron daemon refers to the contents of the crontab file and executes commands on a regular basis.

Use the crontab command to configure the crontab file. Use crontab -e to create a new crontab file or change its contents. This will launch the editor and load the user's crontab file.

Put one setting per line in the crontab file. In the crontab file used by general users, each line consists of the following six elements separated by spaces.

min hour day mon day-of-week command

The first four are the designation of minutes (min), hours (hours), days (day), and months (mon). day-of-week specifies the day of the week, such as 0 for Sunday and 1 for Monday.

For example

0 0 1 1 * find $HOME -name '*core' -atime +7 exec rm {} \;

At 0:00 on January 1st, the command find $ HOME -name'* core' -atime +7 exec rm {} \; is executed. If you specify * instead of a number in the first five fields that specify a command, the command will be executed regardless of the number in that field. Execute with the -l option to display the contents of the crontab file. To delete the crontab file, use crontab -r.

Measure command execution time

Use the time command to measure the command execution time. The format of time command.

bash programming

Shell script

A file in which commands are stored in advance so that they can be executed in a batch is called shell script. It's a bit old-fashioned, but it's also called batch in the sense that the processing method is decided before the execution starts. It is also called shell program because the process is organized for some purpose.

It is troublesome to type bash script_name every time you execute a shell script, so if you put #! / Bin / bash at the beginning of the file, you can execute it as a command. So you can do this just by typing ./file_name (but you need to make the file mode executable, like chmod + x file_name). A shell script that can be executed as a command in this way is called a __shell command __.

Behavior when executing shell commands

First, when bash finds out that the command name is file_name, it searches for this file from the command search path (however, when it is ./file_name, it does not search from the path because the path has already been specified). And when the file is in executable mode, look at the first line of the file.

  1. When the beginning of a line is #!, The kernel is requested to execute the file. Then start the file specified by the absolute path name after #! As a program. For example, #! / Bin / csh or #! / Usr / bin / python. Use a text file as the input. What is written after the absolute path is regarded as an argument.
  2. Otherwise, start bash and use the rest of the text file as its input

Note that the handling of # is non-interactively different from when using the shell interactively. Note that when using # in a shell script, it will be recognized as a __comment __. For example, if you execute rm important # from the command line and execute it with a shell script, the files that disappear will be different. If you want to delete ʻimportant # in a shell script, escape it like ʻimportant \ #.

When bash is started, the current working directory of bash to be started and the current working directory to be started are __matched __. You can also use relative paths to execute shell commands.

Variable details

In bash, variables can also use array. For example, declare the array names = (hatter duchess alice). Now you can refer to the value as ʻecho $ {names [0]}`.

When writing a shell script, there are times when you want to use the default value when a variable is not defined, or do processing according to the number of arrays. To do this, bash provides a way to find out if a variable is defined or the number of arrays. The basic ones are listed below.

Designation meaning
${variable} The value of the variable variable (the value of the variable)$If you want to add the letter z immediately after a, this notation is not interpreted as a variable az)
${#variable} Number of elements in array variable
$# Number of arguments when invoking a shell command
$0 Shell command filename
$num Numth argument when invoking a shell command
$* All arguments when invoking a shell command(Strings, not arrays)
${variable:-word} If the variable specified by variable is defined, its value, if not defined, the word value
${variable:=word} If the variable specified by variable is defined, its value is set, if it is not defined, it is set as the word value, and that value is set in variable.
${variable:?word} If the variable specified by variable is defined, its value is displayed, and if it is not defined, the word value is displayed and the process ends.
${variable:+word} If the variable specified by variable is defined, its value, if not defined, an empty string

Use the read command to input to a variable from standard input.

I wrote an actual example of a shell script in a file called script.sh.

script.sh


#!/bin/bash
echo "this result is produced by $0"
echo '$#' is $#
echo '$*' is $*
echo '$$' is $$ '(process id)'
echo ${word:-abcg}
echo '$1' is ${word:=$1}

If you execute this with ./script.sh hello bash and the command line, it will be as follows. スクリーンショット 2020-04-04 15.26.09.png $$ represents the process ID of that shell. A typical example of using this is __ to create a temporary file used in a shell script. If you substitute something like tmp = tmp. $$, you can safely create a temporary file using the property that the process ID will never be covered until the process ends.

I / O redirection

Process inputs and outputs are specified using a 0-based number called descriptor. 0 represents standard input, 1 represents standard output, and 2 represents standard error output. It is possible to output the output to the descriptor num to file in the form of num> file. If you add & and descriptor num after the redirection> to make> & num, the output will be redirected to the num file. It is difficult to understand, so I will give an example.

The following ʻoutput.sh` is a script that outputs the string stdout to standard output and the string stderr to standard error.

output.sh


#!/bin/bash
echo "stdout" >&1
echo "stderr" >&2

This is from https://qiita.com/laikuaut/items/e1cc312ffc7ec2c872fc.

  1. With ./output.sh> a, the character string of stdout is entered in a. This is the same as ./output.sh 1> a, only 1 is omitted.
  2. With ./output.sh 2> a, the character string of stderr is entered in a.
  3. With ./output.sh 1> a 2> b, the stdout string is in a and the stderr string is in b.
  4. With ./output.sh> & a, both strings are entered in a. Inserting a space between > and & will result in an error.
  5. In ./output.sh> a 2> & 1, both strings are entered in a.
  6. In ./output.sh 2> & 1> a, __a can only contain the stdout string __. This is because the string stderr is output to __standard output at the first 2> & 1. After that, > a is done, so only the standard output is written to the file.

About control structure

In programming, it may be necessary to judge the conditions, decide which part to execute next, or repeat the process until a certain condition is satisfied. The syntax for controlling such execution is here

  1. if
  2. for
  3. while
  4. case
  5. Function
  6. Other commands that support the control structure

To take up.

  1. if

The if syntax is as follows.

if commandlist1
then commandlist2
else commandlist3
fi

__ A command list __ is a list of commands separated by ; or line breaks. In the ʻif command, when the execution result of commandlist1(that is, the exit status of the last command) is normal termination (0),commandlist2is executed assuming that the condition is satisfied. Otherwise, runcommandlist3. fi` is the reverse of if. There is also elif.

  1. for

The grammar of for is as follows.

for variable in list
do commandlist
done

In the for statement, the first word of the list list is set as the value of the variable variable, and the command list commandlist from do to done is executed. Then set the second word in the list to variable and repeat ... There are also continue and break commands.

Lists can be expressed in multiple ways. You can enumerate them as for i in a1.py a2.py, or you can use file name substitution like for i in * .py. In bash, you can assign each path to the variable ʻi by setting: to the environment variable that represents the delimiter ʻIFS and setting it to for i in $ PATH.

As a concrete example, the following ʻif_for.sh`.

if_for.sh


#!/bin/bash
for i in u.c t.c
do
    echo $i
    cc -c $i
    if [ $? != 0 ] #Here is if test$? !=Same with 0
    then break
    fi
done

cc is the command to compile. Since compilation fails (file names such as u.c and t.c cannot be compiled because they are appropriately decided names), $? Is a non-zero value. The [ command has the form [exp], calculates the expression of ʻexp, and if the result is true, it ends normally (returns 0 as the exit status), otherwise it ends abnormally (1 as the exit status) (Returns) command. The same as the [command is thetest` command. This time, the file u.c doesn't exist (probably), so it's okay if the for loop stops properly.

There are various expressions in the expression ʻexp`, so some of them are shown.

---a filename File filename exists ---d filename File filename exists and is a directory --str1 = str2 String str1 and string str2 are equal --str1! = str2 String str1 and string str2 are not equal --num1 -eq num2 Integer num1 = integer num2 (equal) when compared as a number --num1 -gt num2 Integer num1> Integer num2 (greater than) when compared as a number

  1. while

The grammar of while is as follows.

while commandlist1
do commandlist2
done

This repeats commandlist2 until commandlist1 terminates normally (returns 0 as the exit status). As a concrete example, the following while.sh.

while.sh


while true
do
sleep 10
w
done

This is to execute the w command every 10 seconds. The w command is a command that shows who is logged in and what they are doing. The command after while enters the loop if it completes successfully (ie when$?Is 0). Since the true command only ends normally, it loops forever. There is also a false command, which just terminates abnormally.

  1. case

The grammar of the case is as follows.

case string in
pattern1 ) commandlist1 ;;
pattern2 ) commandlist2 ;;
...
esac

First check if the string string matches pattern1, and if so, execute commandlist1. If there is no match, move to pattern2. The case statement repeats this. The last ʻesacis a reverse description ofcase. As a concrete example, the following case.sh`.

case.sh


#!/bin/bash
for word in *
do
    case $word in
        *.out)
            echo $word is out file;;
        *.sh)
            echo $word is sh file;;
        *)
            echo $word is other file;;
    esac
done

For example, if you have a file called ʻaa.out bb.sh`` cc.txt` in a directory, you can run it.

aa.out is out file
bb.sh is sh file
cc.txt is txt file

Is displayed.

5. Function

Functions can be defined in the form name () {commandlist;}. After executing this definition, executing name as a command will execute the corresponding command list. A concrete example is written below in func.sh.

func.sh


go () {
echo "file name = $1"
if test ! -r $1     # !Means negative
then echo $1 is not found
else echo $1 is found
fi
}
go foo.txt
go bar.txt

If you run it with ./func_script.sh in a directory where the file foo.txt exists and the file bar.txt does not exist

file name =  foo.txt
foo.txt is found
file name =  bar.txt
bar.txt is not found

Will be.

6. Other commands that support the control structure

-The ʻexec command terminates the execution of the shell itself that is currently executing the ʻexec command and executes the specified file. For example, the command ʻexec / bin / sh` will move to sh if it succeeds, and will continue in the shell as it is if it fails.

-The ʻeval command takes a command line command line as an argument and executes it. For example, ʻeval echo hello will only display hello. This is used

  1. When executing the character string itself output by a certain command as a command
  2. When evaluating nested variables Is. An example of 1 is ʻeval $ SHELL --version, which tells you the version. An example of 2 is ʻeval'$' $ temp, which allows you to refer to the contents of the variable pointed to by the variable temp.

-The ʻexit command terminates the execution of the shell. ʻExit expr exits with the value of the expression ʻexpr as the exit status. It is normal to set ʻexit 1 for normal termination and ʻexit 0 for abnormal termination. The expression ʻexpr is optional.

-The source command exists for csh, bash, and zsh. For sh, use .. In the case of bash shell_script, __ will start a new bash process and execute shell_script, but if it is executed in source or ., it will be executed in that shell. If you execute the following source_dot.sh with source and bash respectively, you can see that the current directory after completion is different. However, zsh can no longer replace source with ..

source_dot.sh


echo 1 \$PWD is $PWD
echo 2 ls
ls
echo 3 change dir
cd ..
echo 4 \$PWD is $PWD
echo 5 ls
ls

-The goto command is a command that moves to the line wherelabel:is written when there is a line called goto label.

Signal processing

In bash and sh, you can specify the processing for the signal received in the shell script by using the trap command. With trap command num, the command command is executed when the signal specified by the signal number num occurs. If trap'' num, do not accept the signal specified by the signal number num (execute''). If trap num, the processing of the signal specified by the signal number num is restored. The following loop.sh is introduced as a concrete example.

loop.sh


trap "echo 'you tried to kill me!'" TERM
trap "echo 'you hit CTRL-C!'" INT
while true; do
      sleep 5
      ps
done

If you start it with bash loop.sh, it will continue to execute the ps command every 5 seconds. This process cannot be stopped with Ctrl-C. This is because Ctrl-C sends a signal called INT, but after executing the ʻechocommand, it returns to the while statement again. Even if you stop the process once withCtrl-Z, check the process ID (PID) with the ps command and type the killcommand, this process is still alive (even if you return to the foreground with thefgcommand you The tried to kill me! Is displayed and thepscommand continues to be executed). This is because thekillcommand sends a signal called TERM. If you want to kill this process, use thekill -KILL` command.

Signal sent at logout

Depending on the UNIX settings, logging out may send a HUP (HangUP) signal to all processes running from that terminal. A process that does not specifically process the HUP signal terminates when it receives the HUP signal. The process you want to run after logging out is started with a command after nohup. For example, if you set nohup python aa.py &, python aa.py will continue to work even after you log out.

Complex replacement

When the variable name is stored in the variable n, writing$ {$ n}to retrieve its value does not replace it. This is because after the variable substitution of $ n, the variable substitution is no longer performed. To retrieve the value of that variable, you need to evaluate it again using ʻeval` or the like. Specifically, the following. スクリーンショット 2020-04-04 17.20.45.png

The shell decomposes the command line into arguments after all substitutions (alias substitution, history substitution, variable substitution, command substitution, filename substitution, etc.) are completed. Therefore, if the value of the variable is __ empty string __ or __ contains blanks, note that the __ subscript may shift when getting the nth argument.

Built-in commands

The command executed by the shell itself is called __built-in command __. Built-in commands are called internal commands, and other commands are sometimes called external commands. It is basically impossible for other processes to refer to or change the state of a process in one shell. Therefore, cd, which changes the current working directory of the shell process, is a built-in command. There may be both built-in and external commands, such as the kill command, but the external command kill has the limitation that the job number stored by the shell cannot be used.

Commands that support bash

· Basename and dirname commands The basename command outputs the pathname with the directory and extension removed. If the extension is not specified, the path name with the directory removed is output. The dirname command outputs the pathname with the file name removed. It is easier to understand by looking at the execution example. スクリーンショット 2020-04-04 23.33.26.png

・ ʻExpr command A command that calculates the argument expression and outputs the calculation result to standard output. The format ʻexpr exp. The expression ʻexp` has various forms, some of which are shown.

Designation meaning
exp1 & exp2 formulaexp1And formulaexp2If either is empty or 0, 0 is output, otherwise 0 is output.exp2To output
exp1 = exp2 formulaexp1Whenexp2Outputs 1 if is an integer match, otherwise outputs 0 (if either is not an integer, the strings are compared)
exp1 + exp2 Output the result of addition (other-Or*、/、%Etc. can be used)
length str StringstrPut out the length of
substr string exp1 exp2 Stringstringofexp1From the secondexp2番目of部分文字列を出力する

However, bash has a built-in syntax of $ ((exp)), so you don't have to use this command. This is a command often used in sh. スクリーンショット 2020-04-04 17.38.37.png

About the shell

Various shells are used on UNIX. The most typical shells are sh and csh, but these shells are rarely used nowadays. This is because there is a convenient shell based on these with various enhancements. A brief introduction to a typical shell.

--tcsh is a shell with extended functions based on csh. In particular, there are many enhancements to functions for interactive work such as command line editing. --ksh is a shell with enhanced sh functionality. It was developed by David Korn, so it takes the acronym k. --Bash is a shell developed by the GNU Project. It is based on the syntax of sh. --Zsh is a shell created with the aim of being the "final shell". An ambitious shell that incorporates almost all existing features. It is based on the syntax of sh.

useful function

The relatively new shell has various features not found in sh or csh. The typical functions are as follows.

--File name replacement that specifies a range of numbers --Automatic spelling correction function --Regular command execution function --Advanced redirection --Regular command execution --Execute command at a specified time --Login and logout monitoring function

There are many shells, so find the one that suits you.

At the end

This is the end of the summary. The above content is only the first volume of [UNIX super text](https://www.amazon.co.jp/new The-UNIX-Super-Text-revised and expanded version / dp / 4774116823), so if you have the opportunity, the second volume I might do it. I would appreciate it if you could point out any mistakes.

Recommended Posts

UNIX basics (second part)
Basics of Python × GIS (Part 1)
100 Natural Language Processing Knock Chapter 2 UNIX Command Basics (Second Half)
Basics of Python x GIS (Part 3)
Basics of Python x GIS (Part 2)
Understand the "temporary" part of UNIX / Linux