簡體   English   中英

cat在一行中顯示多行文件內容

[英]cat displays multi-line file content in a single line

在Linux Shell腳本中,我試圖在日志文件中跟蹤每個命令狀態(“成功”或“失敗”)。

下面是我的代碼片段,用於將條目記錄到日志文件中。

scp $user@$host:$from $to 2>error.txt

command_run_status=$?

if [[ $command_run_status == 0 ]]; then log_message="secure copy on ("${host}") with ("${user}") is successful"; else log_message="error copying files "`cat error.txt`; fi

./logging.sh "$CURRENT_PID" "$log_message" "D"

使用以下條目創建日志文件:

DEBUG 2019-06-21 10:05:35,347 BST [pid1234] [autouser]: error copying files usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 [...] [[user@]host2:]file2

但是,我期望下面的日志條目-error.txt文件內容帶有換行符。

DEBUG 2019-06-21 10:05:35,347 BST [pid1234] [autouser]: error copying files

usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]

           [-l limit] [-o ssh_option] [-P port] [-S program]

           [[user@]host1:]file1 [...] [[user@]host2:]file2

error.txt的內容如下:

usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]

           [-l limit] [-o ssh_option] [-P port] [-S program]

           [[user@]host1:]file1 [...] [[user@]host2:]file2

有人可以評論顯示多行文件內容的原因是在日志文件中以單行顯示嗎?

要在日志文件中打印錯誤文件內容(帶有換行符),命令需要進行哪些更改?

`cat error.txt`

Bash通過在子shell環境中執行命令並將命令替換替換為命令的標准輸出來執行擴展,並刪除所有尾隨的換行符。 嵌入的換行符不會被刪除, 但是可以在分詞過程中將其刪除

為防止單詞分裂,請在雙引號中包括命令替換。

echo $(printf 'a\nb\nc')

版畫

a b c

echo "$(printf 'a\nb\nc')"

版畫

a
b
c

(最好將$(command)替換為舊式的`command`)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM