In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
1. What is a shell script
Shell is a command line interpreter, its function is to follow a certain syntax to interpret the input commands and pass them to the system. It provides users with an interface system-level program to send requests to Linux in order to run the program. Shell itself is a program written in C language, and it is a bridge for users to use Linux.
Simply put, a shell script is a stack of commands.
Basic format of 2.shell script
The beginning of the script: the first line, write in the top box
#! / bin/bash
Comment information: lines starting with "#" are ignored in shell scripts, so in order to facilitate their own and others to view, then we usually add comment information in the script to facilitate later viewing. As the ancients said: scripts that do not add notes are all hooligans. The usual comment information can be written as follows:
#! / bin/bash# application# versions name# email
Variables of 3.shell script
Local variables:
A normal variable, valid only in the shell in which it was created
Local variables:
For the variables in the current shell, local variables contain environment variables, and non-environment variables of local variables do not have inheritance.
Environment variables:
Valid for the shell that created it and its derived child processes
Position parameter variables:
$1 for 2, 3 for 3.
Special variables:
$0: script file path itself
$#: number of script parameters
$*: all parameters (subsequent parameters are represented by multiple strings)
$@: all parameters (the following parameters are represented by a string)
The basic Operation of 4.shell script
The basic operational symbols in shell scripts are:
+-* /% * *
Because shell is a weakly typed programming language, we need to write this when doing arithmetic operations:
Let VAR=$num1 op $num2 VAR=$ [expression] VAR=$ ((expression)) VAR=$ (expr argu1 argu2 argu3)
Example: calculation of 6 to 8
Let VAR=6+8 VAR=$ [6: 8]
Enhanced assignment:
The variable is saved back to this variable after doing some arithmetic operation
Example:
Self-increase: let VAR+=1 self-minus: let VAR-=1.
Conditional testing of 5.shell scripts
Usually, to judge whether a requirement is met or not, it needs to be implemented by the testing mechanism.
You can use "$?" To judge whether the previous order is correct or not
How to write test expressions to implement the required tests:
Execute the command and use the status return value of the command to determine:
0: successful
1-255: failed
Test expression:
Test EXPRESSION
[EXPRESSION]
`EXPRESSION'
Then the test types of bash are basically divided into three categories:
(1) numerical testing: numerical comparison
-eq: whether it is equal to
-ne: is it not equal to
-gt: whether it is greater than
-ge: whether it is greater than or equal to
-lt: whether it is less than
-le: whether less than or equal to
Example: judge whether 2 is greater than 3
(2) string testing: character comparison
= =: is it equal to
>: whether it is greater than
< :是否小于 !=:是否不等于 =~:左侧字符串是否能被右侧的PATTERN所匹配 例:判断主机名为空,或者为localhost.locadomain,或者包含localhost,或者包含linux,则通通将其设为 www.magedu.com #!/bin/bash#H=$(hostname)[[ "$H" == " " || "$H" =~ "localhost.locadomain" || "$H" =~ "localhost" || "$H"=~"linux" ]] && hostname www.magedu.com (3)文件测试: 存在性测试: -a FILE -e FILE 文件的存在性测试,存在则为真,否则则为假 存在性及类型测试: -b FILE:是否存在为块设备文件,是则为真,否则为假 -c FILE:是否存在为字符设备文件 -d FILE:是否存在为目录文件 -f FILE:是否存在为普通文件 -h FILE或 -L FILE :是否存在为符号链接文件 -p FILE :是否存在为命令管道文件 -S FILE:是否存在为套接字文件 文件权限测试: -r FILE :是否存在并且对当前用户可读 -w FILE :是否存在并且对当前用户可写 -x FILE :是否存在并且对当前用户可执行 特殊权限测试: -u FILE :是否存在并且拥有SUID权限 -g FILE :是否存在并且拥有SGID权限 -k FILE :是否存在并且拥有sticky权限 文件是否有内容: -s FILE :文件是否存在并且非空,有则为真,没有则为假 时间戳: -N FILE:文件自从上一次读操作后是否被修改过 从属关系测试: -O FILE :当前用户是否为文件的属主 -G FILE :当前用户是否为文件的属组 双目测试: FILE1 -ef FILE2 :FILE1与FILE2是否指向同一个文件系统的相同inode的硬链接 FILE1 -nt FILE2 :FILE1是否新于FILE2 FILE1 -ot FILE2 :FILE1 是否旧于FILE2 例:通过命令行参数,给定两个文本文件名,如果某文件不存在,则结束脚本执行,若都存在时,返回每个文件的行数,并echo出其中行数较多的文件名 #!/bin/bash#[ $# -ne 2 ] && echo "请给定两个文本文件名" && exit 2[ -e "$1" ] && file1=$(cat "$1" | wc -l) || exit 3[ -e "$2" ] && file2=$(cat "$2" | wc -l) || exit 4[ "$file1" -gt "$file2" ] && echo "$1的行数为$file1" || echo "$2的行数为$file2" 6.shell脚本之选择执行 过程式编程语言的代码执行顺序: 顺序执行:逐条执行 选择执行: 代码有一个分子:条件满足时才会执行 两个或以上的分支:只会执行其中一个满足条件的分支 循环执行: 代码片段(循环体) 要执行0、1或多个来回 shell脚本中的选择执行: 单分支的if语句: if 测试条件;then 代码分支 fi 或者: if 测试条件 then 代码分支 fi 双分支的if语句: if 测试条件;then 条件为真时执行的分支 else 条件为假时执行的分支 fi 多分支的if语句: if 测试条件;then 条件为真时执行的分支 elif 测试条件;then elif 测试条件;then elif 测试条件;then ...... else 条件为假时执行的分支 fi 例: 1.通过参数传递一个用户名给脚本,此用户名如果不存在的话,则进行添加 #!/bin/bash#if [ "$#" -ne 1 ];then echo "请输入一个用户名" exit 2fiuser=$(grep "^$1\>"/ etc/passwd) if! [- s" $user "]; then useradd" $1 "echo" user $1 has added "fi"
two。 Through the command parameters, two numbers are given, and the larger number is output.
#! / bin/bash#if ["$#"-ne 2]; "then echo" Please give two numbers "exit 2fiif [" $1 "- gt" $2 "]; then echo" $1 "elif [" $1 "- eq" $2 "]; then echo" $1 "2" else echo "$2" fi
Command interaction of 7.shell script
Read [OPTIONS]...
-pendant PROMPT' prompt
-t TIMEOUT
Example:
1. Write a script that prompts the user for a string, and exits if the input is quit. Otherwise, the script content of the string entered is displayed.
#! / bin/bash#read-p "Please enter a string:" Aif ["$A" = = quit]; then exit 2else echo "$A" fi
two。 Background: the company has a new employee and needs to open a system account for the new employee and count the information of the new employee. (by means of interaction)
Let the user specify a user name and password. If the user name exists before, delete it first. Then add the system account for the user.
After completion, you need to count the employee's mobile phone number, email,QQ number and age information, which are collected and stored in the user's home directory.
To complete the above operation, ask the user whether it is necessary to set up a separate working directory for the user as / data/username. The default is to do so. If not, enter n or N
#! / bin/bash#read-p "Please enter the user name:" usergrep "^ $user\ >" / etc/passwd > / dev/null & & userdel-r $useruseradd $userread-p "Please enter the user password:" pswdecho $pswd | passwd-- stdin $user > / dev/nullread-p "Please enter the mobile number:" numberread-p "Please enter the email:" mailread-p "Please enter the QQ number:" qqread-p "Please enter the age information:" ageecho "Mobile number: $number,email:$mail,QQ:$qq Age: $age "> / home/$user/xinxi.txtread-p" need to set up a working directory [Y] "Aif [" $A "= = Y] Then mkdir-p / date/$user echo "user created, working directory created" else echo $An echo "user created" fi "
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.