Home >Operation and Maintenance >Linux Operation and Maintenance >What exactly is a shell script? how to use?
Perhaps, many people have heard the names shell or bash, but do you know what they are?
Let’s go back to the basic knowledge of computers: All computers are composed of hardware and software. Hardware is the part that everyone can touch and see, such as: keyboard, screen, CPU, memory body, hard drive, etc. Without hardware, the so-called computer would not exist, because the input, output and calculation of the entire system are inseparable from hardware. May I ask: How do you use a computer without a keyboard and screen? However, the input you make through the keyboard and the output you see on the screen are really made possible by the software. The software directly responsible for communicating with these hardware is the so-called kernel. The kernel must be able to take over keyboard input, then hand it over to the CPU for processing, and finally output the execution results to the screen. Of course, except for the keyboard and screen, all hardware must be supported by the kernel before it can be used.
So, how does the kernel know what we input on the keyboard? That's what the shell we introduce here is responsible for. Because the data processed by the computer itself is binary machine code, which is very different from the language we humans are accustomed to using. For example, if we enter the pwd command, we know that this means print working directory (a very simple human voice), but as the kernel, it does not know what pwd is. The kernel only looks at the machine code. At this time, the shell It will help us translate pwd into code that the kernel can understand. Therefore, when we use computers, we basically deal with the shell, rather than directly communicating with the kernel, let alone directly controlling the hardware.
Backup website and database script developed using batch program bat under Windwos
@echo off set date=%date:~0,4%-%date: ~5,2%-%date:~8,2% mysqldump -uroot -ppassword -A -B > D:\back\"%date%".sql rar.exe a -k -r -s -ml D:\bak\"%date%".sql.rar D:\bak\"%date%".sql del D:\bak\*.sql rar.exe a -k -r -s -ml D:\bak\"%date%"htdocs.rar D:\work\PHPnow\htdocs
Clear /var/log/message system log file command script
# 使用root身份运行这个脚本 # 清除日志版本,版本:v1 cd /var/log cat /dev/null > messages echo "Logs cleaned up."
Problem:
1. Cannot execute without root execution permission
2. There is no process control to determine whether the file exists
LOG_DIR=/var/log # $UID为0的时候,用户才具有root用户的权限 ROOT_UID=0 # 使用root用户来运行 if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit 1 fi cd $LOG_dir || { echo "Can't change to necesary directory." >&2 exit 1 } cat /dev/null > messages echo "Logs cleaned up." exit 0 # 推出之前返回0表示成功,返回1表示失败
The above is the detailed content of What exactly is a shell script? how to use?. For more information, please follow other related articles on the PHP Chinese website!