我有一个大约 12300 行的大文件,看起来与此类似。
001.domain.com=001.somedomain.com:10001
002.domain.com=002.somedomain.com:10002
003.domain.com=003.somedomain.com:10003
Run Code Online (Sandbox Code Playgroud)
我希望文件在完成后看起来像这样
001.domain.com=IP_Address_of_001.somedomain.com:10001
002.domain.com=IP_Address_of_002.somedomain.com:10002
003.domain.com=IP_Address_of_003.somedomain.com:10003
Run Code Online (Sandbox Code Playgroud)
所以基本上我需要在= 符号后找到并替换主机名与 IP 地址。
如果有人能指出我正确的方向,我将不胜感激。
Ubuntu 16.04
#!/bin/bash
site="hello"
wDir="/home/websites/${site}/httpdocs/"
for file in $(find "${wDir}" -name "*.css")
do
echo "$file";
done
exit 0;
Run Code Online (Sandbox Code Playgroud)
即使我定义了开始目录,shellcheck 也会警告我,但脚本工作得很好。
root@me /scripts/ # shellcheck test.sh
In test.sh line 6:
for file in $(find "${wDir}" -name "*.css")
^-- SC2044: For loops over find output are fragile. Use find -exec or a while read loop.
Run Code Online (Sandbox Code Playgroud) Ubuntu 16.04
bash -version
GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)
Run Code Online (Sandbox Code Playgroud)
我想要grep2 个模式,然后将它们并排列出。目前,这就是我所拥有的:
root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json: "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json: "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",
Run Code Online (Sandbox Code Playgroud)
这就是我想要的,尽管任何类似的东西实际上都可以。
root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json: tire_id: 1815123428733289, appID: 18412365908966538
Run Code Online (Sandbox Code Playgroud)
文件示例在这里:
{
"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client": {
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": XXXX,
"vendor": "default"
},
"locale": "en_US", …Run Code Online (Sandbox Code Playgroud) Ubuntu 16.04
即使 for 的输出client in */; do不会产生尾部斜杠,如果我在循环内对文件执行时间测试时回显变量 $client ,则会出现尾部斜杠。
cd "$wDir"
for client in */; do
cd "$wDir"/"$client";
#-- check to see if any .csv files exists
if ls *.csv &>/dev/null; then
for csvfile in *.csv; do
if test $(find "$csvfile" -mmin +2880); then
echo "$client has files older than 2 days ..." >> "staleFtpAccts"
fi
done
fi
done
Run Code Online (Sandbox Code Playgroud)
当我执行脚本时,一个 / 被放置在 $client 变量之后,如下所示:
root@me /home/frank # bash script.sh
Start ...
000029_000020/ has files older than 2 …Run Code Online (Sandbox Code Playgroud) 如何在 Bash 脚本中测试 Nginx 配置文件?目前我在 shell 中使用 -t :
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)
但我想在脚本中做到这一点?