How does Linux systematically learn the shell method

Table of contents

foreword

Common commands of the shell

Shell common scenarios

demo of the shell

Summary of Shell Advantages


 

foreword

If you want to learn Linux shell systematically, here are some suggestions:

  1. Learn the basics: Learn about basic Linux concepts, commands, and filesystem structures. Learn commonly used shell commands, such as ls, cd, mkdir, rm, etc., and understand their usage and parameters.

  2. Practical operation: deepen understanding through practical operation. Install a Linux distribution on your computer, you can choose a common distribution such as Ubuntu or CentOS, and execute various commands through the terminal. Try tasks such as creating, deleting, and copying files and directories to familiarize yourself with common operations.

  3. Learn script programming: Shell script is a script program written to run in the shell environment. Learning shell scripting can help you automate tasks and write complex sequences of commands. Learn basic concepts such as script syntax, variables, conditional judgment, loops and functions, and practice by writing simple scripts.

  4. Read documents and books: Official documents are an important resource for learning the Linux shell. You can consult the man pages (man pages) of various commands to understand the usage and parameters of the commands. Also, there are many good Linux shell programming books for in-depth study and understanding.

  5. Participate in communities and forums: Join Linux communities and forums to communicate and share experiences with other users. This provides access to additional learning resources, problem solving, and the ability to learn from other people's experiences.

  6. Hands-on projects: Try applying your shell knowledge through hands-on projects. For example, write a backup script, log analysis script, or system monitoring script, etc. This will exercise your skills and gain a deep understanding of shell usage scenarios.

Remember, learning the Linux shell takes time and practice, and continuous learning and practice is the key to improving your skills. By constantly trying, exploring and solving problems, you will gradually master and skillfully use the Linux shell.

Common commands of the shell

  1. ls: Display files and folders in a directory
  2. cd: switch working directory
  3. mkdir: create a new directory
  4. rm: delete a file or directory
  5. mv: move or rename a file or directory
  6. cp: copy a file or directory
  7. pwd: display the current working directory
  8. cat: display file content
  9. less: display file content in pages
  10. grep: search for a string in a file
  11. find: search for a file or directory
  12. sort: Sort the contents of the file
  13. chmod: modify file or directory permissions
  14. exit: exit the current shell

Shell common scenarios

Commonly used shells can accomplish the following tasks in Linux systems:

  1. File and directory management: through shell commands, you can create, delete, copy, and move files and directories, as well as view and modify file permissions, ownership, etc.

  2. System management: You can use shell commands to manage user accounts, including creating, deleting, and modifying user account information, as well as setting user permissions and passwords.

  3. Network management: Through shell commands, you can configure network interfaces, view network status, set firewall rules, and execute network diagnostic tools, etc.

  4. Process management: You can use shell commands to view the processes running in the system, and to start, stop and manage processes.

  5. System monitoring: You can view the usage of system resources, such as CPU, memory, disk, etc., and check system logs and error messages through shell commands.

  6. Scripting: A shell script is a script program that automates tasks by writing a series of commands. Scripts can be written to batch process files, perform scheduled tasks, automate system configuration, and more.

  7. Data processing and analysis: Text data can be processed through shell commands, such as extraction, filtering, sorting, statistics, etc., and tools such as Awk and Sed can also be used for more complex text processing and data analysis.

  8. Software package management: In Linux systems, commonly used software package management tools such as apt, yum, dnf, etc. can install, update and delete software packages and manage software warehouses through shell commands.

In general, the Linux shell provides a wealth of commands and tools that can be used for various tasks such as system management, file operations, network configuration, process control, scripting, and data processing. It is an important tool for interacting with the operating system, which can greatly improve efficiency and flexibility.

demo of the shell

Output the current time every 30 seconds.

#!/bin/bash

while true; do
    echo "当前时间:$(date)"
    sleep 30
done

Save the above code as a file named timer.sh, and add execution permission to the file (chmod +x timer.sh). Then execute ./timer.sh in the terminal, and the current time will be printed every 30 seconds.

The while true; do in the script means an infinite loop, echo "current time: $(date)" prints the current time, and the sleep 30 command makes the script pause for 30 seconds.

You can customize the interval time and output format of the timer according to your needs.

Effect: 

This is a suggested timer, if using a real timer you should be using: crontab

The following is an example of using the crontab command to set up a cron job:

  1. Edit scheduled tasks: Use the following command to edit the scheduled tasks of the current user:

    crontab -e

    This will open a text editor where cron tasks can be added or modified.

  2. Add a scheduled task: In the opened text editor, add a scheduled task in the following format:

     * * * * command

    where * * * * *is a time expression representing the minute, hour, date, month, and week position. commandis a command to be executed periodically.

    For example, to run a script file every day at 9:00 /path/to/script.sh, add the following line:

    0 9 * * * /path/to/script.sh
  3. Save the scheduled task: After editing the scheduled task, save and close the text editor.

  4. View scheduled tasks: You can use the following command to view the list of scheduled tasks of the current user:

    crontab -l
  5. Delete scheduled tasks: Use the following command to delete all scheduled tasks of the current user:

    crontab -r

This is just a simple example, you can set more complex scheduled tasks according to your specific needs. Note that crontab's time expressions support more advanced syntax, such as using wildcards, ranges, commas, etc.

Summary of Shell Advantages

Shell (Shell Script) is an interpreted programming language with several advantages:

  1. Easy to learn: Shell syntax is simple and intuitive, easy to learn and use. It is based on the command line interface, which can interact and operate by entering commands.

  2. Powerful scripting capabilities: Shell scripts can automate a series of operations, such as file processing, system management, task scheduling, etc. It supports advanced syntax such as conditional judgment, loop control, and function definition, making it easy and convenient to write complex scripts.

  3. Wide platform support: Shell scripts can run on almost all Unix/Linux systems, including various distributions and operating systems. This makes it a powerful tool for cross-platform development and management.

  4. System-level access rights: Shell scripts can directly access various resources and functions of the operating system. For example, you can manage files and directories, execute system commands, manage processes, and more. This makes shell scripts very useful in system administration, automated deployment, and system monitoring.

  5. Integration with other tools: Shell scripts can be seamlessly integrated with other command line tools and programs. By calling other commands and programs, you can extend the functionality of shell scripts and leverage existing tools to accomplish more complex tasks.

  6. Rapid prototyping: Shell script is a powerful tool for rapid prototyping, which can quickly write and test code, quickly verify ideas and solve problems. It does not need to be compiled, it can be executed directly, which improves the development efficiency.

Although Shell scripting may not be as good as other programming languages ​​in terms of performance and complexity, it can still be a very valuable tool when it comes to system administration, automating tasks, and quick scripting.

Guess you like

Origin blog.csdn.net/feng8403000/article/details/131612744