Linux shell script read file line by line All In One
Linux shell script read file line by line All In One
Linux shell 脚本逐行读取文件
I just want to replace thegrep
command, and filter out the real IP address 192.168.18.195
with native
shell syntax
.
#!/usr/bin/env bash
IPs=$(ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b')
echo $IPs
# 192.168.18.195
ifconfig.txt
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether a4:83:e7:91:62:79
inet6 fe80::1ca2:3b0a:df9d:465f%en1 prefixlen 64 secured scopeid 0x7
inet 192.168.18.195 netmask 0xffffff00 broadcast 192.168.18.255
inet6 fd80:eae6:1258:0:37:7544:1d1:7b08 prefixlen 64 autoconf secured
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
https://gist.github.com/xgqfrms/a9e98b17835ddbffab07dde84bd3caa5
IP
while..do..done
bash loop
# 多个命令写在同一行上,需要使用 `;` 符号分割
while read -r line; do COMMAND; done < input.file
ip-filter.sh
#!/usr/bin/env bash
# 写死文件的绝对路径 👎
# input="/absolute/path/to/file.txt"
# 动态读取 参数 ✅
input=$1
# while IFS= read -r line
while read -r line
do
echo "$line"
# match regex ???
done < "$input"
$ bash ./ip-filter.sh ifconfig.txt
# OR
$ chmod +x ./ip-filter.sh
$ ./ip-filter.sh ifconfig.txt
https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/
demos
sed
filter IP
使用 sed 代替 grep
# sed regex❓
TODO fix sed ❌
#!/usr/bin/env bash
# 写死文件的绝对路径 👎
# input="/absolute/path/to/file.txt"
# 动态读取 参数 ✅
input=$1
rgexp='192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b'
# while IFS= read -r line
while read -r line
do
echo "$line"
# match regex ???
# rgexp.test($line)
# echo "$line" | sed -e 's/rgexp//' -n
# echo "$line" | sed -e 's/192\.168/✅/' -n
# echo "$line" | sed -e 's/192\.168/✅/'
# printf "$line" | sed -e 's/192\.168/✅/' > ./ip.md
# printf "$line" | sed -e 's/192\.168/✅/' > ./ip.md
# if echo "$line" | sed -e 's/192\.168/✅/' -n
if echo "$line" | sed -e 's/192\.168/✅/'
then
echo "✅"
else
echo "❌"
fi
done < "$input"
<<EOF
$ echo "this is abc" | sed -e 's/abc/xyz/'
this is xyz
# 不输出
$ echo "this is abc" | sed -e 's/abc/xyz/' -n
EOF
# $ ./ip-filter.sh ./ifconfig.txt
# https://www.cnblogs.com/xgqfrms/p/17362135.html
https://www.cnblogs.com/xgqfrms/p/16824934.html
# man sed
$ man sed
# man sed
$ man sed
grep
filter IP
$ ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b'
$ ifconfig | grep -woE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])'
$ ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])[[:space:]]'
$ ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b'
$ ifconfig | grep -woE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])'
$ ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])[[:space:]]'
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
shell script if then
/ else
/ elseif then
/ fi
if else 的 [...]
判断语句中大于使用 -gt
,小于使用 -lt
。
如果使用 ((...))
作为判断语句,大于和小于可以直接使用 >
和 <
。
https://www.runoob.com/linux/linux-shell-process-control.html
shell script & multi-line comments
<<EOF
&EOF
#!/usr/bin/env bash
# TODO: python3 & pip3 install checker
# if not installed then
# # install promoto
# echo "❌"
# else
# echo "✅"
# fi
rm -rf ./python_package/dist/*.*
# ⚠️ 必须要先进入 python project package 的目录,才能执行构建打包和上传文件
cd ./python_package/
# && 串行
./build.sh && ./upload.sh
<<EOF
"""
# && 串行
./python_package/build.sh && ./python_package/upload.sh
ERROR Source /Users/xgqfrms-mm/Documents/github/math_package_project does not appear to be a Python project: no pyproject.toml or setup.py
build ✅
ERROR InvalidDistribution: Cannot find file (or expand pattern): 'dist/*'
upload ✅
"""
EOF
https://pypi.org/project/math-package-xgqfrms/0.0.2/
https://www.geeksforgeeks.org/multi-line-comment-in-shell-script/
https://stackoverflow.com/questions/43158140/way-to-create-multiline-comments-in-bash
Linux bash shell 脚本
多行注释
/ 块级注释
Here documents
&Here strings
https://bash.cyberciti.biz/guide/Here_documents
https://bash.cyberciti.biz/guide/Here_strings
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17362135.html
未经授权禁止转载,违者必究!