Linux and Mac are the systems I have come into contact with most recently. As a user who has transferred from Windows, I did not get used to it at the beginning. But as YOU get to know Linux, it turns out that Linux is programmer friendly. I feel the work is more efficient and convenient for dealing with some batch tasks and scheduled tasks, so I also want to talk about shell programming in depth.

Linux commands are necessary. And most of the time you manipulate files with Python, but you can use shell script to achieve the same effect.

shell

First of all, let’s look at what a shell is. The shell itself is also a shell and shell. In fact, the shell is a bridge between the user and the kernel of the system. Linux and UNIX-like systems support shell script. Shell script is a literal translation program that does not need to be compiled and executed directly.

There are many types of Linux shells. Popular shells include Ash, bash, KSH, CSH, ZSH, etc. With more types, there are requirements for standardization, which is where POSIX comes from. The bash shell is the main one here, and the other shells are pretty much the same. You can view the types of shells supported by the system by running the following command.

cat /etc/shells
Copy the code

Bash shell

BASH (Bourne Again SHell) is popular because of its powerful functions. Specifically, one of them is the auto-completion function. For example, when we use the ls command, pressing the TAB key will automatically search the directory or file name in the specified directory and give the auto-completion function according to your input.

Then you can go through the following

which bash
Copy the code

Check the location of bash

hello world

We can create our first shell script and call it hello.sh so the extension doesn’t really mean anything, we can use it entirely, but in addition to representing the file type, the advantage is to tell an IDE like VSCode that this file shell script, These ides will beautify them or support friendly prompts that make it easy to read and develop shell scripts

-rw-r--r--   1 jangwoo  staff     0  3 13 11:27 hello.sh
Copy the code
#! /bin/bash
Copy the code

The #! Next we define the script interpreter, which can be specified as the bash location we just queried through which bash, which we will use next

#! /bin/bash
echo "Hello world"
Copy the code

To execute a script, run the hello.sh script using bash hello.sh or./hello.sh.

-bash: ./hello.sh: Permission denied
Copy the code
chmod +x hello.sh
Copy the code

annotation

In shell script, a comment is marked with the # symbol, starting with the # line, the entire line is commented, and if # appears on the line, everything from # to the end of the line is commented as follows

#! /bin/bash
# this is a comment
echo "Hello world" #this is a also comment
Copy the code

variable

In shell script variables are mainly divided into system variables and user variables. System variables are created and maintained by the system as the name implies, while user variables are created and maintained by users.

#! /bin/bash
# this is a comment
echo "Hello world" #this is a also comment

echo $BASH
echo $BASH_VERSION
echo $HOME
echo $PWD
Copy the code

Use an equal sign to assign a value to a defined variable. Note that there is no space on either side of =. You do not need to define a variable with a keyword like var as in other languages.

name=zidea
echo $name
echo the username $name
Copy the code

If a variable value contains Spaces, it needs to be wrapped in either single or double quotation marks

title='machine learning tut'
echo $title
Copy the code

How do I read user input

You can use a space after read followed by a variable to receive user input.

#! /bin/bash

echo "Enter title:"
read title
echo "Enter title: $title" 
Copy the code

We can read multiple variables, these variables with a space between the interval to distinguish, then can accept multiple user input, the user input with a space to distinguish between corresponding to the different variables, if the user input can’t correspond, if less than the number of variables, this is the most on the left side of the first variable for alignment, the remaining variable assignment, If the user enters more than the number of variables, the extra input is assigned to the last (right-most) variable

echo "Enter title:"
read title subtitle level
echo "Enter title: $title"
echo "Enter subtitle: $subtitle"
echo "Enter level $level" 
Copy the code

Note that when we put $title in a statement, we need to use double quotes around the statement

Enter title:
machine leanring deep learning 3
Enter title: machine
Enter subtitle: leanring
Enter level deep learning 3
Copy the code

Here are some options for read: -p indicates that the user input is on the same line as the prompt for read, and -s indicates that the user input is no longer displayed on the screen.

echo "Enter title:"
read -p 'title:' title
echo "title: ${title}"
Copy the code
#! /bin/bash

echo "Enter username and password:"
read -p 'username:' username
read -sp 'password:' password
echo "username: ${username}, password ${password}"
Copy the code

You can take as many inputs as you want and store them in an array in a variable by using -a

echo "input titls"
read -a titles
echo "title ${titles[0]},title ${titles[1]}"
Copy the code

If read is not followed by a receive input variable, as follows, we can use the $REPLY variable to receive user input

#! /bin/bash
echo "input titls"
read 
echo $REPLY
Copy the code