Bash - 逐行回显,忽略行之间的空间

Win*_*n.T 3 bash shell-script echo text-processing

我已经阅读了以下帖子: bash - 用新行替换空格,但它无助于解决我的问题。

   [My Directory]

[root@testlabs Config]# ls 
Archive Backup_Files
config_file.tgz
hostname1-config.uac
hostname2-config.uac
hostname3-config.uac
My-config_17Oct2014.tgz
non_extension-config_file1
non_extension-config_file2
[root@testlabs Config]#
Run Code Online (Sandbox Code Playgroud)

我需要从文件中回显 MD5 校验和结果列表。我可以这样做:

##IDENTIFY MD5 CHECKSUM##

 //To output the md5sum of the original file with a correct format to a temp file [md5<space><space>file_name]
ls $FULL_FILE_NAME | md5sum  $FULL_FILE_NAME > /tmp/md5sum.tmp

//To compare the md5sum of the orignal file with the md5sum of the backup copies in the archive directory
ls $CONFIG_ARCHIVE_DIR | grep -i --text $CONFIG_FILE_HOSTNAME-$FILE_TIMESTAMP.tgz | md5sum -c /tmp/md5sum.tmp >> /tmp/md5sum2.tmp

##COMPARING MD5 CHECKSUM##

 if [ -s /tmp/md5sum2.tmp ];
 then
   echo ""
   echo "Comparison of MD5 for files archived:"
   echo '---------------------------------------'
   /bin/sort -d /tmp/md5sum2.tmp
 fi
Run Code Online (Sandbox Code Playgroud)

这将是执行时的结果:(/tmp/md5sum2.tmp 中的内容的回显)

Comparison of MD5 for files archived:
---------------------------------------
 config_file.tgz: OK
 hostname1-config.uac: OK
 hostname2-config.uac: OK
 hostname3-config.uac: OK
 My-config_17Oct2014.tgz: OK
 non_extension-config_file1: OK
 non_extension-config_file1: OK
Run Code Online (Sandbox Code Playgroud)

##通缉##

但是我希望结果以这种方式显示:

Comparison of MD5 for files archived:
---------------------------------------
 - config_file.tgz: OK
 - hostname1-config.uac: OK
 - hostname2-config.uac: OK
 - hostname3-config.uac: OK
 - My-config_17Oct2014.tgz: OK
 - non_extension-config_file1: OK
 - non_extension-config_file2: OK
Run Code Online (Sandbox Code Playgroud)

我尝试这样做,(将 /tmp/md5sum2.tmp 的内容与前面的“-”回显到 /tmp/md5sum3.tmp 中)

1)

 ##COMPARING MD5 CHECKSUM##

  if [ -s /tmp/md5sum2.tmp ];
  then
  echo ""
  echo "Comparison of MD5 for files archived:"
  echo '---------------------------------------'
  /bin/sort -d /tmp/md5sum2.tmp

  for CONFIG_FILES in `/bin/cat /tmp/md5sum2.tmp`
  do
/bin/sort -d /tmp/md5sum2.tmp | grep $CONFIG_FILES > /tmp/md5sum3.tmp
  done

  for MD5_COMPARE in $(/bin/sort -d /tmp/md5sum3.tmp)
  do
   echo -e " - $MD5_COMPARE\n"
  done
 fi
Run Code Online (Sandbox Code Playgroud)

结果 1)

Comparison of MD5 for files archived:
 ---------------------------------------
 - config_file.tgz:
 - OK
 - hostname1-config.uac:
 - OK
 - hostname2-config.uac:
 - OK
 - hostname3-config.uac:
 - OK
 - My-config_17Oct2014.tgz.tgz:
 - OK
 - non_extension-config_file1:
 - OK
 - non_extension-config_file2:
 - OK
Run Code Online (Sandbox Code Playgroud)

2)

for MD5_COMPARE in $(/bin/sort -d /tmp/md5sum3.tmp)

  do
  echo -n " - $MD5_COMPARE"
  done
Run Code Online (Sandbox Code Playgroud)

结果 2)

  Comparison of MD5 for files archived:
  ---------------------------------------
- config_file.tgz: - OK - hostname1-config.uac: - OK - hostname2-config.uac: - OK - 
  hostname3-config.uac: - OK - My-config_17Oct2014.tgz: - OK - non_extension-config_file1: -
OK - non_extension-config_file2: - OK
Run Code Online (Sandbox Code Playgroud)

Cos*_*tas 5

逐行读取文件的标准程序是

while IFS= read -r MD5_COMPARE
do
  echo "- $MD5_COMPARE"
done < /tmp/md5sum2.tmp | /bin/sort -d
Run Code Online (Sandbox Code Playgroud)

但也sed应该工作

/bin/sort -d /tmp/md5sum2.tmp | sed 's/^/ -/'
Run Code Online (Sandbox Code Playgroud)