无法在Substring |之后为变量赋值Unix Shell Scripting

时间:2015-01-12 07:34:31

标签: shell unix ksh

这里发生了一些奇怪的事情,或者我无法弄清楚自己。

我无法将值从一个变量赋值给其他变量。我正在做的是,检索文件名,然后 substring ,检查最后6个字母,即 创建日期(yymmdd) 该文件,需要进一步处理。 我列出了2个不同的调试版本,它正在重新启动文件名,但是在子字符串中发生了一些问题并将其分配给另一个变量。

  • 虽然在basename本身期间,它对子字符串做得很好(转到 调试版本2

调试版本1:

代码:

fname=''
fdate=''
for file in /home/fimsctl/datafiles/outbound/timelog/timelog_file_*.csv ; do

    echo "Debugging Test: 123"

    fname=` basename $file `

    echo "Debugging Test: 456"

    echo "$fname"

    echo "Debugging Test: 789"

    fdate=` $fname | cut -c2-4 `

    echo "Debugging Test: abc"

    echo "$fdate"

    echo "Debugging Test: def"

  done

输出:

Debugging Test: 123
Debugging Test: 456
timelog_file_150112.csv
Debugging Test: 789
testb.ksh[119]: timelog_file_150112.csv:  not found
Debugging Test: abc

Debugging Test: def

调试版本2:

代码:

fname=''
fdate=''
for file in /home/fimsctl/datafiles/outbound/timelog/timelog_file_*.csv ; do

    echo "Debugging Test: 123"

    fname=` ( basename $file ) | cut -c14-19 `

    echo "Debugging Test: 456"

    echo "$fname"

    echo "Debugging Test: 789"

    fdate=` $fname | cut -c2-4 `

    echo "Debugging Test: abc"

    echo "$fdate"

    echo "Debugging Test: def"

  done

输出

Debugging Test: 123
Debugging Test: 456
150112
Debugging Test: 789
testb.ksh[119]: 150112:  not found
Debugging Test: abc

Debugging Test: def

1 个答案:

答案 0 :(得分:2)

这一行是问题所在:

fdate=` $fname | cut -c2-4 `

应该是:

fdate=`echo "$fname" | cut -c2-4`