保存功能输出时出现随机错误

时间:2014-08-29 07:59:21

标签: shell sh

我试图创建一个包含行的.txt文件的脚本 像:

davda103:David:Davidsson:800104-1234:TNCCC_1:TDDB46 TDDB80:

然后对它们进行排序等等。这就是我的问题所在的背景:

#!/bin/sh -x
cat $1 |
while read a
do

testsak = `echo $a | cut -f 1 -d :`; <---**

echo $testsak;

done

箭头所在的位置,当我尝试运行此代码时,我会遇到一些奇怪的错误。

+ read a
+ cut -f+ echo  1 -d :davda103:David:Davidsson:800104-1234:TNCCC_1:TDDB46
TDDB80:
+ testsak = davda103
scriptTest.sh: testsak: Det går inte att hitta
+ echo

(我的瑞典语我的Linux是因为学校-.-)无论如何,这个错误只是说它找不到......某事。什么想法可能导致我的问题?

2 个答案:

答案 0 :(得分:4)

在赋值运算符周围有额外的空格,删除它们:

testsak=`echo $a | cut -f 1 -d :`; <---**

答案 1 :(得分:0)

等号周围的空格

testsak = `echo $a | cut -f 1 -d :`; <---**

导致bash将此解释为带有参数testak的命令=和命令替换的结果。删除空格将修复即时错误。

a中提取值的一种更有效的方法是让read执行此操作(并使用输入重定向而不是cat):

while IFS=: read testak the_rest; do
    echo $testak
done < "$1"
相关问题