![Linux 服务器设置是否可以将 echo 语句重定向到文件?](https://linux22.com/image/130629/Linux%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%AE%BE%E7%BD%AE%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%B0%86%20echo%20%E8%AF%AD%E5%8F%A5%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%E6%96%87%E4%BB%B6%EF%BC%9F.png)
首先我要说的是,我是 bash 脚本编写的新手。我的任务是尝试将正在写入名为“kiltslog.txt”的文本文件的内容重定向到我们网络上的另一个目录。注意:我没有编写以下脚本:它每 10 分钟运行一次...我不知道上面的日志文件是如何从以下脚本生成/更新的(如果有的话)。还有一件事......“kiltslog.txt”文件每 10 分钟更新一次,所有以“echo StartExec:”开头的“echo”语句都写入上述日志文件中。另外,我在脚本中的任何地方都没有看到任何对“kiltslog.txt”文件的引用。
#!/bin/bash
start_ts=`date +%s`
out_date=$(date -d @$start_ts --rfc-3339 ns)
echo StartExec: $out_date $start_ts
# min free space, in KB
# equal to 4TB
minSpaceThreshold=4294967296
echo "Checking free space..."
myUsed=$(df -k /nielsen_extracts | tail -1 | awk '{print $3}')
## this can be collapsed into one awk function here:
df -k /nielsen_extracts | tail -1 | /bin/awk '{if (int ($3)>4294967296) print "Enough space: " $3;else print "Not enough space " $3 }'
if [[ $myUsed -gt $minSpaceThreshold ]] ;
then
echo "We have enough free space:" $myUsed
else
echo "We do not have enough free space:" $myUsed
exit
fi
start_ts=`date +%s`
out_date=$(date -d @$start_ts --rfc-3339 ns)
echo DEVStartExec: $out_date $start_ts
# Use a lockfile containing the pid of the running process
# If script crashes and leaves lockfile around, it will have a different pid so
# will not prevent script running again.
#
lf=/tmp/pidLockFileKiltsProd
# create empty lock file if none exists
cat /dev/null >> $lf
read lastPID < $lf
# if lastPID is not null and a process with that pid exists , exit
[ ! -z "$lastPID" -a -d /proc/$lastPID ] && exit
echo not running
# save my pid in the lock file
echo $$ > $lf
nielsen_extracts_root=/nielsen_extracts
app_data_root=/nielsen_extracts/KiltsFilesRequests/AppData
requests_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests
queue_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/queue
processing_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/processing
complete_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/complete
request_name=
request_id=
request_user=
request_parent_path=
tmp_curl_url=
tmp_curl_param_NewStatus=
##ls $requests_root
# check queue for requests
##echo $(date +Y%m%d)
# tar request in scratch space
#find $requests_root/queue -maxdepth 1 -type d -print
DIRECTORIES=$(find $queue_root -mindepth 1 -type d)
for d in $DIRECTORIES
do
echo "Processing $d directory..."
echo "Moving $d from queue to processing directory..."
#cat $d/requestinfo | awk 'NR==2' | awk BEGIN { FS = ': " };
request_id=$(cat $d/requestinfo | awk 'BEGIN { FS = ":" } ; { print $2 }' | awk 'NR==1' | sed -e 's/^ *//g' -e 's/ *$//g')
request_name=$(cat $d/requestinfo | awk 'BEGIN { FS = ":" } ; { print $2 }' | awk 'NR==2' | sed -e 's/^ *//g' -e 's/ *$//g')
request_user=$(cat $d/requestinfo | awk 'BEGIN {FS = ":" } ; {print $2}' | awk 'NR==3' | sed -e 's/^ *//g' -e 's/ *$//g')
request_parent_path=$(cat $d/requestinfo | awk 'BEGIN {FS = ":" } ; {print $2}' | awk 'NR==4' | sed -e 's/^ *//g' -e 's/ *$//g')
request_id="$(echo $request_id | tr '[a-z'] '[A-Z]')"
echo "request_id: $request_id"
echo "request_name: $request_name"
echo "request_user: $request_user"
echo "request_parent_path: $request_parent_path"
echo "Updating status on front-end to PROCESSING..."
tmp_curl_url="https://kiltsfiles.chicagobooth.edu/Services/UpdateRequestStatus.aspx?RequestID="
tmp_curl_url="$tmp_curl_url$request_id"
tmp_curl_url="$tmp_curl_url&NewStatus=P"
echo "curling $tmp_curl_url ..."
curl $tmp_curl_url | grep updated
#exit
cd $d
pwd
cd ..
pwd
echo "Moving request data to processing..."
mv $request_id/ ../processing
#mv $d $processing_root
# create the subfolder in scratch
echo "Creating subdirectory in scratch /mnt/kiltGlobus/scratch/$request_id"
mkdir -p /mnt/kiltGlobus/scratch/$request_id
#tar the file list
echo "Running tar process with cvz args on request..."
tar cvz -T /nielsen_extracts/KiltsFilesRequests/AppData/Requests/processing/$request_id/filelist -f /mnt/kiltGlobus/scratch/$request_id/$request_name.tgz
#exit
echo "tar complete"
echo "Moving request data to complete..."
cd $processing_root
pwd
mv $request_id/ ../complete
#move to the globus endpoint
mkdir /mnt/kiltGlobus/RMS/$request_user
echo "Moving file to Globus endpoint (RMS)..."
mv /mnt/kiltGlobus/scratch/$request_id/$request_name.tgz /mnt/kiltGlobus/RMS/$request_user
#finish with email notification and front-end update
echo "Updating status on front-end to COMPLETE..."
tmp_curl_url="https://kiltsfiles.chicagobooth.edu/Services/UpdateRequestStatus.aspx?RequestID="
tmp_curl_url="$tmp_curl_url$request_id"
tmp_curl_url="$tmp_curl_url&NewStatus=C"
echo "curling $tmp_curl_url ..."
curl $tmp_curl_url | grep updated
echo "Cleaning up scratch dir..."
rm -rf /mnt/kiltGlobus/scratch/$request_id
done
end_ts=`date +%s`
out_date=$(date -d @$end_ts --rfc-3339 ns)
echo EndExec: $out_date $end_ts
ts_diff=$(($end_ts-$start_ts))
echo ExecTime: $ts_diff
答案1
该脚本的标准输出可能会重新路由到文件:
thescript >/path/to/kiltslog.txt
所以你必须改变它的名称(cron 作业或其他)。