替代方法而不是使用 for f in "$@" bash shell 命令?

Alternative method instead of using for f in "$@" bash shell command?

我有以下从 tinypng.com 获得的脚本,而不是在“$@”命令中使用 for f 我希望它只是 运行 在桌面上指定的文件夹中文件夹。谢谢!

echo ~/Desktop/FOLDER
for f in "$@"
do

   echo $f | while IFS= read file
   do
        filename=$(basename $file)
        ext=$(echo ${filename##*.} | tr "[:upper:]" "[:lower:]")
        if [ -f $file ]
        then
            if ( [ $ext == "png" ] || [ $ext == "jpg" ] || [ $ext == "jpeg" ] )
            then
                JSON=`curl -i --user api:00000000000000 --data-binary @$file https://api.tinypng.com/shrink 2>/dev/null  `
                URL=${JSON/*url\":\"/}
                URL=${URL/\"*/}

                curl $URL>${file} 2>/dev/null
            fi
        fi
    done

done

afplay /System/Library/Sounds/Submarine.aiff

假设您只是想遍历文件夹中的文件,那么 for f in ~/Desktop/FOLDER/* 应该可以。

或者 for f in $(find ~/Desktop/FOLDER -maxdepth 1 -type f) 这个也会从列表中删除所有子文件夹,这样你就可以删除 if [ -f $file ](但它也会列出隐藏文件,不确定这是否适合你的情况)