如何将字符串更改为ksh中的路径

时间:2014-01-27 22:25:59

标签: bash ksh

ls -lAtr /data/log.* | tail -1 | awk '{ printf $9 }' > $logfile
echo $logfile
cat $logfile  # I want to cat the content of this log file, but this wouldn't work

logfile2=/usr/some/path/text.log
echo $logfile2
cat $logfile2 # This work

我是shell编程的新手,我想知道如何将日志文件转换为logfile2之类的东西(我问过正确的问题吗?),这样我就可以像对待文件一样对待它了。

2 个答案:

答案 0 :(得分:1)

您是否尝试将ls|tail|awk的结果存储在$logFile中?如果是这样的话:

logFile=$(ls -lAtr /data/log.* | tail -1 | awk '{ printf $9 }')

然而,you shouldn't parse the output of ls

答案 1 :(得分:1)

认为你正在寻找(也适用于bash)

logfile2="$(</usr/some/path/text.log)"

来自ksh手册页

$(cat file) can be replaced by the equivalent but faster $(<file).

e.g。

> cat text.log
line 1
line 2
> ksh
> logfile2="$(<text.log)"
> echo "$logfile2"
line 1
line 2