Open azl397985856 opened 4 years ago
declare -a array。 比如:
declare -a badstrings=("Failed password for invalid user"
"input_userauth_request: invalid user"
"pam_unix(sshd:auth): check pass; user unknown"
"input_userauth_request: invalid user"
"does not map back to the address"
"pam_unix(sshd:auth): authentication failure"
"input_userauth_request: invalid user"
"reverse mapping checking getaddrinfo for"
"input_userauth_request: invalid user"
)
${#array[@]} 或者 ${#array[*]}
array[n] 其中n为从0到 ${#array[*]} - 1的整数
filename=(`ls`)
for var in ${filename[@]};do
echo $var
done
array[n] = 'modified' 其中n为从0到 ${#array[*]} - 1的整数
Bash 内置的从标准输入读取数据的接口。
- 从键盘输入中读取数据:
read variable
- 从键盘输入中读取数据,并转化为数组:
read -a array
- 限制最大允许读取的字节数为character_count:
read -n character_count variable
- 使用其他分隔符,而不是默认的换行:
read -d new_delimiter variable
- 不转义:
read -r variable
- 展示prompt:
read -p "Enter your input here: " variable
- 静默模式:
read -s variable
例子:
FILE=/etc/passwd
while read line
do
# store field 1
F1=$(echo $line|cut -d$FS -f1)
# store field 2
F2=$(echo $line|cut -d$FS -f6)
# store field
F3=$(echo $line|cut -d$FS -f7)
echo "User \"$F1\" home directory is $F2 and login shell is $F3"
done < $FILE
配置防火墙规则。
- View chains, rules, and packet/byte counters for all tables:
sudo iptables -vnL
- Set chain policy rule:
sudo iptables -P chain rule
- Append rule to chain policy for IP:
sudo iptables -A chain -s ip -j rule
- Append rule to chain policy for IP considering protocol and port:
sudo iptables -A chain -s ip -p protocol --dport port -j rule
- Delete chain rule:
sudo iptables -D chain rule_line_number
- Save iptables configuration of a given table to a file:
sudo iptables-save -t tablename > path/to/iptables_file
- Restore iptables configuration from a file:
sudo iptables-restore < path/to/iptables_file
常见的rule有DROP 和 ACCEPT,分别表示黑名单和白名单。 更多参考:https://www.thegeekstuff.com/2011/06/iptables-rules-examples/
脚本化操作文本。
- Replace the first occurrence of a string in a file, and print the result:
sed 's/find/replace/' filename
- Replace all occurrences of an extended regular expression in a file:
sed -E 's/regex/replace/g' filename
- Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):
sed -i '' 's/find/replace/g' filename
- Replace only on lines matching the line pattern:
sed '/line_pattern/s/find/replace/' filename
- Print only text between n-th line till the next empty line:
sed -n 'line_number,/^$/p' filename
- Apply multiple find-replace expressions to a file:
sed -e 's/find/replace/' -e 's/find/replace/' filename
- Replace separator / by any other character not used in the find or replace patterns, e.g., #:
sed 's#find#replace#' filename
比如:sed -r 's/\s+/,/' 是变成类似CSV的格式。
变量,数组 $$ pid $? last returned value ${} 和 $() read mktemp trap iptables sed