无法在 Substring | 之后的变量中赋值Unix Shell 脚本

Unable to assign value in a variable after Substring | Unix Shell Scripting

这里发生了一些奇怪的事情,或者我自己也想不通。

我无法将一个变量的值赋给另一个变量。我正在做的是,检索 filename 然后 substring 检查最后 6 个字母,即 该文件的创建日期(yymmdd),需要进一步处理。 我已经列出了 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

这一行是问题所在:

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

应该是:

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