这个脚本将cvs列表分成三列。我们关注“name”列。我想找出字符最多的名称。一旦我找到了字符最多的名称,我想将其分配给一个变量。
#!/bin/bash
for i in $(cat homeaway.txt )
do
echo $i | while IFS=, read -r area name host
do
maxLength=0
length=${#name}
if [ $length -gt $maxLength ] ; then
maxLength=$length
else
:
fi
printf "%s\n" $maxLength
done
done
该脚本的意思是:如果长度大于最大长度(maxlength),则将长度设置为maxLength,否则不做任何操作。其中包含最多字符的区域字符串是“script_name_12345678999999”,它有26个字符。当脚本读取所有字符时,$maxLength应返回26。
__DATA__
HOME,script_name_12345,USAhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_1,EUROhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_123456,EUROhost.com
AWAY,script_name_12345678999999,USAhost.com
HOME,script_name_1234,USAhost.com
AWAY,script_name_1234578,USAhost.com
HOME,script_name_12,EUROhost.com
AWAY,script_name_123456789,USAhost.com
一旦脚本到达26个字符的区域值,应停止将任何内容分配给$maxLength。 取而代之的是,它返回每个字符串长度的列表,我不知道为什么会出现零。
casper@casper01.com $ ./length_test.sh
17
0 ### how does the zero get in here ?
15
13
15
18
26 ###script_name_12345678999999
16
19
14
21
maxLength=0
吗? - David C. Rankin