gpt4 book ai didi

linux - 杂乱和重复的输出

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:10 25 4
gpt4 key购买 nike

我的代码在功能上是正确的。问题是输出不是它应该的方式。事物的间距应该正确,并且出于某种原因,我在使用 $line 变量时得到了重复的文件名。

这是我的代码:

#!/bin/bash
printf "Enter a directory path."
read path
if [ -e $path ]
then
echo -e "Filename \t Lines \t \t Type"
echo -e "--------- \t ------ \t -----"
for f in `$path/ls`
do
if [ -f $f ]
then
lines=`wc -l $f`
echo -e " $f \t $lines \t \t Regular File "

else
echo -e " $f \t N/A \t \t Directory File "



fi
done
else
printf "Directory path does not exist"
fi

输出看起来像这样:

Filename         Lines           Type 
--------- ------ -----
Desktop N/A Directory File
Documents N/A Directory File
Downloads N/A Directory File
Music N/A Directory File
mycrontab 0 mycrontab Regular File
mycrontab.txt 1 mycrontab.txt Regular File
wc: mynewfile.txt: Permission denied
mynewfile.txt Regular File
myscript 26 myscript Regular File
myscript.sh 24 myscript.sh Regular File
mytextlog.txt 91 mytextlog.txt Regular File
Pictures N/A Directory File
Public N/A Directory File
Templates N/A Directory File
typescript 0 typescript Regular File
Videos N/A Directory File
yumassignment.txt 1784 yumassignment.txt Regular File

最佳答案

您的答案在评论中。一些代码审查:

  • 您的缩进风格……很有趣。
  • 使用read -p "prompt" path ,你不要单独打印提示
  • 始终引用变量,尤其是当值来自用户时:if [ -e "$path" ]
  • don't parse ls output : for f in "$path"/*
  • 如前所述:lines=$(wc -l < $"f")

关于linux - 杂乱和重复的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com