Basic knowledge of Shell scripting
Blog Directory
Shell Script Basics
II. Redirection and Pipeline Operation
Using Shell Variables
Shell Script Basics
Shell script in Linux system is a special application program, which is between the operating system kernel and the user. It acts as a "command interpreter", responsible for receiving and interpreting the operation commands input by the user, passing the operations to be executed to the kernel for execution, and outputting the execution results.
1, Simple shell script [root@centos01 ~]# vim aaa.sh #!/ bin/bash#Description E-Mail:2807489749@qq.com BY:LYXcd /boot/echo "Show current directory: "pwdecho "View files starting with vml: "ls -lh vml*[root@centos01 ~]# chmod +x aaa.sh
In the a.sh script file above, the first line "#!/ bin/bash"is a special script declaration that indicates that statements after this line are interpreted and executed by the/bin/bash program; other statements that begin with"#"indicate comment information; the echo command is used to output strings to make the output information of the script easier to read. The above configuration consists of three commands: cd /boot/, pwd, ls -lh vml*. After executing this script file, the output is the same as executing these three commands separately in turn, thus realizing the automation process of "batch processing."
By ".../ aaa.sh ", which must be granted X permissions on the file before execution.
[root@centos01 ~]# ./ aaa.sh /boot-rwxr-xr-x. 1 root root 5.7M Oct 23 22:35 vmlinuz-0-rescue-2b580d1a2e8348b8aa9f78be11137b41-rwxr-xr-x. 1 root root 5.7M Aug 23 2017 vmlinuz-3.10.0-693.el7.x86_64[root@centos01 ~]# source aaa.sh [root@centos01 ~]# sh a.sh 2. Redirection and pipeline operation 1. Redirection output
It means to save the normal output result of the command to the specified file and overwrite the original content in the file. If the file does not exist, a new file will be created using the ">" operation symbol.
Indicates appending the normal output of the command to the specified file sh using the ">>" operation symbol.
For example:
[root@centos01 ~]# echo "aaa" aaa[root@centos01 ~]# echo "aaa" > 1.txt [root@centos01 ~]# cat 1.txt aaa[root@centos01 ~]# echo "bbb" >> 1.txt [root@centos01 ~]# cat 1.txt aaabbb2. Redirect input
Redirecting input refers to changing the way input is received in a command from the default keyboard to a specified file rather than waiting for input from the keyboard. Redirect input using "