小编Cur*_*Sam的帖子

在文件中查找主机名并将其替换为 IP 地址

我有一个大约 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 地址。

如果有人能指出我正确的方向,我将不胜感激。

sed shell-script text-processing cut

4
推荐指数
1
解决办法
2926
查看次数

即使给定了开始搜索的路径,shellcheck 也会警告 find 输出上的循环

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)

bash find shellcheck

3
推荐指数
2
解决办法
1260
查看次数

我可以搜索 2 个模式并将它们并排列出吗?

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)

grep json

3
推荐指数
1
解决办法
1446
查看次数

如何从定义时间测试输出的目录的变量中删除尾部斜杠

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 slash delete

2
推荐指数
1
解决办法
1748
查看次数

如何测试 nginx 配置文件在 Bash 脚本中是否有效?

  • Ubuntu 16.04
  • Bash 版本 4.4.0
  • nginx 版本:nginx/1.14.0

如何在 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)

但我想在脚本中做到这一点?

scripting configuration nginx

1
推荐指数
1
解决办法
3327
查看次数