#!/bin/bash
cd /usr/local/service/hadoop/etc/hadoop
file=hdfshosts
if [ ! -f "$file" ];then
echo "$file not exists"
exit -1
fi
bak_file="$file.txt.bak"
if [ ! -f "$bak_file" ];then
cp -f $file $file.txt.bak
fi
output_file="$file.tmp"
## 生成json文件
echo '[' > "$output_file"
first_record=true
while IFS= read -r line
do
if [ "$first_record" = false ]; then
echo ',' >> "$output_file"
fi
echo " {\\"hostName\\": \\"$line\\"}" >> "$output_file"
first_record=false
done < "$file"
echo ']' >> "$output_file"
mv -f $output_file $file
chown hadoop:hadoop $file
chmod 755 $file
cat $file
本页内容是否解决了您的问题?