我正在使用 bash 遍历文件文件夹,但我需要剪切前面的路径。例如,如果我有这个'/temp/test/filename',我想切断'/temp/test/'并将文件名存储到一个变量中,这样我就可以在其中写入一个带有文件名的日志。
谁能帮我吗?问题是变量 temp 始终为空。
这是我的 bash 代码:
#!/bin/bash
for file in /temp/test/*
do
if [[ ! -f "$file" ]]
then
continue
fi
temp="$file"|cut -d'/' -f3
$file > /var/log/$temp$(date +%Y%m%d%H%M%S).log
done
exit