乌班图16.04
即使 for 的输出client in */; do
不会产生尾部斜杠,如果我在循环内对文件执行时间测试时回显变量 $client ,则会出现尾部斜杠。
cd "$wDir"
for client in */; do
cd "$wDir"/"$client";
#-- check to see if any .csv files exists
if ls *.csv &>/dev/null; then
for csvfile in *.csv; do
if test $(find "$csvfile" -mmin +2880); then
echo "$client has files older than 2 days ..." >> "staleFtpAccts"
fi
done
fi
done
当我执行脚本时,一个 / 被放置在 $client 变量后面,如下所示:
root@me /home/frank # bash script.sh
Start ...
000029_000020/ has files older than 2 days ...
#--
Finished ...
Start ...
000033_000040/ has files older than 2 days ...
#--
Finished ...
Start ...
000033_000041/ has files older than 2 days ...
#--
Finished ...
Start ...
000033_000042/ has files older than 2 days ...
#--
Finished ...
Start ...
000033_000043/ has files older than 2 days ...
#--
Finished ...
这就是我追求的结果...
root@me /home/frank # bash script.sh
Start ...
000029_000020 has files older than 2 days ...
#--
Finished ...
Start ...
000033_000040 has files older than 2 days ...
#--
Finished ...
Start ...
000033_000041 has files older than 2 days ...
#--
Finished ...
Start ...
000033_000042 has files older than 2 days ...
#--
Finished ...
Start ...
000033_000043 has files older than 2 days ...
#--
Finished ...
答案1
尝试这个POSIX 参数扩展:
echo "${client%/}"
所以
echo "${client%/} has files older than 2 days ..."
参数扩展扩展参数:
"$foo"
,"$1"
.您可以使用它来执行字符串或数组操作:"${file%.mp3}"
,"${0##*/}", "${files[@]: -4}"
.他们应该总是被引用。看:http://mywiki.wooledge.org/BashFAQ/073以及 man bash 中的“参数扩展”。另请参阅http://wiki.bash-hackers.org/syntax/pe。