我有一個 bash 腳本來找出某個目錄下的所有非空檔案。目前它僅在找到時列印檔案名。我想再添加一行以十六進制格式列印前 32 個位元組。
#!/bin/sh
files=$(find /data/ -type f ! -empty)
for f in $files;
do
if [ -f "$f" ]; then
tr -d '\000' <$f | tr -c '\000' '\n' | grep -q -m 1 ^ || echo $f
fi
done
我嘗試在最后添加一個“&& xxd -g 1 -l 32 $f”,但它不起作用!
uj5u.com熱心網友回復:
從檔案中獲取前 32 個字符:
dd if=so.bash ibs=32 count=1 2>/dev/null | od -h
dd獲取前 32 個字符od -h以十六進制格式列印它們
你也可以用 xxd 來做
xxd -l 32 $f
$f檔案在哪里
#!/bin/bash
files=$(find /data/ -type f ! -empty)
for f in $files
do
if [ -f "$f" ]; then
tr -d '\000' <"$f" | tr -c '\000' '\n' | grep -q -m 1 ^ || echo $f
xxd -l 32 "$f"
echo ""
fi
done
- 這
echo ""是在每個檔案之間有一個空行來拆分輸出。
uj5u.com熱心網友回復:
建議一行gawk腳本:
gawk 'BEGINFILE{print FILENAME}/[^\x00]/{system("xxd -l 32 "FILENAME;nextfile}' $(find /data/ -type f ! -empty)
gawk解釋
BEGINFILE{print FILENAME}
BEGINFILE{ # before processing a file
print FILENAME; # print the filename
}
/[^\x00]/{system("xxd -l 32 "FILENAME);nextfile}
/[^\x00]/{ # if records contains non NULL character
system("xxd -l 32 "FILENAME); # print first 32 hex charachters
nextfile; # read next file
}
uj5u.com熱心網友回復:
在 bash shell 中運行的每個命令都會回傳一個存盤在 bash 變數“$?”中的值。然后可以將命令拆分如下:
#!/bin/sh
files=$(find /data/ -type f ! -empty)
for f in $files;
do
if [ -f "$f" ]; then
tr -d '\000' <$f | tr -c '\000' '\n' | grep -q -m 1 ^
if [ $? -ne 0 ]; then
echo $f
xxd -l 32 $f
fi
fi
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483483.html
標籤:重击
