3

I have a little problem with a small script. The textfile has a lot of entries like:

permission-project1-admin
permission-project2-admin
....

The script looks like this (indeed, it is an awful one but still helps me):

#!/bin/bash
for i in $(cat adminpermission.txt); do
    permission=$(echo $i | cut -f1)

printf "dn:$permission,ou=groups,dc=domain,dc=com \n"
printf "objectclass: groupOfUniqueNames \n"
printf "objectclass: top \n"
printf "description: \n"
printf "cn:$permission \n\n"
done

The output looks fine, but because the textfile has a newline character at the end, the first line of printf is devided into two lines like:

dn:permission-project1-admin
,ou=groups,dc=domain,dc=com
objectclass: groupOfUniqueNames
objectclass: top
description:
cn:permission-project1-admin

My question is, how I can eliminate the newline character between the first two lines?

4

3 回答 3

3

首先正确地做。

while read permission rest
do
  ...
done < adminpermission.txt

此外,heredocs。

于 2013-01-23T22:29:34.733 回答
2

尝试:

$(echo $i | tr -d "\n" | cut -f1)
于 2013-01-23T22:32:21.790 回答
0

您是否检查过您的是否adminpermission.txt包含 DOS 样式的回车符?您的代码将去除换行符,但根据您查看输出的方式,回车符可能会像您描述的那样中断行。

你可以试试

mv adminpermission.txt backup.txt
tr -d '\r' < backup.txt > adminpermission.txt 

转换为 UNIX EOL,然后再次运行您的脚本。

于 2013-01-23T22:37:57.943 回答