JSONファイルのテキストの変更(すべてのツールを使用)

JSONファイルのテキストの変更(すべてのツールを使用)

ポート転送機能を持つVPNを使用しており、転送されたポートを取得するスクリプトがすでにあります。そのポートはで開かれますが、転送iptables設定ファイルを起動する前にポートは自動的に変更されません。

マイ転送設定ファイル:以下を/home/vlastimil/.config/transmission/settings.json含みます。

{
    "alt-speed-down": 50,
    "alt-speed-enabled": false,
    "alt-speed-time-begin": 540,
    "alt-speed-time-day": 127,
    "alt-speed-time-enabled": false,
    "alt-speed-time-end": 1020,
    "alt-speed-up": 0,
    "bind-address-ipv4": "0.0.0.0",
    "bind-address-ipv6": "::",
    "blocklist-date": 1591362636,
    "blocklist-enabled": false,
    "blocklist-updates-enabled": true,
    "blocklist-url": "http://list.iblocklist.com/?list=ydxerpxkpcfqjaybcssw&fileformat=p2p&archiveformat=gz",
    "cache-size-mb": 4096,
    "compact-view": false,
    "details-window-height": 1010,
    "details-window-width": 1562,
    "dht-enabled": true,
    "download-dir": "/home/vlastimil/Downloads",
    "download-queue-enabled": false,
    "download-queue-size": 1,
    "encryption": 0,
    "idle-seeding-limit": 30,
    "idle-seeding-limit-enabled": false,
    "incomplete-dir": "/home/vlastimil/Downloads",
    "incomplete-dir-enabled": false,
    "inhibit-desktop-hibernation": true,
    "lpd-enabled": false,
    "main-window-height": 442,
    "main-window-is-maximized": 0,
    "main-window-width": 664,
    "main-window-x": 1256,
    "main-window-y": 570,
    "message-level": 2,
    "open-dialog-dir": "/home/vlastimil/Downloads",
    "peer-congestion-algorithm": "",
    "peer-id-ttl-hours": 6,
    "peer-limit-global": 200,
    "peer-limit-per-torrent": 50,
    "peer-port": 24833,
    "peer-port-random-high": 65535,
    "peer-port-random-low": 49152,
    "peer-port-random-on-start": false,
    "peer-socket-tos": "default",
    "pex-enabled": true,
    "port-forwarding-enabled": false,
    "preallocation": 2,
    "prefetch-enabled": true,
    "queue-stalled-enabled": true,
    "queue-stalled-minutes": 30,
    "ratio-limit": 2,
    "ratio-limit-enabled": false,
    "recent-download-dir-1": "/home/vlastimil/Downloads",
    "recent-download-dir-2": "/home/vlastimil/Downloads/memtest/PRO/torrent",
    "recent-download-dir-3": "/home/vlastimil/Downloads/memtest/Pro",
    "recent-download-dir-4": "/media/vlastimil/4TB_Seagate_NTFS/Movies",
    "rename-partial-files": true,
    "rpc-authentication-required": false,
    "rpc-bind-address": "0.0.0.0",
    "rpc-enabled": true,
    "rpc-host-whitelist": "",
    "rpc-host-whitelist-enabled": true,
    "rpc-password": "{426d7fbcb4015f3821d212e0203d5e20033661141zKMndND",
    "rpc-port": 9091,
    "rpc-url": "/transmission/",
    "rpc-username": "",
    "rpc-whitelist": "127.0.0.1",
    "rpc-whitelist-enabled": true,
    "scrape-paused-torrents-enabled": true,
    "script-torrent-done-enabled": false,
    "script-torrent-done-filename": "/home/vlastimil",
    "seed-queue-enabled": false,
    "seed-queue-size": 10,
    "show-backup-trackers": true,
    "show-extra-peer-details": false,
    "show-filterbar": false,
    "show-notification-area-icon": true,
    "show-options-window": true,
    "show-statusbar": true,
    "show-toolbar": true,
    "show-tracker-scrapes": true,
    "sort-mode": "sort-by-age",
    "sort-reversed": false,
    "speed-limit-down": 2048,
    "speed-limit-down-enabled": false,
    "speed-limit-up": 0,
    "speed-limit-up-enabled": false,
    "start-added-torrents": false,
    "statusbar-stats": "total-transfer",
    "torrent-added-notification-enabled": false,
    "torrent-complete-notification-enabled": false,
    "torrent-complete-sound-command": "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'",
    "torrent-complete-sound-enabled": false,
    "trash-can-enabled": true,
    "trash-original-torrent-files": false,
    "umask": 18,
    "upload-slots-per-torrent": 14,
    "user-has-given-informed-consent": true,
    "utp-enabled": true,
    "watch-dir": "/home/vlastimil/Downloads",
    "watch-dir-enabled": false
}

41行目にあるのに技術的には修正されたのかよく分からないが、この問題のために修正されたのです。


だから私は65535という新しいポート番号を持っています所定の位置に古いポート番号を変更します。

sedorをどのように書くかはわかりませんが、awk少しはあるようですが、極度に単純な。ありがとうございます。

ベストアンサー1

jq最上位peer-portキー値を65535に更新するために使用されます。

file=/home/vlastimil/.config/transmission/settings.json

cp "$file" "$file.tmp" &&
jq '."peer-port" |= 65535' "$file.tmp" >"$file" &&
rm -f "$file.tmp"

新しいポート値とともにシェル変数を使用します。

newport=65535
file=/home/vlastimil/.config/transmission/settings.json

cp "$file" "$file.tmp" &&
jq --argjson new "$newport" '."peer-port" |= $new' "$file.tmp" >"$file" &&
rm -f "$file.tmp"

jq直接編集できないため、結果を元の名前にリダイレクトしながらファイルの一時コピーで実行します。これにより、jq元のファイルで実行するのではなく新しい一時ファイルにリダイレクトし、古いファイルに新しいファイルを移動することで、元のmvファイルのメタデータ(所有権や権限など)が保存されます。

たとえば、シェル変数の値が$newport数値でない場合など、問題が発生した場合、一時ファイルは削除されず、変更されていない文書が保存されます。

おすすめ記事